0

To generate AES key in java i had used as fallow

public byte[] GenerateAESKey() {
        // Generate a new AES key
        SecretKey key = null;
        try {
            KeyGenerator keygen = KeyGenerator.getInstance("AES");
            keygen.init(128);
            key = keygen.generateKey();
            return key.getEncoded();
        } catch (NoSuchAlgorithmException e) {
            return null;
        }
    }

In iOS I tried it in this way using iOS-Crypto-API.

 id<SecretKey> key=[[[AESKeyGenerator alloc] init] generate:128 onError:&error];
  NSLog(@"Key: %@  ",key.key);

When I print both keys looks different, Is that above generated iOS Key is correct or not? Help me to solve this out.

Kampai
  • 22,848
  • 21
  • 95
  • 95
  • Check this: http://stackoverflow.com/questions/13946724/objective-c-aes-encrypt-equal-to-java-version – Mrunal Dec 18 '14 at 13:12

2 Answers2

3

AES keys are essentially bit strings. So when you generate a new AES key, it will be created using some randomness of the system or the library. Even if you generate two keys in the same library, they will be different as collisions for 128-bit keys are really unlikely to happen.

For encryption and decryption you need the same key at both ends, so you need to transport it in some way. Depending on your system, you could do this at system setup (key embedded in the source code or in some way static) or using asymmetric encryption to send the key from one machine to the next (possibly secured with Diffie-Hellman Key Exchange).

You will have to encode the key in some way that is acceptable by the library. Popular encodings are Base 64 and Hex. You may need to convert between encodings.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • Thanks for reply @Artjom,Help me , In java i didn't use any key, So need to pass the some private key like that ?, if yes, In java i didn't pass any key,How it's taken in java? – user3679223 Dec 18 '14 at 13:20
  • 1
    You generate the key only once, either in Java or on iOS, but not both. That wouldn't make any sense, since the keys are different. Please read about [Public-key cryptography](https://en.wikipedia.org/wiki/Public-key_cryptography) and if don't understand it [ask a new question](http://stackoverflow.com/questions/ask) with a proper description of your system and what you want to achieve. Don't edit this question, because it would make the given answers obsolete. – Artjom B. Dec 18 '14 at 13:40
1

Symmetric keys are, basically, byte arrays which are obtained from random source. The length of the keys depends on the cipher algorithm that is going to use such keys.

When you generate a secret key (i.e., a symmetric key) you get a random byte array. If you generate it again, you will get a different one.

Therefore, it is totally normal to get two different keys from two different key generations.

Marc Manzano
  • 158
  • 7
  • That i can't tell. But the answer to your question is: don't worry if the generated keys are different in the two different snippets, it is the _expected_ behavior. – Marc Manzano Dec 22 '14 at 08:19