IOSで文字列を暗号化してから、それをTCPサーバーに送信しようとしています。 PythonバージョンのコードとiOSバージョンを以下に示します。両方のバージョンの出力を参照してください。彼らはかなり似ていますが、長さが異なり、私は理由を知らない。誰でもそれをチェックできますか、理由は何でしょうか?PythonでのAES暗号化は、iOSと異なる形式です。
私はすでに16のテキスト長を与えたので、PythonスクリプトのPADDINGは破棄しなければならないことに注意してください。
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 = "123456789"
# 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:
暗号化された文字列:57AayWF4jKYx7KzGkwudIBZUsn1ULOC0C4c5YF3xeI8 =
復号化された文字列:password12345678
NSString *[email protected]"123456789";
NSString *mystr [email protected]"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データ:password12345678
57AayWF4jKYx7KzGkwudIKNlwA + HErrmiy1Z0szzZds =
あなたがしている場合にのみ、パディングを無視することができます[ECBモード](http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_28ECB.29)を使用してください。私はあなたがECBモードを使用していないことを願っています。 – sarnold
私はPythonでモードを設定していない、私はそれがデフォルト値を使用していると思う。 – Bora
@sarnold ECBモードでもパディングが必要です.IVをECBモードに設定しないでください。また、プラットフォームがIVを必要とする場合はすべてゼロに設定する必要がありますが、確実に埋め込みが必要です。それとも、Python/iOSプラットフォームに特有のものですか? –