ios - AES256 大于 32Bytes

标签 ios objective-c aes nsdata

我有这个将 NSData 转换为 AES256 的 Objective-c 代码,最近我发现密码的最大数量是 32 个字节:

- (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;
}

我想知道是否有可能找到一些方法来增加这些字节数?导致密码大于32位,有可能吗?

最佳答案

  1. 字节不是数字。

  2. 256 位 key 远远超出了暴力破解的范围。

  3. 不要为加密 key 使用密码。如果您需要使用密码,请使用 PBKDF2 等密码 key 派生函数从密码创建安全加密 key 。密码可以是任意长度,PBKDF2 函数将生成正确长度的安全加密 key 。指定 > 10K 的迭代计数。

  4. 让 CCCrypt 工作是创建安全加密方案的微不足道的部分。

  5. 考虑使用 RNcryptor,它将处理这些细节以及更多内容。

  6. 您将需要处理密码/ key 的安全性,这并不容易。

关于ios - AES256 大于 32Bytes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30008647/

相关文章:

c - TinyOS:如何将 uint_16 转换为两个 uint_8 整数(AES 加密)

php - Alamofire 下载文件存在进度问题

ios - 快速创建一个 300dpi 黑白的 TIFF Group 4

ios - 导航栏不显示

objective-c - 使用 Singleton 类的全局变量 NSMuteableArray

iphone - 添加 SQLcipher sqlite3_exec 后返回 SQLITE_NOTADB

ios - 第二次重新创建 MPMoviePlayerController 时设置 initialPlaybackTime 失败

c - c中的openssl aes解密

java - cipher.update在java中做什么?

ios - 将 Siesta 与非 Restful API 结合使用