苹果给的内容比较多,这里只看关键部分:偏移量 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 的内存
真相大白了
虽然传的@"",但系统自动把后面的也当做偏移量进行了运算。
经验证把@”“换成正确长度的内存中数据 ,能解密出来正确数据。