jdk 加密
案例一:对称加密
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.tomcat.util.codec.binary.Base64;
public class ChiperUtil {
/**
*
* @param str
* @param key 对称加密 key必须为16位
* @return
* @throws Exception
*/
public static String aesEncrypt(String str,String key) throws Exception{
if(str ==null || key ==null){
return null;
}
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"));
byte[] bytes = cipher.doFinal(str.getBytes("utf-8"));
return new Base64().encodeToString(bytes);
}
public static String aesDeccrypt(String str,String key) throws Exception{
if(str ==null||key==null){
return null;
}
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"));
byte[] bytes = new Base64()

本文主要介绍了JAVA中对称加密的原理和使用方法,通过一个具体的案例展示了如何进行加解密操作,深入理解JAVA的加密技术。
最低0.47元/天 解锁文章
1239

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



