Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AES Encryption in Python is different form iOS

Tags:

python

ios

aes

I am trying to encrypt a string in IOS and then send it to a TCP server. Python version of code and iOS versions are shown below. Please see outputs of the both versions. They look quite similar but the lengths are different and I do not know the reason. Can anybody check it , what could be the reason?

Please note that PADDING in Python script should be discarded , as I gave a text length of 16 already.

PYTHON Code:

     #!/usr/bin/env python

     from Crypto.Cipher import AES
     import base64
     import os

     # the block size for the cipher object; must be 16, 24, or 32 for AES
     BLOCK_SIZE = 16

     PADDING = '{'

     # one-liner to sufficiently pad the text to be encrypted
     pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING

     # one-liners to encrypt/encode and decrypt/decode a string
     # encrypt with AES, encode with base64
     EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))
     DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)


     secret = "1234567890123456" 

     # create a cipher object using the random secret
     cipher = AES.new(secret)

     encoded = EncodeAES(cipher, 'password12345678')
     print 'Encrypted string:', encoded

     decoded = DecodeAES(cipher, encoded)
     print 'Decrypted string:', decoded

OUTPUT:

Encrypted string: 57AayWF4jKYx7KzGkwudIBZUsn1ULOC0C4c5YF3xeI8=

Decrypted string: password12345678

NSString *forKey=@"1234567890123456";
NSString *mystr =@"password12345678";
const char *utfString = [mystr UTF8String];
NSData *aData=[NSData dataWithBytes: utfString length: strlen(utfString)];
aData=[mystr dataUsingEncoding:NSUTF8StringEncoding];
NSData *data;//=[aData AES128EncryptWithKey:forKey];
data=[aData AES128EncryptWithKey:forKey];

NSString *base64 = [data base64EncodedString];

aData=[data AES128DecryptWithKey:forKey];
mystr=[[NSString alloc] initWithData:aData encoding:NSUTF8StringEncoding];

NSLog(@"AES data : %@  \n %@",mystr,base64 );

OUTPUT:

AES data : password12345678
57AayWF4jKYx7KzGkwudIKNlwA+HErrmiy1Z0szzZds=

like image 908
Bora Avatar asked Jan 22 '12 23:01

Bora


People also ask

How do you decrypt AES encrypted files in Python?

Decrypting File Using AES First, open the encrypted file and read the file size and the initialization vector. The IV is required for creating the cipher. Next create the cipher using the key and the IV. We assume the key has been communicated using some other secure channel.

Is AES public or private key?

AES uses symmetric key encryption, which involves the use of only one secret key to cipher and decipher information. The Advanced Encryption Standard (AES) is the first and only publicly accessible cipher approved by the US National Security Agency (NSA) for protecting top secret information.


1 Answers

OK , here it is. Thanks sarnold for the clue :)

from Crypto.Cipher import AES
import base64
import os

    # the block size for the cipher object; must be 16, 24, or 32 for AES
    BLOCK_SIZE = 16
    mode = AES.MODE_CBC
    secret = "1234567890123456" #os.urandom(BLOCK_SIZE)

    # create a cipher object using the random secret
    cipher = AES.new(secret,mode)

    # encode a string
    #tx=cipher.encrypt('1234567890123456')
    #print base64.b64encode(tx)

    myData='aaaaaaaaaaaaaaaa'
    #encoded = EncodeAES(cipher, myData)
    encoded = cipher.encrypt(myData)
    print 'Encrypted string:', base64.b64encode(encoded)
    mode = AES.MODE_ECB
    cipher=AES.new(secret,mode)
    decoded = cipher.decrypt(encoded)
    print 'Decrypted string:', decoded

Python OUTPUT:

Encrypted string: C9pEG6g8ge76xt2q9XLbpw==

Decrypted string: aaaaaaaaaaaaaaaa

*Changed AES CCOptions to kCCOptionECBMode in iOS. *

CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionECBMode,keyPtr, CCKeySizeAES128, NULL,[self bytes], dataLength,  buffer, bufferSize,  &numBytesEncrypted);

And now output is:

iOS Output:

AES data : aaaaaaaaaaaaaaaa
C9pEG6g8ge76xt2q9XLbpw==

like image 149
Bora Avatar answered Sep 23 '22 09:09

Bora