2012-01-22 7 views
5

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 =

+2

あなたがしている場合にのみ、パディングを無視することができます[ECBモード](http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_28ECB.29)を使用してください。私はあなたがECBモードを使用していないことを願っています。 – sarnold

+0

私はPythonでモードを設定していない、私はそれがデフォルト値を使用していると思う。 – Bora

+0

@sarnold ECBモードでもパディングが必要です.IVをECBモードに設定しないでください。また、プラットフォームがIVを必要とする場合はすべてゼロに設定する必要がありますが、確実に埋め込みが必要です。それとも、Python/iOSプラットフォームに特有のものですか? –

答えて

5

OK、ここにあります。おかげで手がかり:)

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 = "123456789" #os.urandom(BLOCK_SIZE) 

    # create a cipher object using the random secret 
    cipher = AES.new(secret,mode) 

    # encode a string 
    #tx=cipher.encrypt('123456789') 
    #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のためsarnold OUTPUT:

暗号化された文字列:C9pEG6g8ge76xt2q9XLbpwの==

復号化された文字列:aaaaaaaaaaaaaaaa

は*でkCCOptionECBModeにAESのCCOptionsを変更iOS。 *

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

そして今、出力は次のようになります。

のiOS出力:

AESデータ:aaaaaaaaaaaaaaaa
C9pEG6g8ge76xt2q9XLbpwの==

+0

素敵な仕事:)私はあなたが "コミュニティのwiki"を選択してはいけないと思っていますが、あなた自身の質問に答えるのに間違いはありません。あなたはそれから評判を得なければなりません。 ;) – sarnold

+0

Wikiステータスが削除されました。コミュニティWikiは、時間の経過と共に変化する可能性のある非常に一般的な質問に対する標準的な回答など、より広範なユーザーグループが何かを編集できるようにする場合に便利です。あなた自身の質問に答えるのに間違っていることはありません。実際、それは励まされます。 –