3DES加密java的实现代码
加密算法的实现 2006-10-29 10:12:07 阅读249 评论0 字号:大中小
3DES加密java的实现代码
public void getKey(String strKey)
{
try{
KeyGenerator _generator = KeyGenerator.getInstance("DES");
_generator.init(new SecureRandom(strKey.getBytes()));
this.key = _generator.generateKey();
_generator=null;
}catch(Exception e){
e.printStackTrace();
}
}
private byte[] getDesCode(byte[] byteD)
{
Cipher cipher;
byte[] byteFina=null;
try{
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byteFina = cipher.doFinal(byteD);
}catch(Exception e){
e.printStackTrace();
}finally{
cipher=null;
}
return byteFina;
}
public static void main(String[] args){
System.out.println("hello");
DesEncrypt des=new DesEncrypt();//实例化一个对像
des.getKey("aadd");//生成密匙
String strEnc = des.getEncString("钟汉康");//加密字符串,返回String的密文
System.out.println(strEnc);
String strDes = des.getDesString(strEnc);//把String 类型的密文解密
System.out.println(strDes);
}
}
本文提供了使用Java实现3DES加密的具体代码示例。通过KeyGenerator生成密钥,并利用Cipher类进行加解密操作,展示了完整的加密流程及其实现细节。

1036

被折叠的 条评论
为什么被折叠?



