在iOS中AES加解密时调用CCCrypt的时候偏移量传的@““导致新工程无法解出旧工程的密文引发的事件(为什么偏移量,这里不用,设置为nil;不用的话,必须为nil,不可以为@“”)

 

苹果给的内容比较多,这里只看关键部分:偏移量 iv


    @param      iv              Initialization vector, optional. Used for

                                Cipher Block Chaining (CBC) mode. If present,

                                must be the same length as the selected

                                algorithm's block size. If CBC mode is

                                selected (by the absence of any mode bits in

                                the options flags) and no IV is present, a

                                NULL (all zeroes) IV will be used. This is

                                ignored if ECB mode is used or if a stream

                                cipher algorithm is selected. For sound encryption,

                                always initialize IV with random data.


CCCryptorStatus CCCrypt(
    CCOperation op,         /* kCCEncrypt, etc. */
    CCAlgorithm alg,        /* kCCAlgorithmAES128, etc. */
    CCOptions options,      /* kCCOptionPKCS7Padding, etc. */
    const void *key,
    size_t keyLength,
    const void *iv,         /* optional initialization vector */
    const void *dataIn,     /* optional per op and alg */
    size_t dataInLength,
    void *dataOut,          /* data RETURNED here */
    size_t dataOutAvailable,
    size_t *dataOutMoved)
    API_AVAILABLE(macos(10.4), ios(2.0));

翻译成中文是:

Initialization vector, optional. Used forCipher Block Chaining (CBC) mode. If present,must be the same length as the selectedalgorithm's block size. If CBC mode isselected (by the absence of any mode bits inthe options flags) and no IV is present, aNULL (all zeroes) IV will be used. This isignored if ECB mode is used or if a streamcipher algorithm is selected. For sound encryption,always initialize IV with random data.

初始化向量iv,是可选的。用于密码块链接(CBC)模式。如果存在,则长度必须与所选算法的块大小相同。如果选择了CBC模式(由于选项标志中没有任何模式位),并且不存在IV,则将使用全零IV。如果使用ECB模式或选择了streamcipher算法,则忽略此选项。对于声音加密,总是用随机数据初始化IV。

 

这里说的不明显,然后在其他代码中搜到内容是:

"偏移量,这里不用,设置为nil;不用的话,必须为nil,不可以为@“” "

但是为什么,没有人说。

 

今天来解答的就是这个问题。

首先长度不够:长度必须与所选算法的块大小相同

长度不够,系统怎么办呢?系统会继续向后读取内存中的值

那向后读取的内存中的值是什么呢?

打断点 查看     *vkey 的内存

 

 

真相大白了

 

虽然传的@"",但系统自动把后面的也当做偏移量进行了运算。

 

经验证把@”“换成正确长度的内存中数据 ,能解密出来正确数据。

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值