2017-02-28 16 views
-1

我正在對我的文件實施加密/解密。代碼如下。我找不到問題。 我錯過了什麼嗎?我需要實現128位AES加密。 有什麼不對嗎? 它結束了錯誤Android javax.crypto.BadPaddingException

"javax.crypto.BadPaddingException: error:1e06b065:Cipher functions:EVP_DecryptFinal_ex:BAD_DECRYPT" 

請幫我。

private static byte[] encodeFile(byte[] yourKey, byte[] fileData) 
     throws Exception { 
    byte[] encrypted = null; 
    SecretKeySpec skeySpec = new SecretKeySpec(yourKey, 0, yourKey.length, "AES"); 
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7PADDING"); 
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec); 
    encrypted = cipher.doFinal(fileData); 
    return encrypted; 
} 

private static byte[] generateKey() throws NoSuchAlgorithmException { 
    byte[] keyStart = "This is my key".getBytes(); 
    String id = "dummypass"; 
    int iterationCount = 1000; 
    int saltLength = 32; 
    int keyLength = 128; 
    SecureRandom random = new SecureRandom(); 
    byte[] salt = Arrays.copyOf(keyStart,saltLength); 
    random.nextBytes(salt); 
    KeySpec keySpec = new PBEKeySpec(id.toCharArray(), salt, 
      iterationCount, keyLength); 
    SecretKeyFactory keyFactory = SecretKeyFactory 
      .getInstance("PBKDF2WithHmacSHA1"); 
    byte[] keyBytes = new byte[0]; 
    try { 
     keyBytes = keyFactory.generateSecret(keySpec).getEncoded(); 
    } catch (InvalidKeySpecException e) { 
     e.printStackTrace(); 
    } 
    SecretKey key = new SecretKeySpec(keyBytes, "AES"); 
    return key.getEncoded(); 
} 

private static byte[] decodeFile(byte[] yourKey, byte[] encryptedData) 
     throws Exception { 
    SecretKeySpec skeySpec = new SecretKeySpec(yourKey, 0, yourKey.length, 
      "AES"); 
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7PADDING"); 
    cipher.init(Cipher.DECRYPT_MODE, skeySpec); 
    byte[] decrypted = cipher.doFinal(encryptedData); 
    return decrypted; 
} 

public static void Encrypt(byte[] bytesToEncrypt, File target) { 
    try { 
     BufferedOutputStream bos = new BufferedOutputStream(
       new FileOutputStream(target)); 
     byte[] key = generateKey(); 
     byte[] encryptedBytes = encodeFile(key, bytesToEncrypt); 
     bos.write(encryptedBytes); 
     bos.flush(); 
     bos.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
public static byte[] DecryptFile(byte[] bytesToDecrypt) { 
    byte[] decodedData = new byte[0]; 
    try { 
     byte[] key = generateKey(); 
     decodedData = decodeFile(key, bytesToDecrypt); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return decodedData; 
} 
+1

不要使用ECB模式,它不安全,請參閱[ECB模式](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Electronic_Codebook_.28ECB.29),向下滾動到企鵝。 取而代之的是將CBC模式與隨機IV一起使用,只是將加密的數據與IV一起用於解密,它並不需要保密。不要在解密時報告任何傳遞錯誤。 – zaph

回答

3

驗證key在兩個EncryptDecryptFile相同。

既然撥打電話generateKeygenerateKey撥打電話SecureRandomnextBytes鍵將會不同。

您需要保存解密過程中使用的加密密鑰。

+0

您能否提一下爲加密和解密生成理想密鑰的效果方式? –

+0

加密時使用'generateKey'創建一個密鑰,保存該密鑰進行解密並將其傳遞給解密函數。注意:AES是對稱加密,並使用相同的密鑰進行加密和解密。 – zaph

+0

如果我使用CBC而不是EBC,我需要提及一個隨機IV。對?那麼我需要保存IV還是用於解密? –

相關問題