Python 中的 AES 加密与 iOS 中的不同

发布于 2024-12-28 09:53:36 字数 1894 浏览 1 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∝单色的世界 2025-01-04 09:53:36

好的,就在这里。感谢 sarnold 提供线索:)

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 输出:

加密字符串:C9pEG6g8ge76xt2q9XLbpw==

解密字符串:aaaaaaaaaaaaaaaa

*在 iOS 中将 AES CCOptions 更改为 kCCOptionECBMode。 *

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

现在输出是:

iOS 输出:

AES 数据:aaaaaaaaaaaaaaaaa
C9pEG6g8ge76xt2q9XLbpw==

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==

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文