RNCryptor is not decrypting in iOS - ios

I am using RNCryptor in my application. I generated an encrypted string for a plain text using a password and when I try to decrypt the encrypted string using a password, it does not give me the original plain text instead it gave me some random string.
I guess I am missing something in string encoding/decoding but I am not sure what I am missing here.
Can you please guide me to fix this?
Code
func encrypt(plainText : String, password: String) -> String {
let data: Data = plainText.data(using: .utf8)!
let encryptedData = RNCryptor.encrypt(data: data, withPassword: password)
let encryptedString : String = encryptedData.base64EncodedString(options: Data.Base64EncodingOptions.lineLength76Characters)
return encryptedString
}
func decrypt(encryptedText : String, password: String) -> String {
do {
let data: Data = Data.init(base64Encoded: encryptedText, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)!
let decryptedData = try RNCryptor.decrypt(data: data, withPassword: password)
let decryptedString : String = decryptedData.base64EncodedString(options: Data.Base64EncodingOptions.lineLength76Characters)
return decryptedString
}
catch {
return "FAILED"
}
}
let plainText = "123456789"
let password = "ABCDEFGHIJ"
let encryptedText = self.encrypt(plainText: plainText, password: password)
let decryptedText = self.decrypt(encryptedText: encryptedText, password: password)
print("ENCRYPTED TEXT : \(encryptedText)")
print("DECRYPTED TEXT : \(decryptedText)")
ENCRYPTED TEXT : AwEsB6wlUSIJ31TAbaeAjVXP272zW89aa2rR9v6zYWwKUf6Hs5GSHekMKQT+n0vw6jMtjsQVhtzO
8AcqGpTLrQ9YR0PUS07P+8HboCp6Ge8UxQ==
DECRYPTED TEXT : MTIzNDU2Nzg5

Just try like below, its worked for me now.
func encrypt(plainText : String, password: String) -> String {
let data: Data = plainText.data(using: .utf8)!
let encryptedData = RNCryptor.encrypt(data: data, withPassword: password)
let encryptedString : String = encryptedData.base64EncodedString() // getting base64encoded string of encrypted data.
return encryptedString
}
func decrypt(encryptedText : String, password: String) -> String {
do {
let data: Data = Data(base64Encoded: encryptedText)! // Just get data from encrypted base64Encoded string.
let decryptedData = try RNCryptor.decrypt(data: data, withPassword: password)
let decryptedString = String(data: decryptedData, encoding: .utf8) // Getting original string, using same .utf8 encoding option,which we used for encryption.
return decryptedString ?? ""
}
catch {
return "FAILED"
}
}
Thanks.

In decrypt func you must decode Base64 first than RNC.

Related

IOS Decrypt string with CryptoSwift

