Note that there are some explanatory texts on larger screens.

plurals
  1. POAES gets different results in iOS (Obj-C) and Android (Java)
    primarykey
    data
    text
    <p>I'm a complete newbie to this kind of encryption things but I have a Java app and an iOS, and I want them to both be able to ecrypt a text to a same result. I use AES. I found these codes, with a little modification of course, but they return different result</p> <p>iOS Code:</p> <pre><code>- (NSData *)AESEncryptionWithKey:(NSString *)key { unsigned char keyPtr[kCCKeySizeAES128] = { 'T', 'h', 'e', 'B', 'e', 's', 't', 'S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' }; size_t bufferSize = 16; void *buffer = malloc(bufferSize); size_t numBytesEncrypted = 0; const char iv2[16] = { 65, 1, 2, 23, 4, 5, 6, 7, 32, 21, 10, 11, 12, 13, 84, 45 }; CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionECBMode | kCCOptionPKCS7Padding,, keyPtr, kCCKeySizeAES128, iv2, @"kayvan", 6, dataInLength, buffer, bufferSize, &amp;numBytesEncrypted); if (cryptStatus == kCCSuccess) { return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted]; } free(buffer); return nil; } </code></pre> <p>and the Java code is:</p> <pre><code>public static void main(String[] args) throws Exception { String password = "kayvan"; String key = "TheBestSecretKey"; String newPasswordEnc = AESencrp.newEncrypt(password, key); System.out.println("Encrypted Text : " + newPasswordEnc); } </code></pre> <p>and in another java class (<code>AESencrp.class</code>) I have: </p> <pre><code>public static final byte[] IV = { 65, 1, 2, 23, 4, 5, 6, 7, 32, 21, 10, 11, 12, 13, 84, 45 }; public static String newEncrypt(String text, String key) throws Exception { Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); byte[] keyBytes= new byte[16]; byte[] b= key.getBytes("UTF-8"); int len = 16; System.arraycopy(b, 0, keyBytes, 0, len); SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES"); IvParameterSpec ivSpec = new IvParameterSpec(IV); System.out.println(ivSpec); cipher.init(Cipher.ENCRYPT_MODE,keySpec,ivSpec); byte[] results = cipher.doFinal(text.getBytes("UTF-8")); String result = DatatypeConverter.printBase64Binary(results); return result; } </code></pre> <p>The string I wanted to encrypt is <code>kayvan</code> with the key <code>TheBestSecretKey</code>. and the results after Base64 encoding are:</p> <p>for iOS: <code>9wXUiV+ChoLHmF6KraVtDQ==</code></p> <p>for Java: <code>/s5YyKb3tDlUXt7pqA5OFA==</code></p> <p>What should I do now?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COThere's some sort of mismatch between how you're configuring the encryption between the two implementations. As you've no doubt seen, there are a lot of configuration parameters for CCCrypt on iOS. I'm not familiar with Java's Cipher, so you may need to read the documentation for both and make sure you're using the same key size, algorithm, padding... Also, in your Java code above I don't see where you're setting the string you want to encrypt. Is it possible you omitted that step and are encrypting an empty string?
      singulars
    2. COyes, there is a lot of parameters and I'm so confused with them. I have to read them again. But about your final question, the string i want to encrypt is set in main method in java. I've edited the question and added this part.
      singulars
    3. COAgain, I'm not certain about Java Cipher... but it looks like you're using different padding in Objective C. ie: kCCOptionPKCS7Padding vs AES/CBC/PKCS5Padding. (Since you're using a block cipher the algorithm needs to know how to pad the last block, if there isn't enough data to fill the whole block.) However, if I recall correctly PKCS5 and PKCS7 are basically the same algorithm, so I'm not sure that's the problem--you might try changing one or the other implementation to make sure they use the same padding.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload