otp算法java源代码 otp工作原理

求java版汉诺塔的演示程序

源代码:

创新互联公司于2013年创立,先为东乃等服务建站,东乃等地企业,进行企业商务咨询服务。为东乃企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

/**

*本程序完成的功能是利用汉递规算法实现汉诺塔的动态演示程序

*/

import javax.swing.*;

import java.awt.geom.*;

import java.awt.event.*;

import java.awt.*;

public class Hanio extends JApplet implements ActionListener, Runnable

{

/**

*diskNum是盘子的数量

*/

private int diskNum ;

/**

*各个组件的句柄

*/

private JButton begin, stop;

private JLabel lDiskNum;

private JTextField text;

JPanel pane;

/**

*定义一个线程句柄

*/

private Thread animate;

/**

*定义a,b,c三个柱子上是否有盘子,有哪些盘子

*/

private int adisk[];

private int bdisk[];

private int cdisk[];

public void init()

{

Container content = getContentPane();

content.setLayout(new BorderLayout());

lDiskNum = new JLabel(盘子的数目);

text = new JTextField(8);

begin = new JButton(开始);

begin.addActionListener(this);

stop = new JButton(停止);

stop.addActionListener(this);

pane = new JPanel();

pane.setLayout(new FlowLayout());

pane.add(lDiskNum);

pane.add(text);

pane.add(begin);

pane.add(stop);

content.add(pane, BorderLayout.SOUTH);

}

public void paint(Graphics g)

{

Graphics2D g2D = (Graphics2D)g;

Ellipse2D.Double ellipse;

g2D.setPaint(getBackground());

if(adisk != null)

{

/**

*消除以前画的盘子

*/

for(int j=adisk.length, i=0; --j=0; i++ )

{

ellipse = new Ellipse2D.Double(20+i*5, 180-i*10, 180-i*10, 20);

g2D.fill(ellipse);

ellipse = new Ellipse2D.Double(220+i*5, 180-i*10, 180-i*10, 20);

g2D.fill(ellipse);

ellipse = new Ellipse2D.Double(420+i*5, 180-i*10, 180-i*10, 20);

g2D.fill(ellipse);

}

drawEllipse(g, 20, adisk);//画A组盘子

drawEllipse(g, 220, bdisk);//画B组盘子

drawEllipse(g, 420, cdisk);//画C组盘子

}

pane.repaint(); 

}

public void update(Graphics g)

{

paint(g);

}

/**画出椭圆代表盘子,g是图形环境,x是最下面的盘子的横坐标,

*arr是柱子数组

*/

public void drawEllipse(Graphics g,int x,int arr[])

{

Graphics2D g2D = (Graphics2D)g;

Ellipse2D.Double ellipse;

g2D.setPaint(Color.gray);

g2D.draw(new Line2D.Double(x+90, 10, x+90, 180));

for(int j=arr.length, i=0; --j=0; i++ )

if(arr[j] != 0)

{

if(i%2 == 0)

g2D.setPaint(Color.blue);

else

g2D.setPaint(Color.red);

ellipse = new Ellipse2D.Double(x+i*5, 180-i*10, 180-i*10, 20);

g2D.fill(ellipse);

}

}

public void actionPerformed(ActionEvent e)

{

String command = e.getActionCommand();

if(command.equals(开始))

{

/**

*进行初始化,开始的时候只有a柱子上有盘子,其他柱子都没有

*/

diskNum = Integer.parseInt(text.getText());

adisk = new int[diskNum];

for(int i=0; iadisk.length; i++)

adisk[i] = 1;

bdisk = new int[diskNum];

for(int k=0; kbdisk.length; k++)

bdisk[k] = 0;

cdisk = new int[diskNum];

for(int i=0; icdisk.length; i++)

cdisk[i] = 0;

repaint();

if(animate == null || !animate.isAlive())//创建一个线程

{

animate = new Thread(this);

animate.start();

}

}

if(command.equals(停止))

{

for(int k=0; kbdisk.length; k++)

bdisk[k] = 0;

for(int i=0; icdisk.length; i++)

cdisk[i] = 0;

repaint();

text.setText();

animate = null;

}

}

/**

*线程方法,在此调用汉诺塔执行移动盘子操作

*/

public void run()

{

hanio(diskNum, 'A', 'B', 'C');

repaint();

}

/**

*汉诺塔递规调用程序,n是盘子的数量,A,B,C分别代表三个柱子

*/

public void hanio(int n, char A, char B, char C)

{

if(n 1)

{

hanio(n-1, A, C, B);

pause();//停顿几秒在执行

switch(A)

{

case 'A':adisk[n-1] = 0;break;

case 'B':bdisk[n-1] = 0;break;

case 'C':cdisk[n-1] = 0;break;

default:break;

}

switch(C)

{

case 'A':adisk[n-1] = 1;break;

case 'B':bdisk[n-1] = 1;break;

case 'C':cdisk[n-1] = 1;break;

default:break;

}

repaint();

hanio(n-1, B, A, C);

}

pause();

switch(A)

{

case 'A':adisk[n-1] = 0;break;

case 'B':bdisk[n-1] = 0;break;

case 'C':cdisk[n-1] = 0;break;

default:break;

}

switch(C)

{

case 'A':adisk[n-1] = 1;break;

case 'B':bdisk[n-1] = 1;break;

case 'C':cdisk[n-1] = 1;break;

default:break;

}

repaint();

}

/**

*每隔半妙钟移动一个盘子

*/

public void pause()

{

try{

Thread.sleep(500);//可以修改此值加快盘子移动的速度

}catch(InterruptedException e){}

}

}