I want to decrypt string in base64 and when i create string from decrypted data i get nil in my string.
Here's my code:
private func xd(url: String?) -> URL? {
guard let enryptedData = Data(base64Encoded: url ?? "") else { return nil }
let password: Array<UInt8> = "TW9iaWxueUNhdGVyaW5nRGVlcGxpbmtTZWNyZXQ=".bytes
let salt: Array<UInt8> = "ItyuwaSolSaltSalzToteSoSoutAsin".bytes
let iv = Data(count: 16)
Logger.debug("IV: \(iv)")
do {
let data = Padding.pkcs5.add(to: enryptedData.bytes, blockSize: AES.blockSize)
let key = try PKCS5.PBKDF2(password: password,
salt: salt,
keyLength: 32,
variant: .sha256).calculate()
let decryptedData = try AES(key: key, blockMode: CBC(iv: iv.bytes), padding: .pkcs5)
.decrypt(data)
let string = String(bytes: decryptedData, encoding: .utf8)
Logger.debug("Decrypted string: \(string ?? "")")
return URL(string: string ?? "")
} catch {
Logger.info("\(error)")
return nil
}

Issue in converting Base64string into string

I am trying to convert base64 string in to string format but I always get nil. My base64 string is "NWQwMDU2ZjhiZjRjYmI2M2MxZTI0NzQzNjAxMjMxMzAyMDh8NjAxMjMxMzAyMDh8NWQwMDU2Zjhi\nZjRjYmI2M2MxZTI0NzQzfDYwMTIzMTMwMjA4fG5hdmlnYXRpb25UZXN0MDA1fDIwMTktMDYtMjEg\nMDk6MzQ6MDB8MA==\n"
After decoding, Is suppose to look like this "5d0056f8bf4cbb63c1e2474360123130208|60123130208|5d0056f8bэ͌Ŕ٥ѥQشĂs3C�".
Here is my code event I tried to remove the "=\n" from string but not succeed.
func qrScanningSucceededWithCode(_ str: String?) {
scanTicketView.qrData = QRData(codeString: str)
let charsToRemove: Set<Character> = Set("=\n")
let newNumberCharacters = String(str!.filter { !charsToRemove.contains($0) })
let decodedString = String(data: Data(base64Encoded: newNumberCharacters)!, encoding: .utf8)!
print(decodedString)
}
This works:
let string = "NWQwMDU2ZjhiZjRjYmI2M2MxZTI0NzQzNjAxMjMxMzAyMDh8NjAxMjMxMzAyMDh8NWQwMDU2Zjhi\nZjRjYmI2M2MxZTI0NzQzfDYwMTIzMTMwMjA4fG5hdmlnYXRpb25UZXN0MDA1fDIwMTktMDYtMjEg\nMDk6MzQ6MDB8MA==\n"
let joined = string.replacingOccurrences(of: "\n", with: "")
if let data = Data(base64Encoded: joined) {
String(data: data, encoding: .utf8)
}

JSON data in http body contains special characters in my simple case

I have a very simple model struct Student which only has two properties firstName and lastName:
struct Student {
let firstName: String
let lastName: String
init(_ firstName: String, _ lastName: String) {
self.firstName = firstName
self.lastName = lastName
}
// Convert to json data
func toData() -> Data? {
var json = [String: Any]()
let mirror = Mirror(reflecting: self)
for child in mirror.children {
if let key = child.label?.trimmingCharacters(in: .whitespacesAndNewlines) {
json[key] = child.value
}
}
do {
return try JSONSerialization.data(withJSONObject: json, options: [])
} catch {
print("\(error.localizedDescription)")
}
return nil
}
}
As you see above, I created an toData() function which I use to convert model object to JSON data for my HTTP request body.
I create Student instance by:
let student = Student("John", "Smith")
I got Json Data by:
let jsonData = student.toData()
later I set to my URLRequest body by:
request.httpBody = jsonData!
However, backend team always see :
{\"firstName\":\"John\", \"lastName\":\"Smith\"}
But backend expect:
{"firstName":"John", "lastName":"Smith"}
I am sure it is not backend problem. Looks like something needs to improve in my toData() function. But I don't know what to do. Could someone help me?
Try this :
if let jsonString:String = String(data: jsonData!, encoding: String.Encoding.utf8) {
request.httpBody = jsonString.data(using: String.Encoding.utf8)
}
You can get rid of the extra backslashes by converting your struct to a dictionary manually.
I have tested below method using a dummy rest server (rest-server-dummy from npm) and there are no extra backslashes around the "" characters.
struct Student {
let firstName: String
let lastName: String
init(_ firstName: String, _ lastName: String) {
self.firstName = firstName
self.lastName = lastName
}
// Convert to json data
func toData() -> Data? {
var json = [String: Any]()
json["firstName"] = firstName
json["lastname"] = lastName
do {
return try JSONSerialization.data(withJSONObject: json, options: [])
} catch {
print("\(error.localizedDescription)")
}
return nil
}
}
I used this method to send the data to the dummy server running on localhost:
var request = URLRequest(url:URL(string: "http://localhost:8080/username")!)
request.httpMethod = "POST"
request.httpBody = student.toData()
URLSession.shared.dataTask(with: request, completionHandler: { data, response, error in
data
response
error
}).resume()
The contents of the output from the server log:
{
"lastname": "Smith",
"firstName": "John"
}

AES128 encryption in swift

I’ve an issue with AES-128 encryption. The encrypted string in iOS is different as compared to Android.
Below is android code :
public class Encryption {
private static final String ALGORITHM = "AES";
private static final String UNICODE_FORMAT = "UTF8";
public static String encryptValue(String valueToEnc) {
try {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encValue = c.doFinal(valueToEnc.getBytes());
String encryptedValue = new Base64().encode(encValue);
String urlEncodeddata = URLEncoder.encode(encryptedValue, "UTF-8");
return urlEncodeddata;
} catch (Exception e) {
}
return valueToEnc;
}
private static Key generateKey() throws Exception {
byte[] keyAsBytes;
keyAsBytes = "MySixteenCharKey".getBytes(UNICODE_FORMAT);
Key key = new SecretKeySpec(keyAsBytes, ALGORITHM);
return key;
}
}
Create string extension and use library CryptoSwift
// String+Addition.swift
import CryptoSwift
extension String {
func aesEncrypt(key: String) throws -> String {
var result = ""
do {
let key: [UInt8] = Array(key.utf8) as [UInt8]
let aes = try! AES(key: key, blockMode: ECB() , padding:.pkcs5) // AES128 .ECB pkcs7
let encrypted = try aes.encrypt(Array(self.utf8))
result = encrypted.toBase64()!
print("AES Encryption Result: \(result)")
} catch {
print("Error: \(error)")
}
return result
}
func aesDecrypt(key: String) throws -> String {
var result = ""
do {
let encrypted = self
let key: [UInt8] = Array(key.utf8) as [UInt8]
let aes = try! AES(key: key, blockMode: ECB(), padding: .pkcs5) // AES128 .ECB pkcs7
let decrypted = try aes.decrypt(Array(base64: encrypted))
result = String(data: Data(decrypted), encoding: .utf8) ?? ""
print("AES Decryption Result: \(result)")
} catch {
print("Error: \(error)")
}
return result
}
}
and to use
#IBAction func onBtnClicked(_ sender: Any) {
let value = "My value to be encrypted"
let key = "MySixteenCharKey"
print(key!)
let encryptedValue = try! value.aesEncrypt(key: key!)
print(encryptedValue)
}
For citation of this particular encryption code:
The 16 char length key implies AES-128
The code is without iVector, This implies ECB mode
Using padding as pkcs5 or pkcs7 did not made any difference in my case
Try this:
func encryptValue(stringToEncrypt:String) -> String{
var encryptedString: String = ""
let value = "MySixteenCharKey"
let input: Array<UInt8> = Array(stringToEncrypt.utf8)
let key: Array<UInt8> = Array("MySixteenCharKey".utf8)
do {
let encrypted = try AES(key: key, blockMode: .ECB, padding: PKCS7()).encrypt(input)
let base64 = encrypted.toBase64()
encryptedString = base64
} catch {
print(error)
}
return encryptedString
}
Create an extension of String and use this below function for encrypting and decrypting.
extension String {
//MARK: - Encrypt AES Method
/// This method will encrypt and return Cipher string
func aesEncrypt(key: String = "fqJfdzGDvfwbedsKSUGty3VZ9taXxMVw", iv: String = "1234567890123456") -> String {
if let data = self.data(using: String.Encoding.utf8) {
do {
let enc = try AES(key: key, iv: iv, blockMode: .CBC, padding: PKCS7()).encrypt(data.bytes)
let encData = Data(bytes: enc, count: Int(enc.count))
let base64String: String = encData.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0));
return base64String
}
catch let error {
print(error.localizedDescription)
return ""
}
}
return ""
}
//MARK: - Decrypt AES Method
/// This method will decrypt the Cipher string
func aesDecrypt(key: String = "fqJfdzGDvfwbedsKSUGty3VZ9taXxMVw", iv: String = "1234567890123456") -> String {
if let data = Data(base64Encoded: self, options: Data.Base64DecodingOptions.init(rawValue: 0)) {
do {
let dec = try AES(key: key, iv: iv, blockMode: .CBC, padding: PKCS7()).decrypt(data.bytes)
let decData = Data(bytes: dec, count: Int(dec.count))
let result = String(data: decData, encoding: .utf8)
return result ?? ""
}
catch let error {
print(error.localizedDescription)
return ""
}
}
return ""
}
}

serialization and de-serialization of an object in Swift3

I want to know if there is a possibility of make a serialization and de-serialization of an object in swift 3.
I have an object like this:
class Credentials{
var username:String;
var password:String;
init(){
username = "";
password = "";
}
}
I want to transform this class into a json (and vice versa ) to send it through HTTP post.
I don't want to use Third party libraries.
Thanks for the answer.
First of all it's not necessary to use a class, a struct is sufficient.
Simple solution with an failable initializer expecting a JSON string and a variable jsonRepresentation
struct Credentials {
var username = ""
var password = ""
init(username: String, password:String) {
self.username = username
self.password = password
}
init?(json : String) {
guard let data = json.data(using: .utf8),
let jsonDict = try? JSONSerialization.jsonObject(with: data, options: []) as? [String:String],
let username = jsonDict?["username"],
let password = jsonDict?["password"] else { return nil }
self.username = username
self.password = password
}
var jsonRepresentation : String {
let jsonDict = ["username" : username, "password" : password]
if let data = try? JSONSerialization.data(withJSONObject: jsonDict, options: []),
let jsonString = String(data:data, encoding:.utf8) {
return jsonString
} else { return "" }
}
}

Resources