0

I am new to encryption

The problem:

I am given a set of encrypted strings and I need to decrypt them to show to the mobile client user. For android, it decrypt fine and i am using the following method "decrypt". For iOS, I am having a lot of trouble translating this java method to Objective C. I have attempted using NSData+CommonCrypto, RNCryptor. Both of them will return some decrypted data, However when converted the decrypted data to string, it will always be nil.

Goal:

Translate the java decrypt method to Objective C(Decrypt a string in Objective C using a secrete key)

Any suggestions, comments, opinions, pseudo code would be greatly appreciated. thanks

Android Decrypt Method

public static String decrypt(String message){
    try {
        Cipher c = Cipher.getInstance("AES");
        SecretKeySpec key = new SecretKeySpec(secrKey.getBytes(), "AES");
        c.init(Cipher.DECRYPT_MODE, key);
        byte[] decordedValue = Base64.decode(message.getBytes(), Base64.DEFAULT);
        byte[] decValue = c.doFinal(decordedValue);
        String decryptedValue = new String(decValue);
        String decoded = new String(Base64.decode(decryptedValue, Base64.DEFAULT));
        return decoded;
    }catch(Exception e){
        return null;
    }
}
xiaowoo
  • 2,248
  • 7
  • 34
  • 45
  • You need to supply example input and output along with mode, kind of padding if any and iv if CBC mode and any. Add hex dups of these inputs to the question. Also provide links to the encryption libraries if some of this is unknown. Many encryption libraries have internal defaults and many also do not provide information on these. – zaph Jun 06 '15 at 17:24
  • 1
    The Java `getInstance` method should provide all the necessary information and not rely on defaults. such as: "AES/CBC/PKCS5Padding (128)", "AES/ECB/NoPadding (128)" or some other combination. With the "AES" spec I would guess: ECB mode (really bad choice), PKCS5Padding, and a key length based on the supplied key null padded as needed. See [Class Cipher](https://docs.oracle.com/javase/7/docs/api/javax/crypto/Cipher.html) docs. – zaph Jun 06 '15 at 17:35
  • Update your question with your Objective-C code so people can point out your issue. – rmaddy Jun 06 '15 at 19:56
  • @zaph your second comment solved my problem. "AES/CBC/PKCS5Padding (128)" . Thanks ^^. – xiaowoo Jun 07 '15 at 01:03

1 Answers1

2

The Java getInstance method should provide all the necessary information and not rely on defaults. Such as: "AES/CBC/PKCS5Padding (128)", "AES/ECB/NoPadding (128)" or some other combination.

With the "AES" spec I would guess: ECB mode (really bad choice), PKCS5Padding, and a key length based on the supplied key null padded as needed.

See Class Cipher docs.

zaph
  • 111,848
  • 21
  • 189
  • 228