AES 256 32 bytes key Encrypt/Decrypt Java
本问题已经有最佳答案,请猛点这里访问。
我需要使用带有32字节密钥的AES 256来加密XML消息(它以字符串形式出现)。 我尝试了以下(来自http://aesencryption.net/):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Base64; /** Aes encryption */ public class AES { private static SecretKeySpec secretKey ; private static byte[] key ; private static String decryptedString; private static String encryptedString; public static void setKey(String myKey){ MessageDigest sha = null; try { key = myKey.getBytes("UTF-8"); System.out.println(key.length); sha = MessageDigest.getInstance("SHA-1"); key = sha.digest(key); key = Arrays.copyOf(key, 16); // use only first 128 bit System.out.println(key.length); System.out.println(new String(key,"UTF-8")); secretKey = new SecretKeySpec(key,"AES"); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static String getDecryptedString() { return decryptedString; } public static void setDecryptedString(String decryptedString) { AES.decryptedString = decryptedString; } public static String getEncryptedString() { return encryptedString; } public static void setEncryptedString(String encryptedString) { AES.encryptedString = encryptedString; } public static String encrypt(String strToEncrypt) { try { Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); setEncryptedString(Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes("UTF-8")))); } catch (Exception e) { System.out.println("Error while encrypting:"+e.toString()); } return null; } public static String decrypt(String strToDecrypt) { try { Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING"); cipher.init(Cipher.DECRYPT_MODE, secretKey); setDecryptedString(new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt)))); } catch (Exception e) { System.out.println("Error while decrypting:"+e.toString()); } return null; } public static void main(String args[]) { final String strToEncrypt ="My text to encrypt"; final String strPssword ="C0BAE23DF8B51807B3E17D21925FADF273A70181E1D81B8EDE6C76A5C1F1716E"; AES.setKey(strPssword); AES.encrypt(strToEncrypt.trim()); System.out.println("String to Encrypt:" + strToEncrypt); System.out.println("Encrypted:" + AES.getEncryptedString()); final String strToDecrypt = AES.getEncryptedString(); AES.decrypt(strToDecrypt.trim()); System.out.println("String To Decrypt :" + strToDecrypt); System.out.println("Decrypted :" + AES.getDecryptedString()); } } |
我试图将键数组长度从16更改为32并使用更大的输入字符串
(我认为这是32个长度的关键)但这不起作用。
1
要加密的消息将如下所示:
1 2 3 4 5 6 | <Estructure> <General>GENERAL DATA</General> </Estructure> <Datos> <DATE>20140606</DATE> </Datos> |
当我运行时,我得到以下异常:
Error while encrypting: java.security.InvalidKeyException: Illegal key size or default parameters
使用16字节长度键时,它可以正常工作。 如何使用32个字节?
默认情况下,在JVM中禁用任何高于128位加密的内容,因为Oracle在美国管辖范围内运行。
如果您需要超过128位加密,则必须下载Java密码术扩展(JCE)无限强度管辖权政策文件8下载并将jar文件放入JRE / JDK