2016-10-26 2 views
0

私の友人はAES256について2つの方法でobjective-Cを書きました。キーの長さは常に= kCCKeySizeAES256+1で33バイトAES256で33バイトのキーを使うことができます

は今、私はポートに、このようなJavaphpgoなどの別の言語への2つの方法を必要としています。他の言語は受け付けていません 33バイトのキー

私のiOSアプリは自分のユーザーのためオンラインであるため、objective-Cコードを変更することはできません。

これらの2つの方法を別の言語に移植するのを手伝ってください!

Objective-Cのメソッド(私は私の友人を嫌い):

NSData * AES256EncryptWith(NSData *data, NSString *key){ 
    // 'key' should be 32 bytes for AES256, will be null-padded otherwise 
    char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused) 
    bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding) 

    // fetch key data 
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; 

    NSUInteger dataLength = [data length]; 

    //See the doc: For block ciphers, the output size will always be less than or 
    //equal to the input size plus the size of one block. 
    //That's why we need to add the size of one block here 
    size_t bufferSize = dataLength + kCCBlockSizeAES128; 
    void *buffer = malloc(bufferSize); 

    size_t numBytesEncrypted = 0; 
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, 
              keyPtr, kCCKeySizeAES256, 
              NULL /* initialization vector (optional) */, 
              [data bytes], dataLength, /* input */ 
              buffer, bufferSize, /* output */ 
              &numBytesEncrypted); 
    if (cryptStatus == kCCSuccess) { 
     //the returned NSData takes ownership of the buffer and will free it on deallocation 
     return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted]; 
    } 

    free(buffer); //free the buffer; 
    return nil; 
} 

NSData * AES256DecryptWith(NSData *data, NSString *key){ 
    // 'key' should be 32 bytes for AES256, will be null-padded otherwise 
    char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused) 
    bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding) 

    // fetch key data 
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; 

    NSUInteger dataLength = [data length]; 

    //See the doc: For block ciphers, the output size will always be less than or 
    //equal to the input size plus the size of one block. 
    //That's why we need to add the size of one block here 
    size_t bufferSize = dataLength + kCCBlockSizeAES128; 
    void *buffer = malloc(bufferSize); 

    size_t numBytesDecrypted = 0; 
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, 
              keyPtr, kCCKeySizeAES256, 
              NULL /* initialization vector (optional) */, 
              [data bytes], dataLength, /* input */ 
              buffer, bufferSize, /* output */ 
              &numBytesDecrypted); 

    if (cryptStatus == kCCSuccess) { 
     //the returned NSData takes ownership of the buffer and will free it on deallocation 
     return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted]; 
    } 

    free(buffer); //free the buffer; 
    return nil; 
} 

答えて

0

をあなたはコード内のコメントを読んでください...それが唯一のキーのために32バイトを使用しています。最後のバイトは文字列ターミネータのみです。したがって、33バイトのキーから最初の32バイトを使用してください。

AES256は32バイト(ist 256ビット)でのみ動作します

+0

はい、私はそれを知っています。しかし、私のアプリは33bytesのaes256を使っていて、AppStoreにいます。私はそれらを変更することはできません。私の質問は、Objective-Cのように33byteでaes256を作れる言語はありますか? – VietHung

+0

no .. aes256は32バイトのみで動作するので...あなたのコードもobjective-cの32バイトしか使用しません – Bastian

+0

私のコードは33バイト目を気にしませんか? – VietHung

関連する問題