求java加密源代码(MD5,base64)

import java.security.*;

import javax.crypto.*;

/**

* 本例解释如何利用DES私钥加密算法加解密

*

* @author Devon

* @version 1.0 04/03/10

*/

public class SingleKeyExample {

public static void main(String[] args) {

try {

String algorithm = "DES"; //定义加密算法,可用 DES,DESede,Blowfish

String message = "Hello World. 这是待加密的信息";

// 生成个DES密钥

KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm);

keyGenerator.init(56); //选择DES算法,密钥长度必须为56位

Key key = keyGenerator.generateKey(); //生成密钥

// 生成Cipher对象

Cipher cipher = Cipher.getInstance("DES");

//用密钥加密明文(message),生成密文(cipherText)

cipher.init(Cipher.ENCRYPT_MODE, key); //操作模式为加密(Cipher.ENCRYPT_MODE),key为密钥

byte[] cipherText = cipher.doFinal(message.getBytes()); //得到加密后的字节数组

System.out.println("加密后的信息: " + new String(cipherText));

//用密钥加密明文(plainText),生成密文(cipherByte)

cipher.init(Cipher.DECRYPT_MODE, key); //操作模式为解密,key为密钥

byte[] sourceText = cipher.doFinal(cipherText); //获得解密后字节数组

System.out.println("解密后的信息: " + new String(sourceText));

} catch (Exception ex) {

ex.printStackTrace();

}

}

}

/**

* @author Devon

*/

import java.security.*;

import java.security.spec.*;

import javax.crypto.*;

public class PairKeyExample {

public static void main(String argv[]) {

try {

String algorithm = "RSA"; //定义加密算法,可用 DES,DESede,Blowfish

String message = "张三,你好,我是李四";

//产生张三的密钥对(keyPairZhang)

KeyPairGenerator keyGeneratorZhang =

KeyPairGenerator.getInstance(algorithm); //指定采用的算法

keyGeneratorZhang.initialize(1024); //指定密钥长度为1024位

KeyPair keyPairZhang = keyGeneratorZhang.generateKeyPair(); //产生密钥对

System.out.println("生成张三的公钥对");

// 张三生成公钥(publicKeyZhang)并发送给李四,这里发送的是公钥的数组字节

byte[] publicKeyZhangEncode = keyPairZhang.getPublic().getEncoded();

//通过网络或磁盘等方式,把公钥编码传送给李四

//李四接收到张三编码后的公钥,将其解码

KeyFactory keyFacoryLi = KeyFactory.getInstance(algorithm); //得到KeyFactory对象

X509EncodedKeySpec x509KeySpec =

new X509EncodedKeySpec(publicKeyZhangEncode); //公钥采用X.509编码

PublicKey publicKeyZhang = keyFacoryLi.generatePublic(x509KeySpec); //将公钥的KeySpec对象转换为公钥

System.out.println("李四成功解码,得到张三的公钥");

//李四用张三的公钥加密信息,并发送给李四

Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); //得到Cipher对象

cipher.init(Cipher.ENCRYPT_MODE, publicKeyZhang); //用张三的公钥初始化Cipher对象

byte[] cipherMessage = cipher.doFinal(message.getBytes()); //得到加密信息

System.out.println("加密后信息:" + new String(cipherMessage));

System.out.println("加密完成,发送给李四...");

//张三用自己的私钥解密从李四处收到的信息

cipher.init(Cipher.DECRYPT_MODE, keyPairZhang.getPrivate()); //张三用其私钥初始化Cipher对象

byte[] originalMessage = cipher.doFinal(cipherMessage); //得到解密后信息

System.out.println("张三收到信息,解密后为:" + new String(originalMessage));

} catch (Exception ex) {

ex.printStackTrace();

}

}

}

求几个JAVA小项目源代码,供自己学习参考

package com.test01;

import java.util.Scanner;

public class oop5 { public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

// int x = 9;

// int y = 1;

int x = sc.nextInt();

int y = sc.nextInt();

int z;

z = add(x, y);

System.out.println("x的值为:" + x);

System.out.println("y的值为:" + y);

System.out.println("二者之和为:" + z);

}

/** 四种小算法 */

// 加法运算

public static int add(int a, int b) {

int c;

c = a + b;

return c;

}

// 减法运算

public static int jian(int d, int v) {

int m;

m = d - v;

return m;

}

// 乘积运算

public static int addAdd(int q, int w) {

int e;

e = q * w;

return e;

}

// 除法运算

public static int chu(int p, int k) {

int f;

f = p / k;

return f;

}

}


文章名称:otp算法java源代码 otp工作原理
文章链接:http://myzitong.com/article/doeossp.html