Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use #25

Closed
jookovjook opened this issue Jan 22, 2018 · 2 comments
Closed

How to use #25

jookovjook opened this issue Jan 22, 2018 · 2 comments

Comments

@jookovjook
Copy link

Hello, everyone!
Can you please help me to use SwCrypt?

The task is to:

  1. Generate public and private keys
  2. Covert them to String format
  3. Encrypt some other string with a public key and receive another string as result of encryption
  4. Decrypt string received at the previous step with the private key

To be honest, I'm new in cryptography and know a little about it's principles. Are actions mentioned above possible?

I've read all the issues and still can't get how to use the library.

  1. I've generated public and private keys with.
    let (privateKey, publicKey) = try! CC.RSA.generateKeyPair(512)
    let privKeyStr = privateKey.base64EncodedString()
    let pubKeyStr = publicKey.base64EncodedString()

  2. I have a string which i want to encode and than decode
    let testString: String = "Test string"

  3. Trying to encode it:
    let testStringData = testString.data(using: .utf8)!
    let pubKeyData = pubKeyStr.data(using: .utf8)!
    let tagStr = "L" //still can't get what should I use there
    let tag = tagStr.data(using: .utf8)!
    let encryptedData = try! CC.RSA.encrypt(testStringData, derKey: pubKeyData, tag: tag, padding: .oaep, digest: .sha1)

  4. Get an error:

CCError: decodeError (-4304)

Can you please help me to use the library?
Thanks!

@themuser
Copy link

I've wrote test code just like this.
Hope you've already dealt with this though.

import XCTest
import Foundation
@testable import myApp

let keyPair = try? myAppSwiftTests.createKeyPair(2048)
let plainText = "Hello, world!"

class myAppSwiftTests: XCTestCase {
    
    override func setUp() {
        super.setUp()
        self.continueAfterFailure = false
    }
    
    override func tearDown() {
        super.tearDown()
    }
    
    static func createKeyPair(_ size: Int) throws -> (Data, Data) {
        return try CC.RSA.generateKeyPair(size)
    }
    
    func testRsaKeyPairGeneration(){
        self.measure{
            let (priv, pub) = keyPair!
            XCTAssertNotNil(priv)
            XCTAssertNotNil(pub)
        }
    }
    
    func testRsaPublicKeyEncryption(){
        let (_, publicKey) = keyPair!
        let testData = plainText.data(using: String.Encoding.utf8)!
        
        var encrypted: Data? = nil
        self.measure {
            encrypted = try? CC.RSA.encrypt(testData, derKey: publicKey, tag: Data(), padding: .pkcs1, digest: .sha256)
        }
        XCTAssertNotNil(encrypted)
    }
    
    func testRsaPrivateKeyDecryption(){
        let (privateKey, publicKey) = keyPair!
        let testData = plainText.data(using: String.Encoding.utf8)!
        
        var encrypted: Data? = nil
        var decrypted: (Data, Int)? = nil
        self.measure {
            encrypted = try? CC.RSA.encrypt(testData, derKey: publicKey, tag: Data(), padding: .pkcs1, digest: .sha256)
            decrypted = try? CC.RSA.decrypt(encrypted!, derKey: privateKey, tag: Data(), padding: .pkcs1, digest: .sha256)
        }
        XCTAssertNotNil(encrypted)
        XCTAssertNotNil(decrypted)
        XCTAssertNotNil(decrypted!.0)
        XCTAssertEqual(String(data: decrypted!.0, encoding: .utf8), plainText)
        
    }
}

@soyersoyer
Copy link
Owner

let pubKeyData = pubKeyStr.data(using: .utf8)! is wrong.
You should use the publicKey directly, not the base64 representation.
let pubKeyData = publicKey

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants