Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate an RSA public / private key pair

Tags:

swift

swift2

I'm looking for the simplest way to generate an RSA public / private key pair in swift I've been seeing a lot talk about how iOS doesn't support OpenSSL.

I simply need to generate the key pair and send the public key over to my server, the server will encrypt some data with the key and send it back over for my private key to decrypt. This is a one time transaction and I wont need the key anymore after that.

What is the simplest and lightest solution for this?

like image 641
Deekor Avatar asked Oct 08 '15 17:10

Deekor


People also ask

What is a RSA public private key pair?

An RSA key pair includes a private and a public key. The RSA private key is used to generate digital signatures, and the RSA public key is used to verify digital signatures. The RSA public key is also used for key encryption of DES or AES DATA keys and the RSA private key for key recovery.

How are public private key pairs generated?

Public keys are created using an asymmetric algorithm, which pairs the public key with an associated private key. The most common algorithms used to generate public keys are Rivest-Shamir-Adleman, elliptic curve cryptography and Digital Signature Algorithm.


1 Answers

This Github repo - Heimdall, should help you with generating keys and encrypting your data.

Example usage:

if let heimdall = Heimdall(tagPrefix: "com.example") {
    let testString = "This is a test string"
// Encryption/Decryption
if let encryptedString = heimdall.encrypt(testString) {
    println(encryptedString) // "cQzaQCQLhAWqkDyPoHnPrpsVh..."

    if let decryptedString = heimdall.decrypt(encryptedString) {
        println(decryptedString) // "This is a test string"
    }
}

// Signatures/Verification
if let signature = heimdall.sign(testString) {
    println(signature) // "fMVOFj6SQ7h+cZTEXZxkpgaDsMrki..."
    var verified = heimdall.verify(testString, signatureBase64: signature)
    println(verified) // True

    // If someone meddles with the message and the signature becomes invalid
    verified = heimdall.verify(testString + "injected false message",
                                signatureBase64: signature)
    println(verified) // False
}

Encrypting data with own public key:

The swift-rsautils by btnguyen2k Utils should help you with encrypting your data with your own public key. Its really simple to use.

How to use:

First just drag and drop the RSAUtils.swift file to your project.

And thats it!

Encrypting basic string:

let PUBLIC_KEY = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJh+/sdLdlVVcM5V5/j/RbwM8SL++Sc3dMqMK1nP73XYKhvO63bxPkWwaY0kwcUU40+QducwjueVOzcPFvHf+fECAwEAAQ=="

let sampleText:String = "WHATS UP"

let encrypted:NSData? = RSAUtils.encryptWithRSAPublicKey(sampleText.dataUsingEncoding(NSUTF8StringEncoding)!, pubkeyBase64: PUBLIC_KEY, keychainTag: "yourdomain.com")

let encryptedDataText = encrypted!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions())

print(encryptedDataText)

This prints:

ML5S87dfDB6l1uHFcACm2IdkGHpDGPUaYoSNTO+83qcWYxTEddFeKhETIcqF5n67nRDL0lKi5XV9uEI7hGTyKA==
like image 113
Dejan Skledar Avatar answered Oct 12 '22 01:10

Dejan Skledar