0

I have java code to encrypt a password,

public static String encryptToString(String content,String password) throws IOException {
    return parseByte2HexStr(encrypt(content, password));
}
private static byte[] encrypt(String content, String password) {
    try {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
        secureRandom.setSeed(password.getBytes());
        kgen.init(128, secureRandom);
        SecretKey secretKey = kgen.generateKey();
        byte[] enCodeFormat = secretKey.getEncoded();
        SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        byte[] byteContent = content.getBytes("utf-8");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] result = cipher.doFinal(byteContent);
        return result;
    } catch (Exception e) {
        log.error(e.getMessage(),e);
    }
    return null;
}
public static String parseByte2HexStr(byte buf[]) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < buf.length; i++) {
        String hex = Integer.toHexString(buf[i] & 0xFF);
        if (hex.length() == 1) {
            hex = '0' + hex;
        }
        sb.append(hex.toUpperCase());
    }
    return sb.toString();
}

Now I need to encrypt/decrypt it with objective-c, I do a lot of search and none method would generate the same encrypted output.
What's the objective-c version code equal to java code?

test case: encryptToString("test","password") => DA180930496EC69BFEBA923B7311037A

xfx
  • 1,918
  • 1
  • 19
  • 25

1 Answers1

1

I believe the answers to this question are what you are looking for: Any cocoa source code for AES encryption decryption?

I adapted a function somebody posted as answer: https://gist.github.com/4335132

To use:

NSString *content = @"test";
NSData *dataToEncrypt = [content dataUsingEncoding:NSUTF8StringEncoding];
NSData *data = [dataToEncrypt AES128EncryptWithKey:@"password"];
NSString *hex = [data hexString];

It's not exactly the same because your Java code does not use the password itself for encrypting but uses it to seed a random number generator. But I think that this should still work with what you are trying to do.

Community
  • 1
  • 1
Cocoanetics
  • 8,171
  • 2
  • 30
  • 57
  • Yes, it it shows how the AES function in CommonCrypto are working. You can easily convert your Java code to Objective-C with this knowledge. – Cocoanetics Dec 19 '12 at 07:09
  • Yes, this works if I spec the same key bytes with java version, the problem now is how to generate/calculate the right key. – xfx Dec 19 '12 at 08:05
  • If I understand this correctly you cannot ever get exactly the same result because the key are just random bytes. There is a similar method in the Security.framework. Try Rob Napiers AESKeyForPassword function he documented on his blog: http://robnapier.net/blog/aes-commoncrypto-564 – Cocoanetics Dec 19 '12 at 08:08
  • I need the same way with java version because I need to decrypt the password generated by java code on iOS. I just hard code the key bytes and it works. – xfx Dec 19 '12 at 08:11
  • @xfx Can you please share your code of objective c I am getting "D870F7E3B4B00465AB95EB58DB5278DF" using above objective c code please help me what I need to change ?? Thank you. – Nitesh Meshram Jul 01 '13 at 07:40
  • @NiteshMeshram here is the code. https://dpaste.org/mQ0b7/ I comment something for you to understand it. – xfx Jul 01 '13 at 23:43
  • @xfx Your link is not working. Can you share what is it ? Are you able to do this. As per Cocoanetics comment I feel that results will always be different. Did any one got any success in getting similar results from objective C and JAVA ? – DivineDesert Mar 03 '15 at 06:53
  • @DivineDesert the java code, before the line "cipher.init(Cipher.ENCRYPT_MODE, key);", insert System.out.println(parseByte2HexStr(key.getEncoded())); to see the bytes, which is "2470C0C06DEE42FD1618BB99005ADCA2", that's the key for "password". Manually assign the key will work. Here is the code https://gist.github.com/anonymous/bf3edc9dcbf8bd80e1da – xfx Mar 04 '15 at 10:42