Python 中的 AES 加密与 iOS 中的不同
我正在尝试在 IOS 中加密一个字符串,然后将其发送到 TCP 服务器。 Python版本的代码和iOS版本如下所示。请查看两个版本的输出。它们看起来很相似,但长度不同,我不知道原因。谁能检查一下,可能是什么原因?
请注意,Python 脚本中的 PADDING 应该被丢弃,因为我已经给出了 16 的文本长度。
PYTHON 代码:
#!/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
输出:
加密字符串:57AayWF4jKYx7KzGkwudIBZUsn1ULOC0C4c5YF3xeI8=
解密字符串: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 );
输出:
AES 数据:password12345678
57AayWF4jKYx7KzGkwudIKNlwA+HERrmiy1Z0szzZds=
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=
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,就在这里。感谢 sarnold 提供线索:)
Python 输出:
加密字符串:C9pEG6g8ge76xt2q9XLbpw==
解密字符串:aaaaaaaaaaaaaaaa
现在输出是:
iOS 输出:
AES 数据:aaaaaaaaaaaaaaaaa
C9pEG6g8ge76xt2q9XLbpw==
OK , here it is. Thanks sarnold for the clue :)
Python OUTPUT:
Encrypted string: C9pEG6g8ge76xt2q9XLbpw==
Decrypted string: aaaaaaaaaaaaaaaa
And now output is:
iOS Output:
AES data : aaaaaaaaaaaaaaaa
C9pEG6g8ge76xt2q9XLbpw==