AES 和 DES 都是对称加密的一种,但是 DES 的 Key 是 56 位,而 AES 的 Key 有 128,256,512 可选。
AES
加密AES
String randomKey = "12345678";
public static String ENAES() {
try {
KeyGenerator keyGene = KeyGenerator.getInstance("AES");
keyGene.init(128, new SecureRandom(randomKey.getBytes()));
SecretKey key = keyGene.generateKey();
byte[] bytes = key.getEncoded();
SecretKeySpec keySpec = new SecretKeySpec(bytes, "AES");
Cipher ciper = Cipher.getInstance("AES");
ciper.init(Cipher.ENCRYPT_MODE, keySpec);
bytes = ciper.doFinal("hello".getBytes());
String result = org.apache.commons.codec.binary.Base64.encodeBase64String(bytes);
return result;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
解密AES
String randomKey = "12345678";
public static String DEAES(String str) {
try {
byte[] srcBytes = org.apache.commons.codec.binary.Base64
.decodeBase64(str);
KeyGenerator keyGene = KeyGenerator.getInstance("AES");
keyGene.init(128, new SecureRandom(randomKey.getBytes()));
SecretKey key = keyGene.generateKey();
byte[] bytes = key.getEncoded();
SecretKeySpec keySpec = new SecretKeySpec(bytes, "AES");
Cipher ciper = Cipher.getInstance("AES");
ciper.init(Cipher.DECRYPT_MODE, keySpec);
bytes = ciper.doFinal(srcBytes);
String strs = new String(bytes);
return strs;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
DES
加密DES
String randomKey = "12345678";
public static String ENDES() {
SecureRandom random = new SecureRandom();
try {
DESKeySpec desKey = new DESKeySpec(randomKey.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(desKey);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
byte[] bytes = cipher.doFinal("hello".getBytes());
String result = Base64.getEncoder().encodeToString(bytes);
return result;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
解密DES
String randomKey = "12345678";
public static String DEDES(String str) {
byte[] bytes = Base64.getDecoder().decode(str);
SecureRandom random = new SecureRandom();
try {
DESKeySpec desKey = new DESKeySpec(randomKey.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(desKey);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, securekey, random);
bytes = cipher.doFinal(bytes);
String result = new String(bytes);
return result;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
mian 方法
public static void main(String[] args) throws NoSuchAlgorithmException { System.out.println("AES ENCRYPT:" + ENAES()); System.out.println("AES DECRYPT:" + DEAES(ENAES())); System.out.println("DES ENCRYPT:" + ENDES()); System.out.println("DES DECRYPT:" + DEDES(ENDES())); }
运行结果
AES ENCRYPT:70IScgmG93zMpkKvsNs+TQ==
AES DECRYPT:hello
DES ENCRYPT:uhbGoCVxJa8=
DES DECRYPT:hello