I need to encrypt and decrypt in Swift 3 by using AES-CCM.
I have installed module CryptoSwift, and my app is ok to do normal AES128 like this:
//aes
let input: Array<UInt8> = [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f]
let key: Array<UInt8> = [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f]
let iv: Array<UInt8> = AES.randomIV(AES.blockSize)
do {
let encrypted = try AES(key: key, iv: iv, blockMode: .CBC, padding: .pkcs7).encrypt(input)
print(encrypted)
let decrypted = try AES(key: key, iv: iv, blockMode: .CBC, padding: .pkcs7).decrypt(encrypted)
print(decrypted)
} catch {
print(error)
}
But I cannot find a way to do CCM mode which there is no IV but a nonce shorter than 16 bytes.
Is CryptoSwift able to do AES-CCM or do I have to look for other modules?
Please give me some suggestion.
Related
I'm working on a project that'll open PDF books inside application. I'm fetching the books from the server and they're coming as locked pdf pages and I'm also fetching each pages' password with another API.
The problem is, the list of passwords are coming as AES encrypted. I can not decrypt them properly. I don't know what am I missing. My code is below. Waiting for your answers. Thank you :) (I'm using CrytoSwift library)
(I'm taking key and iv with an another function and keeping them in two different variables.)
import CryptoSwift
var myEncryptedData : String = ""
var key32 : String = ""
var iv16: String = ""
func decryptData() {
do {
let aes = try AES(key: Array(key32.utf8), blockMode: CBC(iv: Array(iv16.utf8)), padding: .pkcs7)
let ciphertext = try aes.decrypt(myEncryptedData.bytes)
print(String(data: Data(ciphertext), encoding: .utf8));
} catch {
print(error)
}
}
I tried to decrypt encryptedString with AES decryption by using CryptoSwift library but I couldn't fetch the data that I expected.
I just added this code just below "do" and it worked
let d = Data(base64Encoded: encryptedString)
Working version is:
do {
let d = Data(base64Encoded: encryptedString)
let aes = try AES(key: Array(key32.utf8), blockMode: CBC(iv: Array(iv16.utf8)), padding: .pkcs7)
let ciphertext = try aes.decrypt(d!.bytes)
let stringCipherText = String(data: Data(ciphertext), encoding: .utf8)
} catch {
print(error)
}
I have a project that retrieve data from API and shows in the app. But API uses AES encryption, I have aesKey and aesIV key and these keys are base64 encoded. I need to encode another string with these keys. To do that I use CryptoSwift library but when I try to use this keys to cipher the string, swift console warns me about invalidKeySize.I tried to decode from base64 to string but it did not work as well. These are the keys for an example;
Key and IV size are AES256, for encryption and decryption I need to use PKCS7 padding type with ECB/CBC block mode
aesKey = lHLBfVxlGoKoaCqWORJEHh3jOvC2EBx2VHGyNAdqYV0=
aesIV = 2spaSfljZ/cunRbuVkdphQ==
and CryptoSwift code block is:
let aes = try AES(key: "\(aesKeyString)", iv: "\(aesIVString)")
let cipherText = try aes.encrypt(Array("all".utf8))
The aesKey and aesIV from the snippet is Base64 encoded, so it's not really useful in that form. The convenience initializer you used is for a String, not base64 encoded data that you provided (that happened to be string, but it's different).
let aesKey = "lHLBfVxlGoKoaCqWORJEHh3jOvC2EBx2VHGyNAdqYV0="
let key = [UInt8](base64: aesKey)
let aesIV = "2spaSfljZ/cunRbuVkdphQ=="
let iv = [UInt8](base64: aesIV)
let aes = try AES(key: key, blockMode: CBC(iv: iv))
let cipherText = try aes.encrypt(Array("all".utf8))
I have key = "somesomesomesome".md5(), length is 32 bytes, iv is empty, trying to decrypt encrypted string using CryptoSwift:
let aes = try AES(key: key.bytes, blockMode: CBC(iv: []), padding: .noPadding)
let result = try aes.decrypt(encrypted)
let decrypted = String(bytes: result, encoding: .utf8)
getting error "dataPaddingRequired". What I am doing wrong?
Add this before setting up your AES:
let padding = Padding.noPadding.add(to: encrypted, blockSize: AES.blockSize)
IN swift I am trying to encrypt text same as CryptoJS encryption. But both are not same here I share with you URL, I need to do the same encryption in Swift. Please help me, here I attach my code also.
Demo CryptoJS encryption
in this URL we are using CryptoJS encryption with ECB and pkcs7. Same code I am doing in ios but not the same
import UIKit
import Foundation
import CryptoSwift
class CryptoViewController: UIViewController {
let KEY = "0123456789123456"
override func viewDidLoad() {
super.viewDidLoad()
self.aesCBC_Encrypt(AES_KEY: KEY, payload: "Hello World!")
}
func aesCBC_Encrypt(AES_KEY: String,payload: String) -> String {
var result = ""
do {
let key: [UInt8] = Array(AES_KEY.utf8) as [UInt8]
let iv = AES.randomIV(AES.blockSize)
let bytes = payload.bytes
let aes = try! AES(key: key, blockMode: ECB(), padding: .pkcs7)
//let encrypted = try aes.encrypt(Array(self.utf8))
let encrypted = try aes.encrypt(bytes)
print("encrypted: \(encrypted)")
result = encrypted.toHexString()
print("AES Encryption Result: \(result)")
} catch {
print("Error: \(error)")
}
return result
}
}
From my code, I am getting this Result
AES Encryption Result: 1b8019c2add38f33de9099aefd5369f5
But when I am try to Encryption from CryptoJS, Then I am getting this Result
Encrypted Text:
Aeoo7GP6b4l/Pdxz2RS+qA==
I am using Cross-Platform-AES for my AES encryption/decryption within my swift app. My encryption is working fine. But when I try to decrypt the service returns value it doesn't give me the correct result. Here how I am doing decryption.
public func decryptStrings(text:String)->String{
let hashKey=cryptoLib.sha256(key, length: 31)
let decryptedData = cryptoLib.decrypt(text.data(using: String.Encoding.utf8), key: hashKey, iv: iv)
let decryptedString=decryptedData?.base64EncodedString()
print("decryptedString \(decryptedString! as String)")
return decryptedString!
}
Please explain me how can I decrypt this in correct way.
Thanks
ENCRYPTION
public func base64Convertion (secretcode:String)->String
{
let hashKey=cryptoLib.sha256(key, length: 31)
let encryptedData=cryptoLib.encrypt(secretcode.data(using: String.Encoding.utf8), key: hashKey, iv: iv)
let encryptedString=encryptedData?.base64EncodedString()
print("encryptedString \(encryptedString! as String)")
return encryptedString!
}
I solved the problem by changing the Decryption method in this way
public func decryptStrings(text:String)->String{
let hashKey=cryptoLib.sha256(key, length: 31)
let decodedData = Data(base64Encoded: text, options: Data.Base64DecodingOptions())
let decryptedData = cryptoLib.decrypt(decodedData, key: hashKey, iv: iv)
let decryptedString=String(data: decryptedData!,encoding:String.Encoding.utf8)
print("decryptedString \(decryptedString!)")
return decryptedString!
}