2012-11-21 42 views
2

我正在使用「AES/CBC/PKCS5」爲Android和Blackberry寫入加密代碼,如下所示。 機器人:AES加密問題

 byte[] encoded = {(byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32, (byte) 0x56, (byte) 0x35, (byte) 0xE3, (byte) 0x03, 
       (byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32, (byte) 0x56, (byte) 0x35, (byte) 0xE3, (byte) 0x03}; 
     SecretKeySpec secretKeySpec = new SecretKeySpec(encoded, "AES"); 

     Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");//AES/CBC/PKCS5Padding 
     System.out.println("swapnil:"+c.getAlgorithm()+" BlockSize:"+c.getBlockSize()); 
      c.init(Cipher.ENCRYPT_MODE, secretKeySpec); 
      byte[] input = "Hello".getBytes(); 
      byte[] output = c.doFinal(input); 
      System.out.println("Swapnil: " + new String(output)); 

對於黑莓:

 byte[] key1 = {(byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32, (byte) 0x56, (byte) 0x35, (byte) 0xE3, (byte) 0x03, 
      (byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32, (byte) 0x56, (byte) 0x35, (byte) 0xE3, (byte) 0x03}; 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 

    try { 
     InitializationVector iv = new InitializationVector(IV_BYTE); 
     //SymmetricKey key = new SymmetricKeyFactory("AES_256", key1, 0, key1.length); 
     AESKey aesKey = new AESKey(key1); 
     EncryptorOutputStream os = EncryptorFactory.getEncryptorOutputStream(aesKey, baos, "AES/CBC/PKCS5", iv);/// 
     System.out.println("Swapnil"+os.getAlgorithm()); 
     os.write("Hello".getBytes()); 
     os.close(); 
     byte[] encryptedData = baos.toByteArray(); 
     String string = new String(encryptedData); 
     LabelField lblField = new LabelField(string); 
     add(lblField); 

    } catch (NoSuchAlgorithmException e) { 
     e.printStackTrace(); 
    } catch (CryptoTokenException e) { 
     e.printStackTrace(); 
    } catch (CryptoUnsupportedOperationException e) { 
     e.printStackTrace(); 
    } catch (CryptoException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

我的問題是在Android中當我運行該代碼,即使具有相同的密鑰後,它生成不同的加密數據的每個I運行該代碼的時間。在黑莓手機中,每當我運行代碼時,它都會生成相同的加密數據。我認爲Android應該也是這樣,但不知何故它不起作用。

我該如何解決這個問題?

+0

你試試下面的答案嗎? – Nikhil

回答

1

其實我沒有在doFinal()之前調用cipher.processBytes()。所以我想要加密長度爲20個字節的數據,它只用來作爲加密數據返回16個字節。

這是最後的代碼:

private byte[] cipherData(PaddedBufferedBlockCipher cipher, byte[] data) throws Exception { 
     int minSize = cipher.getOutputSize(data.length); 
     byte[] outBuf = new byte[minSize]; 
     int length1 = cipher.processBytes(data, 0, data.length, outBuf, 0); 
     int length2 = cipher.doFinal(outBuf, length1); 
     int actualLength = length1 + length2; 
     byte[] result = new byte[actualLength]; 
     System.arraycopy(outBuf, 0, result, 0, result.length); 
     return result; 
    } 
3

我在開發Android的AES加密,它對我來說工作正常。

public class AESEncrtptor { 
    String strKey = "123456789"; 
    byte[] byteKey; 
    byte[] byteVector = new byte[] { 59, 12, (byte) 129, 77, 39, 119, 82, 6, 
      23, 1, 55, 24, 12, (byte) 154, (byte) 224, 14 }; 

    public AESEncrtptor() { 
     try { 
      byteKey = strKey.getBytes("UTF-8"); 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } 
    } 

    public enum EnumCipherMode { 
     CBC, ECB 
    } 

    public String Encrypt(String strdata, EnumCipherMode CipherMode) { 
     try { 
      String strAlgorithm = "AES/CBC/PKCS7Padding"; 
      switch (CipherMode) { 
      case CBC: 
       strAlgorithm = "AES/CBC/PKCS7Padding"; 
       break; 
      case ECB: 
       strAlgorithm = "AES/ECB/PKCS7Padding"; 
       break; 
      } 
      Cipher c = Cipher.getInstance(strAlgorithm); 
      SecretKeySpec keySpec = new SecretKeySpec(byteKey, strAlgorithm); 
      switch (CipherMode) { 
      case CBC: 
       c.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(
         byteVector)); 
       break; 
      case ECB: 
       c.init(Cipher.ENCRYPT_MODE, keySpec); 
       break; 
      } 
      byte[] data = strdata.getBytes(); 

      byte[] encrypted = c.doFinal(data); 
      return Base64.encodeToString(encrypted, Base64.DEFAULT); 
     } catch (Exception e) { 
      return ""; 
     } 
    } 

    public String Decrypt(String strdata, EnumCipherMode CipherMode) { 
     try { 
      String strAlgorithm = "AES/CBC/PKCS7Padding"; 
      switch (CipherMode) { 
      case CBC: 
       strAlgorithm = "AES/CBC/PKCS7Padding"; 
       break; 
      case ECB: 
       strAlgorithm = "AES/ECB/PKCS7Padding"; 
       break; 
      } 
      Cipher d_c = Cipher.getInstance(strAlgorithm); 
      SecretKeySpec d_keySpec = new SecretKeySpec(byteKey, strAlgorithm); 
      switch (CipherMode) { 
      case CBC: 
       d_c.init(Cipher.DECRYPT_MODE, d_keySpec, new IvParameterSpec(
         byteVector)); 
       break; 
      case ECB: 
       d_c.init(Cipher.DECRYPT_MODE, d_keySpec); 
       break; 
      } 

      byte[] decrypted = d_c.doFinal(Base64.decode(strdata, 
        Base64.DEFAULT)); 
      String decryptedStr = ""; 
      for (int i = 0; i < decrypted.length; i++) 
       decryptedStr += (char) decrypted[i]; 
      return decryptedStr; 
     } catch (Exception e) { 
      return ""; 
     } 
    } 
}