Creating a pair of RSA public and private keys

I am looking for the easiest way to create a RSA public / private key pair in swift . I talked a lot about how iOS does not support OpenSSL .

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

What is the simplest and easiest solution for this?

+6
source share
2 answers

This Github repo - Heimdall should help you generate keys and encrypt your data.

Usage example:

 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 } 

Data encryption using your own public key:

swift-rsautils btnguyen2k Utilities should help you encrypt your data using your own public key. Its really easy to use.

How to use:

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

And here it is!

Base line encryption:

 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) 

Fingerprints:

 ML5S87dfDB6l1uHFcACm2IdkGHpDGPUaYoSNTO+83qcWYxTEddFeKhETIcqF5n67nRDL0lKi5XV9uEI7hGTyKA== 
+5
source

This is the easiest way to create RSA key pairs in Swift using SecKeyGeneratePair :

  var statusCode: OSStatus var publicKey: SecKey? var privateKey: SecKey? let publicKeyAttr: [NSObject: NSObject] = [kSecAttrIsPermanent:true, kSecAttrApplicationTag:"publicTag".dataUsingEncoding(NSUTF8StringEncoding)!] let privateKeyAttr: [NSObject: NSObject] = [kSecAttrIsPermanent:true, kSecAttrApplicationTag:"privateTag".dataUsingEncoding(NSUTF8StringEncoding)!] var keyPairAttr = [NSObject: NSObject]() keyPairAttr[kSecAttrKeyType] = kSecAttrKeyTypeRSA keyPairAttr[kSecAttrKeySizeInBits] = 512 keyPairAttr[kSecPublicKeyAttrs] = publicKeyAttr keyPairAttr[kSecPrivateKeyAttrs] = privateKeyAttr statusCode = SecKeyGeneratePair(keyPairAttr, &publicKey, &privateKey) if statusCode == noErr && publicKey != nil && privateKey != nil { printMessage = "Key pair generated OK" print("Public Key: \(publicKey!)") print("Private Key: \(privateKey!)") } else { printMessage = "Error generating key pair: \(statusCode)" } 
+4
source

All Articles