-1

I've searched for a lot of articles online and only find [iOS 3DES with ECB return half correct data ] on Stackoverflow, however when I copy the code in this article, the complier returns a different encrypted result with that I got from the online encrypt tools.

Can anyone help me here?
Thanks

plus, I can't really read obj-c. Just turned to swift from javascript and PHP.

===========Edit===========

Now, I changed my method to AES using swift 1.2. Code is listed as follows(iv and key are kept static for test):

 let key:String = "12345678901234567890123456789012" let iv:String = "12345678901234567890123456789012" let keyData: NSData! = (key as NSString).dataUsingEncoding(NSUTF8StringEncoding) as NSData! let keyBytes = UnsafePointer<UInt8>(keyData.bytes) let keyLength = size_t(kCCKeySizeAES256) let plainData: NSData! = (plainText as NSString).dataUsingEncoding(NSUTF8StringEncoding) as NSData! let dataLength = size_t(plainData.length) let dataBytes = UnsafePointer<UInt8>(plainData.bytes) var bufferData = NSMutableData(length: Int(dataLength) + kCCBlockSizeAES128)! var bufferPointer = UnsafeMutablePointer<UInt8>(bufferData.mutableBytes) let bufferLength = size_t(bufferData.length) let operation: CCOperation = UInt32(kCCEncrypt) let algoritm: CCAlgorithm = UInt32(kCCAlgorithmAES128) let options: CCOptions = UInt32(kCCOptionPKCS7Padding) let ivData: NSData! = (iv as NSString).dataUsingEncoding(NSUTF8StringEncoding) as NSData! let ivPointer = UnsafePointer<UInt8>(ivData.bytes) var numBytesEncrypted: size_t = 0 var cryptStatus = CCCrypt(operation, algoritm, options, keyBytes, keyLength, ivPointer, dataBytes, dataLength, bufferPointer, bufferLength, &numBytesEncrypted) 

And here's my PHP code:

 function addPkcs7Padding($string) { $cipher = MCRYPT_RIJNDAEL_256; $mode = MCRYPT_MODE_CBC; $blocksize = mcrypt_get_iv_size($cipher, $mode); $len = strlen($string); $pad = $blocksize - ($len % $blocksize); $string .= str_repeat(chr($pad), $pad); return $string; } function aes256cbcEncrypt($str, $iv, $key ) { return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, addPkcs7Padding($str) , MCRYPT_MODE_CBC, $iv)); } 
2
  • Note that DES and 3DES should not be used for new work, only for compatibility with existing legacy implementations. The current standard is AES. Additionally ECB mode is a very poor choice, it lacks the security of CBC mode. Commented Apr 7, 2015 at 11:32
  • Thanks Zaph, but now when I changed my encryption method to AES256 with PKCS7 padding, the result is different from what I get from a PHP server. I pasted my code above, how should I solve this problem? Commented Apr 11, 2015 at 17:32

1 Answer 1

0
  1. You are not setting the length of the encrypted data to the value returned by reference: numBytesEncrypted. In this case: bufferData.length = numBytesEncrypted.

  2. MCRYPT_RIJNDAEL_256 specifies a block size of 256 bits, AES only uses a block size of 128, change that in mcrypt to MCRYPT_RIJNDAEL_128. Note: the block size is not the same thing as the key size.

  3. The iv size is the block size (128-bits/16-bytes).

  4. Show example input and output data for more help.

For a Swift example this SO answer:

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks again for the quick reply. How should I set numBytesEncrypted anyway? Lots of docs online said that before the encryption, numBytesEncrypted should be set to zero in the type of size_t.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.