0

私のアプリケーションでは、ファイルをネットワーク経由で送信する前に暗号化する必要があり、反対側では復号化されます。これは私のコードです。

-(void)doEncryptTest:(NSString *)pFileName{

    // read the NSData; 
    NSStringEncoding encoding  =NSUTF8StringEncoding;

    NSString *pFileContent = @"xaaaaaaxxaaaaaax";

    NSString *pKey = @"01234567012345670123456701234567";


    NSData *pData = [pFileContent dataUsingEncoding:encoding];

    NSData *pEncryptedData = [pData AES256EncryptWithKey:pKey];


    NSData *decrypted=[pEncryptedData AES256DecryptWithKey:pKey Data:pEncryptedData];

    NSString* pDecryptedDataStr = [[NSString alloc] initWithData:decrypted
                                                      encoding:encoding];

}

これは正常に動作しています。データ サイズが 16 バイトの場合にのみ、リアルタイムの場合、サイズ 151186 バイトのファイルを送信したとき、[pEncryptedData] のサイズは 15200 であり、実際には復号化されたデータのサイズは元のデータと同じです, しかし pDecryptedDataStr は空白です, 何がうまくいかないのか推測してください, 以下を参照してください. 暗号化と復号化関数,

int keySize = kCCKeySizeAES256;
int padding = kCCOptionPKCS7Padding;
char ivKey[16]={0,0,0,0,
             0,0,0,0,
             0,0,0,0,
             0,0,0,0};
//////////////*Encryption*///////////////////
- (NSData *)AES256EncryptWithKey:(NSString *)key{
    // 'key' should be 32 bytes for AES256, will be null-padded otherwise
    char keyPtr[keySize + 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 = [self 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 );
    char ivVector[kCCBlockSizeAES128+1];
    // fetch key data
    [key getCString:ivVector maxLength:sizeof( ivVector ) encoding:NSUTF8StringEncoding];

    bzero( ivVector, sizeof( ivVector ) ); // fill with zeroes (for padding)


    const void *iv=NULL;
    size_t numBytesEncrypted = 0;


    CCCryptorStatus cryptStatus = CCCrypt( kCCEncrypt, kCCAlgorithmAES128, padding,
                                          keyPtr, keySize,
                                          ivKey /* initialization vector (optional) */,
                                          [self 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 *)AES256DecryptWithKey:(NSString *)key   Data:(NSData*)EncryptedData{
    bool same =[self isEqualToData:EncryptedData];

    // 'key' should be 32 bytes for AES256, will be null-padded otherwise
    char keyPtr[keySize+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 = [EncryptedData 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 );
    const void *iv=NULL;
    char ivVector[kCCBlockSizeAES128+1];
    // fetch key data
    [key getCString:ivVector maxLength:sizeof( ivVector ) encoding:NSUTF8StringEncoding];

    bzero( ivVector, sizeof( ivVector ) ); // fill with zeroes (for padding)

    size_t numBytesDecrypted = 0;
    NSData *output_decrypt = [[NSData alloc] init];
    CCCryptorStatus cryptStatus = CCCrypt( kCCDecrypt, kCCAlgorithmAES128, padding,
                                          keyPtr, keySize,
                                          ivKey /* initialization vector (optional) */,
                                          [EncryptedData bytes], dataLength, /* input */
                                          buffer,bufferSize ,//bufferSize, /* output */
                                          &numBytesDecrypted );
    output_decrypt = [NSMutableData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
    same =[self isEqualToData:output_decrypt];
    if( cryptStatus == kCCSuccess )
    {
        //the returned NSData takes ownership of the buffer and will free it on deallocation

        NSData *pData = [[NSData alloc]initWithBytes:buffer length:numBytesDecrypted];

        return pData;

    }

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

1 に答える 1

0

これを見てくれてありがとう、これが問題だった、

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

        **NSData *pData = [[NSData alloc]initWithBytes:buffer length:numBytesDecrypted];**

        return pData;

    }

解決策は次のとおりです。

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


        return output_decrypt;

    }

なぜ問題が発生したのかわかりません。もちろん、戻り値の型を変更し、呼び出し元の関数で何らかの処理を行ってメモリを解放する必要があります。

于 2012-07-31T10:52:39.327 に答える