Skip to content

Instantly share code, notes, and snippets.

@pfmiles
Created January 6, 2015 09:24
Show Gist options
  • Save pfmiles/d911d5488be5b23d0d07 to your computer and use it in GitHub Desktop.
Save pfmiles/d911d5488be5b23d0d07 to your computer and use it in GitHub Desktop.
自定义对称密钥的AES加密、解密方案
package test;
import java.security.MessageDigest;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class AesEncDec {
// 加密算法
private static final String ALG = "AES";
// 字符编码
private static final String ENC = "UTF-8";
// 密钥正规化算法
private static final String SEC_NORMALIZE_ALG = "MD5";
public static void main(String... args) throws Exception {
String secret = "aaaaa";// 密钥
// 加密
String text = "Hello World! 你好,世界!";// 需要加密的数据原文
String encrypted = encrypt(secret, text);
System.out.println(encrypted);
// 解密
System.out.println(decrypt(secret, encrypted));
}
// 加密
public static String encrypt(String secret, String data) throws Exception {
MessageDigest dig = MessageDigest.getInstance(SEC_NORMALIZE_ALG);
byte[] key = dig.digest(secret.getBytes(ENC));
SecretKeySpec secKey = new SecretKeySpec(key, ALG);
Cipher aesCipher = Cipher.getInstance(ALG);
byte[] byteText = data.getBytes(ENC);
aesCipher.init(Cipher.ENCRYPT_MODE, secKey);
byte[] byteCipherText = aesCipher.doFinal(byteText);
Base64 base64 = new Base64();
return new String(base64.encode(byteCipherText), ENC);
}
// 解密
public static String decrypt(String secret, String ciphertext) throws Exception {
MessageDigest dig = MessageDigest.getInstance(SEC_NORMALIZE_ALG);
byte[] key = dig.digest(secret.getBytes(ENC));
SecretKeySpec secKey = new SecretKeySpec(key, ALG);
Cipher aesCipher = Cipher.getInstance(ALG);
aesCipher.init(Cipher.DECRYPT_MODE, secKey);
Base64 base64 = new Base64();
byte[] cipherbytes = base64.decode(ciphertext.getBytes());
byte[] bytePlainText = aesCipher.doFinal(cipherbytes);
return new String(bytePlainText, ENC);
}
}
@pfmiles
Copy link
Author

pfmiles commented Jan 6, 2015

将SEC_NORMALIZE_ALG的值改为"SHA-256"即可得到AES 256的加密强度

@hangox
Copy link

hangox commented Nov 8, 2015

本来的Java并不支持128位以上的密钥,需要在这里 下载拓展包才能支持128位以上

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment