1

I have a string of data that I want to encrypt in AES 128 and the encrypted data should be in binary. I have tried using CryptoSwith to do so. But the issue I am getting is that the encryption code that I found from an online help converts in into HexString. I tried looking for the conversion to binary but couldn't find a working solution. The code I am using is below :

func encrypt(data: String) -> String?  {
        if let aes = try? AES(key: "0101010101010101", iv: "0000000000000000"),
            let encrypted = try? aes.encrypt(Array(data.utf8)) {
            return encrypted.toHexString()
        }
        return 
    }

Would really appreciate the help.

  • 1
    Have you tried removing the 'toHexString' call? – Alejandro May 16 '18 at 12:33
  • Yes, I did. But it shows error " Cannot convert return expression of type 'Array' to return type 'String?' " – Muhammad Jamaal May 16 '18 at 12:39
  • You can check my answer from some time ago https://stackoverflow.com/a/46479667/1433612 – Au Ris May 16 '18 at 12:40
  • 1
    @MuhammadJamaal Then that error message prompts to change the return value from string to `Array`, which makes sense since then you wouldn't be returning a string anymore. You may also need to change the callers to reflect that. – Alejandro May 16 '18 at 12:44

1 Answers1

1

Assuming you meant something like this?:

let binary = encrypted.map {
    let binary = String($0, radix: 2)
    let padding = String(repeating: "0", count: 8 - binary.count)
    return padding + binary
}.joined()

Which prints the whole Byte array as a sequence of zeros (0) and ones (1):

100101010101011010101010101010100011010101010101

Your method would then look like:

func encrypt(data: String) -> String?  {
    if let aes = try? AES(key: "0101010101010101", iv: "0000000000000000"),
        let encrypted = try? aes.encrypt(Array(data.utf8)) {

        let binary = encrypted.map {
            let binary = String($0, radix: 2)
            let padding = String(repeating: "0", count: 8 - binary.count)
            return padding + binary
        }.joined()

        return binary
    }
    return nil
}
Dejan Skledar
  • 11,280
  • 7
  • 44
  • 70
  • Thank you very much. The function looks great now. – Muhammad Jamaal May 16 '18 at 12:41
  • 1
    This is a questionable solution because `String(byte, radix: 2)` does not print leading zeros. As an example, both `[0x05, 0x01]` and `[0x02, 0x03]` would be printed as `1011` – Martin R May 16 '18 at 12:47
  • 1
    You can shorten this somewhat by using the following: `let binary = encrypted.map { String($0, radix: 2) }.joined()` - saves dropping into a larger closure. – Jacob King May 16 '18 at 12:47
  • @MartinR Interesting, I hadn't considered that. In which case, this solution isn't reliable in the slightest and could skew your data considerably. For encryption, that's no good. – Jacob King May 16 '18 at 12:48
  • Indeed.. Totally forgot about that. – Dejan Skledar May 16 '18 at 12:49
  • @MuhammadJamaal I updated the answer adding the required zero padding. MartinR, thank you for the heads-up. JacobKing, nice solution for the shortened code. – Dejan Skledar May 16 '18 at 13:04
  • @DejanSkledar I am getting another issue here. Does this encryption with binary conversion generate a BitArray? – Muhammad Jamaal May 23 '18 at 07:00
  • This should create a bit array. I'm not sure what you want to achieve. You should be more specific about your problems and current solutions. – Dejan Skledar May 28 '18 at 06:41