iphone - AES 256/CBC/Withoutpadding in objective-c

标签 iphone ios objective-c encryption

我正在尝试使用零填充和 CBC 模式实现 AES 256。我尝试了所有来自 cypto 的方法,但结果与来自服务器的不同

我正在使用这个代码

在我传递简单字符串以检查 databstring 的地方,key 和 iv 作为“iphone”传递。

/**************
- (NSData *)AES256Encrypt:(NSString *)dataString WithKey:(NSString *)key iv:(NSString *)iv {

    // '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];

    NSLog(@"keyPtr: '%s'", keyPtr);

   NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];

    NSLog(@"keyPtr: '%s'", keyData.bytes);
    NSData *dataToEncrypt = [dataString dataUsingEncoding:NSUTF8StringEncoding];
    NSData *ivData = [iv dataUsingEncoding:NSUTF8StringEncoding];

    NSUInteger dataLength = [dataToEncrypt 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;
    CCCryptorRef cryptorRef;

    CCCryptorStatus rc;




    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, 0,
                                          keyData.bytes, kCCKeySizeAES256,
                                          ivData.bytes, // initialisation vector
                                          dataToEncrypt.bytes,
                                          dataToEncrypt.length, /* input */
                                          buffer, bufferSize, /* output */
                                          &numBytesEncrypted);
    if (cryptStatus == kCCSuccess) {


      //  NSString *someDataHexadecimalString = [[NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted] hexadecimalString];

        NSLog(@"%@",[NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted]);



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

**********/

但是每次打印的结果都不一样。

请帮忙。

最佳答案

您的 IV(“iphone”)太短了。 CBC 模式要求 IV 等于密码算法的 block 大小(AES 为 16 字节)。 CCCrypt 从您提供的 iv 缓冲区中读取 16 个字节,并且由于您的缓冲区太短,缓冲区末尾后内存中的任何垃圾都将用作 IV 的其余部分。

所以基本上你每次都使用不同的 IV,这就是为什么你的密文每次都不同。

一般来说,不要将字符串用于 IV。为了安全起见,每条不同的消息的 IV 应该不同,如果您使用硬编码字符串,这很难做到。只需为IV生成16个随机字节,并将它们放在密文的开头。

关于iphone - AES 256/CBC/Withoutpadding in objective-c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18763911/

相关文章:

iphone - 使用 info.plist 为多目标应用程序存储特定于目标的值

iphone - iOS背景图UIVIew和CALayer的关系

iphone - 按下 View Controller 时出现黑屏

iphone - 如何将下载的数据保存到设备的文件系统

iphone - 如何自定义iphone sdk中的默认消息编辑器?

iphone - subview 的导航 Controller 行为?

iphone - 帮助填写 "info.plist"文件

ios - 以编程方式在 iOS 设置中设置全局 Http 代理的可能方法是什么?

ios - 使用我的 Vimeo 帐户中的视频填充 UITableView

iphone - Cocos2d,我忘记了延迟场景的命令是什么