简体   繁体   中英

How to do AES decryption using iv & key with cryptoswift?

Implemented in javascript using cryptojs but unable to do the same in swift using cryptoswift. I am unable to understand how key, iv & ciphertext are generated in javascript looking at the below code. Out of ideas to do the same in Swift using cryptoswift.

decryptPayloadQR(ciphertextStr, SecretKey) {

try {
     let key = CryptoJS.enc.Utf8.parse(SecretKey);
     var ciphertext = CryptoJS.enc.Base64.parse(ciphertextStr);
     var iv = ciphertext.clone();
     iv.sigBytes = 16;
     iv.clamp();
     ciphertext.words.splice(0, 4);
     ciphertext.sigBytes -= 16;
     var decrypted = CryptoJS.AES.decrypt({ ciphertext: ciphertext }, key, {
       iv: iv
     });
     console.log("===decrypted.toString(CryptoJS.enc.Utf8)=",decrypted.toString(CryptoJS.enc.Utf8));
     return decrypted.toString(CryptoJS.enc.Utf8);
   } catch (error) {
     console.log("===error in decrpt fun === : ",error);
     return false
   }
 }

Result is string which can be converted to json.

Swift code below.

    let encryptedString = "OdqBIN4twOxKwe1aIZhTatkzVdfvN/mfB2Arra38tF25+3efc+Kl+HABZtqiGrTIihSpfW/XHW8+31Rl+uHZB2immfB6/w8E2j5ylCV8RKrqHVMMB7BPBLyr8oTuxmYEla1J1NxywLFPyNZCI4zmkYczwVSyssd5VKWH8WaBHR5Yai6MaxugdohW40byPx6xqbhwjHN5w+dh3dJBSFbM5EhQTqPwfBA1v1UBrCXooay47keSFor/7ywjV3e2bU5JrL0o+S26UF6zoVkRP1tdGAY3TqYyrPLUHVq0nIzkmnZdQk5gFGjN0sF58WdhuqjgRAtSrbmL5biuOtmQHFmNx4sUjkF4pYvfYxrj3ze2H/6G03cxCzW/DUytV678IBrd"

//======== iv & cipher text should be same as generated in above javascript code ==========//
        //======== Should these keys be in hex =======//
        let iv = "" /* No idea how to get iv from encrypted string */
        let cipherText = "" /* No idea how to get cipherText from encrypted string */
        let key = "abababababababab"

        do {
            let decryptor = try AES(key: key, iv: iv)
            let decryptedBytes = try decryptor.decrypt(cipherText.bytes)
            print(String(bytes: decryptedBytes, encoding: .utf8)!)
        } catch {
            print(error)
        }

How do I get iv, key & cipher text for swift similar to that in javascript? Thanks in advance.

I was able to do the same in Swift with cryptoswift using below code.

let encryptedString = "OdqBIN4twOxKwe1aIZhTatkzVdfvN/mfB2Arra38tF25+3efc+Kl+HABZtqiGrTIihSpfW/XHW8+31Rl+uHZB2immfB6/w8E2j5ylCV8RKrqHVMMB7BPBLyr8oTuxmYEla1J1NxywLFPyNZCI4zmkYczwVSyssd5VKWH8WaBHR5Yai6MaxugdohW40byPx6xqbhwjHN5w+dh3dJBSFbM5EhQTqPwfBA1v1UBrCXooay47keSFor/7ywjV3e2bU5JrL0o+S26UF6zoVkRP1tdGAY3TqYyrPLUHVq0nIzkmnZdQk5gFGjN0sF58WdhuqjgRAtSrbmL5biuOtmQHFmNx4sUjkF4pYvfYxrj3ze2H/6G03cxCzW/DUytV678IBrd"
let aesIVBlockSize = 16

let cipherData = Data(base64Encoded: encryptedString, options: .ignoreUnknownCharacters)
let ivBytes = Array(Array(cipherData!).dropLast(cipherData!.count - aesIVBlockSize))
let actualCipherBytes = Array(Array(cipherData!).dropFirst(aesIVBlockSize))

do {
     let aes = try AES(key: Array("abababababababab".data(using: .utf8)!), blockMode: CBC(iv: ivBytes))
     let decrypted = try aes.decrypt(actualCipherBytes)
     let outData = Data(bytes: decrypted)
     print(outData)
     print(String(bytes: outData, encoding: .utf8) as Any)
   } catch {
     print("Decryption error: \(error)")
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM