3

I am trying to encrypt the AES key with RSAEncryption, I have created a Function for both, AES Encryption will first encrypt the DATA and then call the RSAEncryption to encrypt the AES Encryption Key itself and then send it to the server for response.

Below is what i tried, right now i am just checking if the public key that i am creating on runtime to encrypt matches with the public key that it should be, when i run the program, i print the pem representation of public key corresponding to the modulus and exponent, the prefix of the RSA public key is missing 32 characters from it.

CODE:

import Foundation

import Alamofire

import SwiftyRSA

class Encryption {

private let rsaModulus: String = ""

private let rsaExponent: String = ""


public func rsaEncryption(password: String, data: Data, modulus: String, exponent: String) -> String? {

            if let modData = Data(base64Encoded: modulus),
            let expData = Data(base64Encoded: exponent),
            let keyData = Rsa.generatePublicKey(withModulus: modData, exponent: expData) {

            let keyDict:[NSObject:NSObject] = [
                kSecAttrKeyType: kSecAttrKeyTypeRSA,
                kSecAttrKeyClass: kSecAttrKeyClassPublic,
                kSecAttrKeySizeInBits: NSNumber(value: 2048),
                kSecReturnPersistentRef: true as NSObject
            ]

            let publicKey = SecKeyCreateWithData(keyData as CFData, keyDict as CFDictionary, nil)
            let pub = try! PublicKey(reference: publicKey!)
            let pem = try! pub.pemString() 

            let clear = try! ClearMessage(string: "Clear Text", using: .utf8)
            let encrypted = try! clear.encrypted(with: pub, padding: .PKCS1)

            print("Encrypted AES Key: \(encrypted.base64String)")
            print(separator: "\n")
            print(pem) //Print pem to check public key generated

        }
        return "NO"
    }
}

Result Output :

Encrypted AES Key: /.../

-----BEGIN RSA PUBLIC KEY-----
MIIBCgKCAQEAvvJwQMvjUI7DDnC2PYwGNGJrkq6acdkjIW1+WznI68FyfVWb15Gz
hiJ7IVVbPo1Rphkbr0Gs8vrkunwBxVIb1wCjiiwqdoR7EUvUHrk5WkNcSoNEu2l0
VnRVuFe+XTKrnQsgfRy2TzyW3eG2kOkQBHWwJCQT6pFOwLXhY4JwSBzdhPIUk7HM
20ntqmamMnKMEmEC2N+qOfBx2hKVv2s7bwGBI8NZoIdt6dbhFXgv5NWN+U9Mx3kd
mXpjz7CaGgxfbBhTy6SfJQzN0Exfv4VGxOHRDkO0Mmu/d2VIeT4Q8lM53YZWuyCm
/pZ4XCgi9Z/2ZmQmCszSOc2/495JQcV9ZQIDAQAB

-----END RSA PUBLIC KEY-----

Actual Output of public key should be :

-----BEGIN PUBLIC KEY-----
**MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A**MIIBCgKCAQEAvvJwQMvjUI7DDnC2PYwG
NGJrkq6acdkjIW1+WznI68FyfVWb15GzhiJ7IVVbPo1Rphkbr0Gs8vrkunwBxVIb
1wCjiiwqdoR7EUvUHrk5WkNcSoNEu2l0VnRVuFe+XTKrnQsgfRy2TzyW3eG2kOkQ
BHWwJCQT6pFOwLXhY4JwSBzdhPIUk7HM20ntqmamMnKMEmEC2N+qOfBx2hKVv2s7
bwGBI8NZoIdt6dbhFXgv5NWN+U9Mx3kdmXpjz7CaGgxfbBhTy6SfJQzN0Exfv4VG
xOHRDkO0Mmu/d2VIeT4Q8lM53YZWuyCm/pZ4XCgi9Z/2ZmQmCszSOc2/495JQcV9
ZQIDAQAB
-----END PUBLIC KEY-----

Characters between stars are missing from the result output.

Anshul Kapoor
  • 39
  • 1
  • 6
  • Rsa.generatePublicKey gave this error : Use of unresolved identifier 'Rsa'. what is the type of "Rsa" ? – utarid Jul 28 '18 at 14:21

0 Answers0