superk589 / RijndaelSwift

A simple Rijndael implementation in Swift
MIT License
10 stars 2 forks source link

How can I put encrypted string as cipherData into decrypt function param? #2

Closed dio-brando closed 5 years ago

dio-brando commented 5 years ago

Hello, I'm a Korean developer, and trying to using Rijndael256 to make a App for IOS Swift. I already completed for android and Rijndael256 works perfectly on android and server both.

In encryption, your codes works perfectly and returns same data with server. If I input string as "ReturnValue",

let data1 = "ReturnValue".unicodeScalars.filter { $0.isASCII }.map { String(format: "%X", $0.value) }.joined().hexadecimal()!
let enc = r?.encrypt(data: data1, blockSize: 32, iv: iv)!;
print(enc!.base64EncodedString())

it returns "I8ELpKcziXiPCg3ixURYaw3XGVDQ1JJCarM+T6EFChQ=" as well as server. Thank you

but in decryption, it seems not work as like android or server. I've expected that

let data2 = "I8ELpKcziXiPCg3ixURYaw3XGVDQ1JJCarM+T6EFChQ=".unicodeScalars.filter { $0.isASCII }.map { String(format: "%X", $0.value) }.joined().hexadecimal()!
let dec = r?.decrypt(data:data2 , blockSize: 32, iv: iv)

would return "ReturnValue", but dec is nil because data length % blockSize is not 0. data2 char length is 44 as you can see.

When I put let enc(Data) into decrypt data param, it works well and let dec gives base64encoded data can be decoded as "ReturnValue\0\0\0\0\0\0\0\0\0". But if I convert a encrypted string as a data(let data2), data length does matter.

So, if i delete or add some chars to match 32 or 64 or 96(added 'AAAAAAA...' in the end between '='), dec is not nil but returns "LB1SmZk/18plf7ZzfFI/DT6NjzJ6Ox3Kry3rwOYmKB/81G83TEGjZomtK9jkPdSKSwUVgH8cXCCfqockVoePGQ==" and this string returns null when decode as base64 encoded. I couldnt think data length should match with block size because I cant control encrypted data length. is there any miss in my code? How can I convert encoded string to proper Data class to decrypt?

superk589 commented 5 years ago
let data2 = "I8ELpKcziXiPCg3ixURYaw3XGVDQ1JJCarM+T6EFChQ=".unicodeScalars.filter { $0.isASCII }.map { String(format: "%X", $0.value) }.joined().hexadecimal()!
let dec = r?.decrypt(data:data2 , blockSize: 32, iv: iv)

I think these lines should be changed to

let data2 = Data(base64Encoded: "I8ELpKcziXiPCg3ixURYaw3XGVDQ1JJCarM+T6EFChQ=")
let dec = r?.decrypt(data:data2 , blockSize: 32, iv: iv)
let plain = dec?.split(separator: 0)[0]

Then the variable plain should equal to "ReturnValue" in ASCII encoded data

dio-brando commented 5 years ago
let data2 = "I8ELpKcziXiPCg3ixURYaw3XGVDQ1JJCarM+T6EFChQ=".unicodeScalars.filter { $0.isASCII }.map { String(format: "%X", $0.value) }.joined().hexadecimal()!
let dec = r?.decrypt(data:data2 , blockSize: 32, iv: iv)

I think these lines should be changed to

let data2 = Data(base64Encoded: "I8ELpKcziXiPCg3ixURYaw3XGVDQ1JJCarM+T6EFChQ=")
let dec = r?.decrypt(data:data2 , blockSize: 32, iv: iv)
let plain = dec?.split(separator: 0)[0]

Then the variable plain should equal to "ReturnValue" in ASCII encoded data

Thanks a lot! It works perfectly! You are my savior!