开发者

Changing ciphertext upon identical encryption (IV) Cocoa?

开发者 https://www.devze.com 2023-04-05 23:12 出处:网络
I was reading a tutorial on how to salt a key to make your encryption secure, but couldn\'t make much of it. I don\'t know a lot about cryptography, and need some help. I am using commoncrypto to encr

I was reading a tutorial on how to salt a key to make your encryption secure, but couldn't make much of it. I don't know a lot about cryptography, and need some help. I am using commoncrypto to encrypt files, and am done, except for the fact that it isn't secure... The ciphertext must not be the same when the user encrypts the same exact file with the same exact key twice.

This is what I have:

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

If someone can help me out, and show me exactly how I would implement salt, that would be great! Thanks again!


First, what you are looking for here is called an initialization vector or IV. Salts are used with hashes, not ciphers. Note that both IVs and salts are specific examples of a nonce.

Now that we have terminology out of the way, what you'll want to do is use a different cipher mode. Currently you're using what's known as ECB - "electronic code book". As you have noted, it has the disadvantage that encrypting the same plaintext twice results in the same ciphertext, making it possible to reverse if the attacker can guess a potential plaintext.

There are a number of alternate cipher modes that fix this - one of the most popular ones is CBC - "cipher block chaining". Essentially, you insert a random block (the IV) at the start; then for each block, XOR the previous ciphertext block (the IV, for the first block) with the plaintext block before passing it through the cipher.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号