AES 和 DES 都是對稱加密的一種,可是 DES 的 Key 是 56 位,而 AES 的 Key 有 128,256,512 可選。 apache
AES dom
加密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; }
解密AESspa
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 code
加密DESblog
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; }
解密DESip
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 方法ci
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:helloget