java简单加密代码 java加密的几种方法(15)


char[] ob = new char[ ];
ob[ ] = Digit[(ib)X F];
ob[ ] = Digit[ibX F];
String s = new String(ob);
return s;
}
public static void main(String args[]) {
MD m = new MD ();
if (Array getLength(args) == ) { //如果没有参数 执行标准的Test Suite
System out println( MD Test suite: );
System out println( MD (\ \ ): + m getMD ofStr( ));
System out println( MD (\ a\ ): + m getMD ofStr( a ));
System out println( MD (\ abc\ ): + m getMD ofStr( abc ));
System out println( MD (\ \ ): + m getMD ofStr( ));
System out println( MD (\ \ ): + m getMD ofStr( ));
System out println( MD (\ message digest\ ): + m getMD ofStr( message digest ));
System out println( MD (\ abcdefghijklmnopqrstuvwxyz\ ): + m getMD ofStr( abcdefghijklmnopqrstuvwxyz ));
System out println( MD (\ ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz \ ):
+ m getMD ofStr( ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ));
} else
System out println( MD ( + args[ ] + )= + m getMD ofStr(args[ ]));
}
lishixinzhi/Article/program/Java/hx/201311/26604
java加密解密代码package com.cube.limail.util;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;/**
* 加密解密类
*/
publicclassEryptogram
{
privatestaticStringAlgorithm ="DES";
private String key="CB7A92E3D3491964";
//定义 加密算法,可用 DES,DESede,Blowfish
staticbooleandebug= false ;
/**
* 构造子注解.
*/
publicEryptogram ()
{
} /**
* 生成密钥
* @return byte[] 返回生成的密钥
* @throws exception 扔出异常.
*/
publicstaticbyte [] getSecretKey () throwsException
{
KeyGeneratorkeygen= KeyGenerator.getInstance (Algorithm );
SecretKeydeskey= keygen.generateKey ();
System.out.println ("生成密钥:"+bytesToHexString (deskey.getEncoded ()));
if(debug ) System.out.println ("生成密钥:"+bytesToHexString (deskey.getEncoded ()));
returndeskey.getEncoded ();
} /**
* 将指定的数据根据提供的密钥进行加密
* @param input 需要加密的数据
* @param key 密钥
* @return byte[] 加密后的数据
* @throws Exception
*/
publicstaticbyte [] encryptData (byte [] input ,byte [] key ) throwsException
{
SecretKeydeskey= newjavax.crypto.spec.SecretKeySpec (key ,Algorithm );
if(debug )
{
System.out.println ("加密前的二进串:"+byte2hex (input ));
System.out.println ("加密前的字符串:"+newString (input ));
} Cipherc1= Cipher.getInstance (Algorithm );
c1.init (Cipher.ENCRYPT_MODE ,deskey );
byte [] cipherByte =c1.doFinal (input );
if(debug ) System.out.println ("加密后的二进串:"+byte2hex (cipherByte ));
returncipherByte ;
} /**
* 将给定的已加密的数据通过指定的密钥进行解密
* @param input 待解密的数据
* @param key 密钥
* @return byte[] 解密后的数据
* @throws Exception
*/
publicstaticbyte [] decryptData (byte [] input ,byte [] key ) throwsException
{
SecretKeydeskey= newjavax.crypto.spec.SecretKeySpec (key ,Algorithm );
if(debug ) System.out.println ("解密前的信息:"+byte2hex (input ));
Cipherc1= Cipher.getInstance (Algorithm );
c1.init (Cipher.DECRYPT_MODE ,deskey );
byte [] clearByte =c1.doFinal (input );
if(debug )
{
System.out.println ("解密后的二进串:"+byte2hex (clearByte ));
System.out.println ("解密后的字符串:"+(newString (clearByte )));
} returnclearByte ;
} /**
* 字节码转换成16进制字符串
* @param byte[] b 输入要转换的字节码
* @return String 返回转换后的16进制字符串
*/

推荐阅读