I am trying to encrypt a string in IOS and then send it to a TCP server. The code version and iOS version of Python are shown below. Watch the releases of both versions. They look very similar, but the lengths are different, and I do not know the reason. Can anyone verify this, what could be the reason?
Please note that PADDING in the Python script should be discarded, as I already gave the text length 16.
PYTHON Code:
from Crypto.Cipher import AES
import base64
import os
BLOCK_SIZE = 16
PADDING = '{'
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))
DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)
secret = "1234567890123456"
cipher = AES.new(secret)
encoded = EncodeAES(cipher, 'password12345678')
print 'Encrypted string:', encoded
decoded = DecodeAES(cipher, encoded)
print 'Decrypted string:', decoded
CONCLUSION:
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;
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 );
CONCLUSION:
AES data: password12345678
57AayWF4jKYx7KzGkwudIKNlwA + HErrmiy1Z0szzZds =
source
share