【发布时间】:2011-11-10 18:57:21
【问题描述】:
我正在阅读有关如何对密钥进行加盐以确保加密安全的教程,但无法充分利用它。我对密码学知之甚少,需要一些帮助。我正在使用 commoncrypto 来加密文件,并且已经完成,除了它不安全的事实......
这就是我所拥有的:
- (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)
NSLog(@"You are encrypting something...");
// 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;
}
如果有人可以帮助我,并确切地告诉我我将如何实施盐,那就太好了!再次感谢!
【问题讨论】:
-
Salting a key 是一个非常奇怪的术语。盐通常与散列一起使用。 IV 在对称加密中的作用类似于盐。你能链接那个教程吗?
-
他在他的密钥派生函数中使用了盐(这与散列有关)。所以盐只用于从密码中获取密钥。
-
我会在上面的代码中添加什么以确保密钥安全?因为到目前为止,它根本不安全。如果我一遍又一遍地用相同的确切密钥加密相同的确切数据,密文就不可能相同。
-
这将是一个 IV,正如 @CodeInChaos 刚刚所说的那样。
标签: cocoa encryption key aes salt