gpt4 book ai didi

iphone - 如何使用 CCCrypt() 加密文件?

转载 作者:可可西里 更新时间:2023-11-01 04:40:36 31 4
gpt4 key购买 nike

当我加密文件(doc、pdf 等)时,我使用:

size_t bufferSize = dataLength + kCCBlockSizeAES128;    
CCCrypt( kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
keyPtr, kCCKeySizeAES256,
NULL /* initialization vector (optional) */,
dataBytes, dataLength, /* input */
buffer, bufferSize,/* output */
&numBytesEncrypted );

解密时,我使用:

size_t bufferSize = dataLength + kCCBlockSizeAES128;
CCCryptorStatus result = CCCrypt( kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
keyPtr, kCCKeySizeAES256,
NULL /* initialization vector (optional) */,
dataBytes, dataLength,/* input */
buffer, bufferSize,/* output */
&numBytesEncrypted );

但解密时,返回error:kCCDecodeError = -4304。

如果我在解密时去掉kCCOptionPKCS7Padding的参数,就没有错误了。但是文件也打不开。

那么,你能告诉我如何传递这些参数吗?

非常感谢!

最佳答案

这用于加密

    NSString *key =@"YourKey";
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];

char *dataIn = "This is your data";
char dataOut[500];// set it acc ur data
bzero(dataOut, sizeof(dataOut));
size_t numBytesEncrypted = 0;

CCCryptorStatus result = CCCrypt(kCCEncrypt, kCCAlgorithmAES128,kCCOptionPKCS7Padding, keyPtr,kCCKeySizeAES256, NULL, dataIn, strlen(dataIn), dataOut, sizeof(dataOut), &numBytesEncrypted);

这是为了解密

char dataOut2[500];
bzero(dataOut2, sizeof(dataOut2));
size_t numBytesDecrypted = 0;

CCCryptorStatus result = CCCrypt(kCCDecrypt, kCCAlgorithmAES128,kCCOptionPKCS7Padding, keyPtr,kCCKeySizeAES256, NULL, dataOut, numBytesEncrypted, dataOut2, sizeof(dataOut2), &numBytesDecrypted);

关于iphone - 如何使用 CCCrypt() 加密文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5564641/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com