Python中的AES加密与iOS不同
我正在尝试在IOS中加密字符串,然后将其发送到TCP服务器。Python版本的代码和iOS版本如下所示。请参阅两个版本的输出。它们看起来很相似,但长度不同,我不知道原因。有人能检查一下吗?原因是什么 请注意,Python脚本中的填充应该被丢弃,因为我已经给出了16的文本长度 PYTHON代码:Python中的AES加密与iOS不同,python,ios,aes,Python,Ios,Aes,我正在尝试在IOS中加密字符串,然后将其发送到TCP服务器。Python版本的代码和iOS版本如下所示。请参阅两个版本的输出。它们看起来很相似,但长度不同,我不知道原因。有人能检查一下吗?原因是什么 请注意,Python脚本中的填充应该被丢弃,因为我已经给出了16的文本长度 PYTHON代码: #!/usr/bin/env python from Crypto.Cipher import AES import base64 import os
#!/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
输出:
加密字符串:57aaywf4jkyx7kzgkwudibzusn1loc0c4c5yf3xei8=
解密字符串:密码12345678
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 );
输出:
AES数据:密码1234567857AayWF4jKYx7KzGkwudIKNlwA+herrmiy1z0szzds=好的,在这里。谢谢萨诺尔德提供的线索:) Python输出: 加密字符串:C9pEG6g8ge76xt2q9XLbpw== 解密字符串:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *在iOS中将AES选项更改为KCCOPIONECBMODE* 现在的输出是: iOS输出: AES数据:aaaaaaaaaa
C9pEG6g8ge76xt2q9XLbpw==如果正在使用,则只能忽略填充。我希望您没有使用ECB模式。我没有用Python设置模式,我相信它使用的是默认值。@sarnold ECB模式也需要填充,IV不应该设置为ECB模式或设置为所有零(如果平台需要IV),但填充肯定是必需的。或者这是Python/iOS平台特有的?@owlstead:在16字节输入数据的特定情况下,您可以使用ECB模式,而无需填充。当然,您不应该使用ECB模式,但您当然可以在没有任何填充的情况下加密16字节的数据。@sarnold但这在ECB模式或CBC模式中没有区别,如果您事先知道长度,并且您有blocksize*N字节,那么您就不必填充。由于ECB模式只适用于随机数据(主要是密钥),填充可能不是默认值,因此我的问题是关于平台/运行时的。干得好:)尽管我认为你不应该选择“社区维基”——回答你自己的问题没有错,这个答案肯定比很多人都好。你应该从中得到名声Wiki状态已删除。当您希望确保更广泛的用户群可以编辑某些内容时,社区wiki非常有用,例如对一个可能需要随时间变化的非常常见问题的规范答案。回答你自己的问题没有错,事实上,这是鼓励的。
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
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionECBMode,keyPtr, CCKeySizeAES128, NULL,[self bytes], dataLength, buffer, bufferSize, &numBytesEncrypted);