2013-04-11 51 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