2017-03-10 158 views
1

我想在我的Android應用程序中進行AES加密/解密。我試着像Android AES加密/解密不正確的結果

無論我怎樣的方式使用解密的部分是從原始數據不同。我一直有這樣的結果:

03-09 21:58:33.457 30329-30329/org.androidapp.test E/ERROR: BEFORE: sDuKOoRteaEUFtA3P0SllSTCpgKJN75FuyPLxdp/ctM= 

03-09 21:58:33.459 30329-30329/org.androidapp.test E/ERROR: AFTER: PBSqM3jHZhemw48wd44pKg== 

的什麼,我現在在做一個簡單的例子:

private static byte[] seedValue = { 
     0x2d, 0x2a, 0x2d, 0x42, 0x55, 0x49, 0x4c, 0x44, 0x41, 0x43, 0x4f, 0x44, 0x45, 0x2d, 0x2a, 0x2d 
}; 
private static String ALGORITHM = "AES"; 
private static SecretKeySpec secretKey = new SecretKeySpec(seedValue, "AES"); 


public static String encrypt(String data) throws Exception { 
    try { 
     Cipher cipher = Cipher.getInstance("AES"); 
     cipher.init(Cipher.ENCRYPT_MODE, secretKey); 
     byte[] cipherText = cipher.doFinal(data.getBytes("UTF8")); 
     String encryptedString = new String(Base64.encode(cipherText ,Base64.DEFAULT)); 
     return encryptedString; 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

public static String decrypt(String data) throws Exception { 
    try { 
     Cipher cipher = Cipher.getInstance("AES"); 
     cipher.init(Cipher.DECRYPT_MODE, secretKey); 
     byte[] cipherText = Base64.decode(data.getBytes("UTF8"), Base64.DEFAULT); 
     String decryptedString = new String(cipher.doFinal(cipherText),"UTF-8"); 
     return decryptedString; 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

使用這些方法(使用傑克遜的讀取和寫入的對象)的樣品:

public static void writeSecurityInfo(String fileName, POJO pojo){ 
     try{ 
      FileUtil.verifyFile(fileName); 
      ObjectMapper mapper = new ObjectMapper(); 

      File file = new File(Environment.getExternalStorageDirectory(), fileName); 

      try { 
       pojo.setName(CryptUtils.encrypt(pojo.getName())); 
       pojo.setAddress(CryptUtils.encrypt(pojo.getAddress())); 
      } 
      catch(Exception e){ 
       e.printStackTrace(); 
      } 

      ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter()); 
      writer.writeValue(file, securityPOJO); 
     } 
     catch(IOException e){ 
      e.printStackTrace(); 
     } 
    } 

而對於閱讀:

public static POJO readSecurityInfo(String fileName){ 
     try { 
      File file = new File(Environment.getExternalStorageDirectory(), fileName); 

      ObjectMapper mapper = new ObjectMapper(); 

      InputStream inputStream = new FileInputStream(file); 

      POJO pojo = mapper.readValue(inputStream, POJO.class); 
      Log.e("ERROR", "BEFORE: "+ pojo.getName()); 
      securityPOJO.setName(CryptUtils.decrypt(pojo.getName())); 
      Log.e("ERROR", "AFTER: "+ pojo.getName()); 
      pojo.setAddress(CryptUtils.decrypt(pojo.getAddress())); 

      return pojo; 

     } 
     catch(Exception e){ 
      e.printStackTrace(); 
     } 
     return null; 
    } 

沒有成功。 使用AES時我做錯了什麼?

+0

請[編輯]你的問題包括[MCVE](添加在您調用'encrypt'和'decrypt'的代碼)。 –

+0

我的猜測是你缺少一些填充。看看這個: http://stackoverflow.com/questions/15554296/simple-java-aes-encrypt-decrypt-example –

+0

@CountTCyber​​SecurityLtd我試着你提供的解決方案,並改變了我的代碼插入密碼AES/CBC/PKCS5PADDING。非常相同的結果。 – learner

回答

0

我的加密和解密方法沒有看到任何錯誤。我用簡單的文字進行了測試:

try { 
     String test = encrypt("My name is Nam"); 
     Log.e("TEST", "xxxx encrypted: "+ test); 
     Log.e("TEST", "xxxx decrypted: "+ decrypt(test)); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

,結果是正確的:

十一月3日至10日:12:38.987 31251-31251 /? E/TEST:xxxx加密: bR80WEK9pa3eicMjCZCtQg == 03-10 11:12:38.987 31251-31251 /? E/TEST:XXXX 解密:我的名字是南

所以,也許你寫的日誌是不正確的:

Log.e("ERROR", "AFTER: "+ pojo.getName()); 

應該是:

Log.e("ERROR", "AFTER: "+ securityPOJO.getName()); 

Log.e("ERROR", "AFTER: "+ CryptUtils.decrypt(pojo.getName())); 

注:我寫了一個關於加密的樣本g如果您想檢查,請使用更安全的in github解密。

+0

一些沒有睡覺的夜晚會造成這種錯誤。對我感到羞恥。 – learner

0

有幾個參數,AES牢記:

  • 密鑰長度(128,256)
  • 核心價值
  • 塊鏈接模式(ECB,CBC,...)
  • CBC有一個IV

我沒有在你的代碼中看到任何設置IV的東西。這是另一個Java AES示例文章。

Simple Java AES encrypt/decrypt example