AESEncrypter加密算法代码示例详解编程语言

package testJava.java; 
import java.security.SecureRandom; 
import java.util.Base64; 
import javax.crypto.Cipher; 
import javax.crypto.KeyGenerator; 
import javax.crypto.SecretKey; 
import javax.crypto.spec.SecretKeySpec; 
/** 
* @Author xx 
* @Date 2014年3月26日 
* @Comments (美国软件出口限制,AES算法,秘钥长度大于128位时需替换对应jdk版本的policy文件: ${java_home}/jre/lib/security/local_policy.jar 和 ${java_home}/jre/lib/security/US_export_policy.jar) 
*/ 
public class AESEncrypter { 
private static final String ENCODEING = "UTF-8"; 
private static final String ALGORITHM = "AES"; 
    private static final String KEY = "2016aes"; 
private static Cipher cipher_encrypt = null;//加密密码器 
private static Cipher cipher_decrypt = null;//解密密码器 
static{ 
try { 
KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM); 
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG" );   
secureRandom.setSeed(KEY.getBytes()); 
kgen.init(128, secureRandom);  
//kgen.init(256, secureRandom);//256位长度秘钥 
SecretKey secretKey = kgen.generateKey(); 
byte[] secretKeyEncoded = secretKey.getEncoded(); 
SecretKeySpec sks = new SecretKeySpec(secretKeyEncoded, ALGORITHM); 
cipher_encrypt = Cipher.getInstance(ALGORITHM); 
cipher_encrypt.init(Cipher.ENCRYPT_MODE, sks); 
cipher_decrypt = Cipher.getInstance(ALGORITHM); 
cipher_decrypt.init(Cipher.DECRYPT_MODE, sks); 
} catch (Exception e) { 
e.printStackTrace(); 
} 
} 
/** 
* 加密 
* @param content 需要加密的内容 
* @return 
*/ 
public static String encrypt(String content) throws Exception{ 
byte[] result = cipher_encrypt.doFinal(content.getBytes(ENCODEING)); 
//            return  Base64.encodeBase64String(result); 
return new String(Base64.getEncoder().encode(result)); 
} 
/** 
* 解密 
* @param content 
* @return 
* @throws Exception 
*/ 
public static String decrypt(String content) throws Exception { 
byte[] result =  cipher_decrypt.doFinal(Base64.getDecoder().decode(content)); 
//            byte[] result =  cipher_decrypt.doFinal(Base64.decodeBase64(content)); 
return new String(result,ENCODEING); 
} 
/** 
* 对字符串数组里的字符串加密 
* @param content 
* @return 
* @throws Exception 
*/ 
public static String[] encryptStrArray(String ...content) throws Exception{ 
for (int i = 0; i < content.length; i++) { 
byte[] result = cipher_encrypt.doFinal(content[i].getBytes(ENCODEING)); 
//            String encryptedStr = Base64.encodeBase64String(result); 
String encryptedStr = new String(Base64.getEncoder().encode(result)); 
content[i] = encryptedStr; 
} 
return content; 
} 
public static void main(String[] args) throws Exception { 
String content = "公布修改五部法律";  //加密    
System.out.println("加密前:" + content);   
String codeStr = encrypt(content); 
System.out.println("加密后:" + codeStr);   
System.out.println("加密后 length:" + codeStr.getBytes("UTF-8").length); 
//解密    
String decryptResult = decrypt(codeStr);   
System.out.println("解密后:" + decryptResult); 
System.out.println("done!"); 
} 
/** 
* 加密前:公布修改五部法律 
加密后:B2Pu0KBO/FGuttUxSpT9/544jgp4OEoL8L4o4N85a0I= 
加密后 length:44 
解密后:公布修改五部法律 
done! 
*/ 
}

 

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/15252.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论