2016-09-14 258 views
0

我試圖使用加密一些數據AES加密和解密。在那裏,我創建了一個Cipher有以下paramters,Java加密:NoSuchAlgorithmException錯誤

算法的名字 - AES

模式 - CBC模式

填充 - PKCS7

Cipher aesCipherForEncryption = Cipher.getInstance("AES/CBC/PKCS7PADDING"); 

以下運行我的加密異常拋出碼。

No Such Algorithm exists java.security.NoSuchAlgorithmException: Cannot find any provider supporting DES/CBC/PKCS7Padding. 

這是我的源代碼,

String strDataToEncrypt = new String(); 
    String strCipherText = new String(); 
    String strDecryptedText = new String(); 

    try { 
     KeyGenerator keyGen = KeyGenerator.getInstance("AES"); 
     keyGen.init(128); 
     SecretKey secretKey = keyGen.generateKey(); 

     final int AES_KEYLENGTH = 128; 
     byte[] iv = new byte[AES_KEYLENGTH/8];  
     SecureRandom prng = new SecureRandom(); 
     prng.nextBytes(iv); 

     Cipher aesCipherForEncryption = Cipher.getInstance("AES/CBC/PKCS7PADDING"); 

     aesCipherForEncryption.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(iv)); 

     strDataToEncrypt = "Hello World of Encryption using AES "; 
     byte[] byteDataToEncrypt = strDataToEncrypt.getBytes(); 
     byte[] byteCipherText = aesCipherForEncryption.doFinal(byteDataToEncrypt); 

     strCipherText = new BASE64Encoder().encode(byteCipherText); 
     System.out.println("Cipher Text generated using AES is " + strCipherText); 


     Cipher aesCipherForDecryption = Cipher.getInstance("AES/CBC/PKCS7PADDING"); 

     aesCipherForDecryption.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv)); 
     byte[] byteDecryptedText = aesCipherForDecryption.doFinal(byteCipherText); 
     strDecryptedText = new String(byteDecryptedText); 

     System.out.println(" Decrypted Text message is " + strDecryptedText); 

    } catch (NoSuchAlgorithmException noSuchAlgo) { 
     System.out.println(" No Such Algorithm exists " + noSuchAlgo); 
    } catch (NoSuchPaddingException noSuchPad) { 
     System.out.println(" No Such Padding exists " + noSuchPad); 
    } catch (InvalidKeyException invalidKey) { 
     System.out.println(" Invalid Key " + invalidKey); 
    } catch (BadPaddingException badPadding) { 
     System.out.println(" Bad Padding " + badPadding); 
    } catch (IllegalBlockSizeException illegalBlockSize) { 
     System.out.println(" Illegal Block Size " + illegalBlockSize); 
    } catch (InvalidAlgorithmParameterException invalidParam) { 
     System.out.println(" Invalid Parameter " + invalidParam); 
    } 

但是,這正與PKCS5與相同參數Cipher

有什麼想法?

+1

[加密文本AES/CBC/PKCS7Padding](可能的重複http://stackoverflow.com/questions/29232705/encrypt-text-to-aes- CBC-pkcs7padding) – 1615903

回答

1

documentation,提到的填充PKCS7不支持。

你可以參考這個answer瞭解更多信息