抑郁症健康,内容丰富有趣,生活中的好帮手!
抑郁症健康 > java ase 加密_java实现ase加密解密

java ase 加密_java实现ase加密解密

时间:2018-08-16 13:07:42

相关推荐

展开全部

这个算法java SDK自带的额 参考代码如下:

/**解密

*@paramcontent待解密内容

*@parampassword解密密钥

*@return

*/

publicstaticbyte[]decrypt(byte[]content,Stringpassword){

try{

KeyGeneratorkgen=KeyGenerator.getInstance("AES");

kgen.init(128,newSecureRandom(password.getBytes()));

SecretKeysecretKey=kgen.generateKey();

byte[]enCodeFormat=secretKey.getEncoded();

SecretKeySpeckey=newSecretKeySpec(enCodeFormat,"AES");

Ciphercipher=Cipher.getInstance("AES");//创建密码器

cipher.init(Cipher.DECRYPT_MODE,key);//初始化

byte[]result=cipher.doFinal(content);

returnresult;//加密

}catch(NoSuchAlgorithmExceptione){

e.printStackTrace();

}catch(NoSuchPaddingExceptione){

e.printStackTrace();

}catch(InvalidKeyExceptione){

e.printStackTrace();

}catch(IllegalBlockSizeExceptione){

e.printStackTrace();

}catch(BadPaddingExceptione){

e.printStackTrace();

}

returnnull;

}

/**

*加密

*

*@paramcontent需要加32313133353236313431303231363533e58685e5aeb931333335303437密的内容

*@parampassword加密密码

*@return

*/

publicstaticbyte[]encrypt(Stringcontent,Stringpassword){

try{

KeyGeneratorkgen=KeyGenerator.getInstance("AES");

kgen.init(128,newSecureRandom(password.getBytes()));

SecretKeysecretKey=kgen.generateKey();

byte[]enCodeFormat=secretKey.getEncoded();

SecretKeySpeckey=newSecretKeySpec(enCodeFormat,"AES");

Ciphercipher=Cipher.getInstance("AES");//创建密码器

byte[]byteContent=content.getBytes("utf-8");

cipher.init(Cipher.ENCRYPT_MODE,key);//初始化

byte[]result=cipher.doFinal(byteContent);

returnresult;//加密

}catch(NoSuchAlgorithmExceptione){

e.printStackTrace();

}catch(NoSuchPaddingExceptione){

e.printStackTrace();

}catch(InvalidKeyExceptione){

e.printStackTrace();

}catch(UnsupportedEncodingExceptione){

e.printStackTrace();

}catch(IllegalBlockSizeExceptione){

e.printStackTrace();

}catch(BadPaddingExceptione){

e.printStackTrace();

}

returnnull;

}

如果觉得《java ase 加密_java实现ase加密解密》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。