【问题标题】:objective c cryptography: how to get equal size of encrypted data for various length目标c密码学:如何获得不同长度的相同大小的加密数据
【发布时间】:2011-12-16 04:24:51
【问题描述】:

我使用 CommonCryptor.h 进行加密和解密以加密各种大小的文本。

EX.
// 第一个字符串

你好,彼得,你好吗。你今天看起来不太好。你想去百货公司吗

//第二个字符串

我很好。

我使用以下链接借用的方法。 Any cocoa source code for AES encryption decryption? 无论我加密多少个字符,我如何才能获得从该方法返回的特定且相等大小的 NSData,例如 256 个字节。 例如,我希望第一个和第二个字符串的加密数据大小为 256 字节。

如果不可能,对于不同长度的 NSData,我应该使用哪种算法来获得相同大小的加密文本?

#import <CommonCrypto/CommonCryptor.h>
@implementation NSData (AESAdditions)
- (NSData*)AES256EncryptWithKey:(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 = [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);

    size_t numBytesEncrypted    = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                          keyPtr, kCCKeySizeAES256,
                                          NULL /* 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 {
    // '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 = [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);

    size_t numBytesDecrypted    = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                          keyPtr, kCCKeySizeAES256,
                                          NULL /* initialization vector (optional) */,
                                          [self 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;
}

【问题讨论】:

  • 假设一个文本是“我是!”另一个是“乱世佳人”(书的正文,不是词组)。你想让两个文本的长度相同吗?您是否希望能够解密它们并恢复原始数据? (你几乎肯定会这样做,但更有趣的事情已经知道了。)

标签: objective-c encryption cryptography aes


【解决方案1】:

正如@Jonathan Leffler 指出的那样,这可能会导致产生大量额外的消息流量。

基本上,您需要在较短的消息中添加足够多的垃圾,使其长度与较长的消息相同。有很多方法可以做到这一点,但这里有一个非常简单的方法。假设您的较短消息不以“Q”结尾。添加足够的 Q 使其与较长消息的长度相同。解密时,删除所有尾随 Q。如果消息确实以“Q”结尾,则使用“X”代替。该方案并非万无一失(较长的消息也可能以 Q 或 X 结尾)。对于一个万无一失的方案,您需要在填充中某处的固定大小字段中编码原始长度(或填充长度),这可能意味着也填充较长的消息。

【讨论】:

    猜你喜欢
    • 2021-10-20
    • 1970-01-01
    • 2013-04-24
    • 2014-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-07
    相关资源
    最近更新 更多