3

最近、CCCrypt を使用して自己定義の暗号アルゴリズムを実装しています。ただし、CCCryptorUpdate を使用して暗号化テキストを復号化すると、出力は元のプレーン テキストより常に 8 バイト少なくなります。以下は私のコードです:

+ (void) EncryptBy3DES:(NSInputStream*)strmSrc Output:(NSOutputStream*)strmDest CryptoRef:(CCCryptorRef)tdesCrypto
{   
    size_t  dataOutMoved;
    uint8_t inputBuf[BlockSize];
    uint8_t outputBuf[BlockSize];

    CCCryptorStatus cryptStatus;
    int iBytesRead = 0;

    while ( (iBytesRead = [strmSrc read:inputBuf maxLength:BlockSize]) > 0 ) 
    {
        NSLog(@"Bytes read from plain buffer: %d", iBytesRead);
        [Util FillBuffer:inputBuf Length:BlockSize Current:iBytesRead];

        cryptStatus = CCCryptorUpdate(tdesCrypto, &inputBuf, BlockSize, &outputBuf, BlockSize, &dataOutMoved);
        assert(dataOutMoved<=BlockSize && cryptStatus==noErr);
        NSLog(@"EncDataOutMoved: %ld", dataOutMoved);
        [Util FillBuffer:outputBuf Length:BlockSize Current:dataOutMoved];
        [strmDest write:outputBuf maxLength:BlockSize];
    }
}

+ (void) DecryptBy3DES:(NSInputStream*)strmSrc Output:(NSOutputStream*)strmDest CryptoRef:(CCCryptorRef)tdesCrypto
{       
    size_t  dataOutMoved;
    uint8_t inputBuf[BlockSize];
    uint8_t outputBuf[BlockSize+kCCBlockSize3DES];

    CCCryptorStatus cryptStatus;
    int iBytesRead = 0;

    while ( (iBytesRead = [strmSrc read:inputBuf maxLength:BlockSize]) > 0 ) 
    {
        NSLog(@"Bytes read from cipher buffer: %d", iBytesRead);
        cryptStatus = CCCryptorUpdate(tdesCrypto, &inputBuf, BlockSize, &outputBuf, BlockSize+kCCBlockSize3DES, &dataOutMoved);
        NSLog(@"Lengh needed: %zu", CCCryptorGetOutputLength(tdesCrypto, BlockSize, YES));
        NSLog(@"DecDataOutMoved: %ld", dataOutMoved);
        assert(dataOutMoved<=BlockSize && cryptStatus==noErr);
        [strmDest write:outputBuf maxLength:dataOutMoved];
    }
}


I encrypted 3 buffer of 4096. When decrypting them, the log shows that the size of 1st decrypted data is 4088, BUT! The missing data is actually in the begining of the 2nd decrypted data. 

2012-04-14 15:17:41.929 otest[25168:7803] Bytes read from cipher buffer: 4096
2012-04-14 15:17:41.929 otest[25168:7803] Lengh needed: 4104
2012-04-14 15:17:41.930 otest[25168:7803] DecDataOutMoved: 4088
end of 1st block:<..d71eaf27 affc4c8c b1c54afa c5434397 ebc17a49>

2012-04-14 15:17:45.291 otest[25168:7803] Bytes read from cipher buffer: 4096
2012-04-14 15:17:45.292 otest[25168:7803] Lengh needed: 4104
2012-04-14 15:17:45.293 otest[25168:7803] DecDataOutMoved: 4096
begining of 2nd block <**86b61bce b4342728** 88240a64 837327d4 0bb572a2 f5220928

86b61bce b4342728は、暗号化前の最初のブロックの最後にあったことに注意してください。

1 ブロック目の先頭も確認しましたが、データの範囲を間違えていないことは確かです。データは復号化されているようですが、次の操作まで取得されません。

各暗号化/復号化操作で完全なブロックを取得したいのですが、キーと Iv ビットの両方を渡す必要があるため、関数 CCCrypt を使用したくありません。私は CCCryptorRef をそれに渡したいだけです。これは比較的簡単です。

ヒューバート

4

2 に答える 2

1

を呼び出して、暗号化と復号化を完了する必要がありますCCCryptFinal。これにより、パディングの追加/削除が処理されます。

于 2012-04-14T14:08:47.857 に答える
1

CCCrypt 関数を使用して、何かを DES 暗号化します。暗号化するデータの長さが8Byteの倍数でない場合、エラーサイズが発生しました。

したがって、私はこれを次のように処理します。

- (NSString *) encryptUseDES:(NSString *)plainText key:(NSString *)key
{
    NSString *ciphertext = nil;
    const char *textBytes = [plainText UTF8String];
    NSUInteger dataLength = [plainText length];
    NSUInteger len1=dataLength % 8;

    if(len1!=0)
    {
        dataLength+=(8-len1);
    }    
    unsigned char buffer[1024];
    memset(buffer, 0, sizeof(char));
    size_t numBytesEncrypted = 0;

    Byte iv[] = {0x02, 0x00, 0x01, 0x02, 0x00, 0x06, 0x00, 0x05};

    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES,
                                          NULL,
                                          [key UTF8String], kCCKeySizeDES,
                                          iv,
                                          textBytes, dataLength,
                                          buffer, 1024,
                                          &numBytesEncrypted);
.....
}

その後、すべて大丈夫です!

于 2013-08-09T06:51:52.423 に答える