2013-04-11 50 views
5

我在Objective C中爲我的iPhone應用程序使用加密類,但是我正努力從我的android應用程序中獲得在JAVA中工作的相同功能。我的加密代碼如下:Java字符串加密

NSString * _secret = @"password"; 
NSString * _key = @"1428324560542678"; 

StringEncryption *crypto = [[StringEncryption alloc] init]; 
NSData *_secretData = [_secret dataUsingEncoding:NSUTF8StringEncoding]; 
CCOptions padding = kCCOptionPKCS7Padding; 
NSData *encryptedData = [crypto encrypt:_secretData key:[_key dataUsingEncoding:NSUTF8StringEncoding] padding:&padding]; 

我試圖複製它在Java,但我得到一個不同的字符串時我編碼相同的數據。所以我做錯了什麼,但我無法弄清楚。這是我的JAVA代碼:

byte[] key = "1428324560542678".getBytes(); 

Cipher c = null; 
      try { 
       c = Cipher.getInstance("AES/ECB/PKCS7Padding"); 
      } catch (NoSuchAlgorithmException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (NoSuchPaddingException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

SecretKeySpec k = new SecretKeySpec(key, "AES"); 
      try { 
       c.init(Cipher.ENCRYPT_MODE, k); 
      } catch (InvalidKeyException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

    try { 
     EditText tv1passwordText = (EditText) findViewById(R.id.password); 
     String password = URLEncoder.encode(tv1passwordText.getText().toString(), "UTF-8"); 

      byte[] encryptedData = c.doFinal(password.getBytes()); 

任何人都可以看到我要去哪裏錯了嗎?

基於下面我說的getBytes但製作琴絃的意見仍然是不同的:

byte[] key = null; 
      try { 
       key = "1428324560542678".getBytes("UTF-8"); 
      } catch (UnsupportedEncodingException e2) { 
       // TODO Auto-generated catch block 
       e2.printStackTrace(); 
      } 

      Cipher c = null; 
      try { 
       c = Cipher.getInstance("AES/ECB/PKCS7Padding"); 
      } catch (NoSuchAlgorithmException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (NoSuchPaddingException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      SecretKeySpec k = new SecretKeySpec(key, "AES"); 
      try { 
       c.init(Cipher.ENCRYPT_MODE, k); 
      } catch (InvalidKeyException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      try { 
       EditText tv1passwordText = (EditText) findViewById(R.id.password); 

       byte[] password = tv1passwordText.getText().toString().getBytes("UTF-8"); 

       byte[] encryptedData = c.doFinal(password); 
+0

您需要指定字符'的getBytes()設置'如果你想要的字符串匹配。 – 2013-04-11 15:26:45

回答

7

這裏是加密和解密的例子:

public static SecretKey generateKey() throws NoSuchAlgorithmException, InvalidKeySpecException { 
    return secret = new SecretKeySpec(password.getBytes(), "AES"); 
} 

public static byte[] encryptMsg(String message, SecretKey secret) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidParameterSpecException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { 
/* Encrypt the message. */ 
    Cipher cipher = null; 
    cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); 
    cipher.init(Cipher.ENCRYPT_MODE, secret); 
    byte[] cipherText = cipher.doFinal(message.getBytes("UTF-8")); 
    return cipherText; 
} 

public static String decryptMsg(byte[] cipherText, SecretKey secret) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidParameterSpecException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException { 

    /* Decrypt the message, given derived encContentValues and initialization vector. */ 
    Cipher cipher = null; 
    cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); 
    cipher.init(Cipher.DECRYPT_MODE, secret); 
    String decryptString = new String(cipher.doFinal(cipherText), "UTF-8"); 
    return decryptString; 
} 

要加密:

SecretKey secret = EncUtil.generateKey(); 
    EncUtil.encryptMsg(<String to Encrypt>, secret)) 

解密

EncUtil.decryptMsg(<byte[]>, secret)) 
+0

Thnak謝謝謝謝!完善。 – user1661369 2013-04-11 18:39:45

+0

@ wangyif2 EncUtil是什麼類? – RyPope 2013-12-19 04:03:29

+0

@RyPope,它將是w/e類的encryptMsg方法所在(完全取決於你)。 – wangyif2 2013-12-19 18:32:42

0

而不是使用ECB的,你應該儘可能使用CBC或點擊率。 ECB is insecure

它看起來像你的Objective-C代碼使用UTF-8編碼,但你沒有在你的Java代碼中指定它。使用getBytes("UTF-8")

+0

感謝您的幫助。我沒有使用getBytes,所以我改變我的代碼使用getBytes,但仍然沒有運氣。 – user1661369 2013-04-11 16:20:32

0

我注意到的一件事在過去引起了問題,那就是被加密的iOS字符串實際上是"Hello World\0",例如,在最後加上一個額外的空字符串。所以請嘗試在Java中的字符串末尾添加\0,並查看它是否會產生相同的結果。

此外,java上的URLEncoder步驟可能會引入額外的控制字符和iOS端不存在的其他內容。這可能是值得的文本後,這與文本進入iOS的加密步驟比較,以確保它們是完全一樣的

+0

刪除了URL編碼器並按照建議使用了getBytes方法,但仍然沒有喜悅。 – user1661369 2013-04-11 16:28:39

+0

是否嘗試在密碼字段中獲得的字符串末尾添加'\ 0'? – 2013-04-11 16:33:27

+0

沒有快樂,我試圖在字符串的末尾添加\ 0,但編碼後的字符串仍然不同 – user1661369 2013-04-11 16:38:37