Created
September 6, 2023 15:43
-
-
Save damodarnamala/df58bda13693f9245002a5f2d6797664 to your computer and use it in GitHub Desktop.
Encryption in swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import CryptoKit | |
import Security | |
import SwiftECC | |
import ASN1 | |
import BigInt | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let domain = Domain.instance(curve: .EC256r1) | |
let (publicKey, privateKey) = domain.makeKeyPair() | |
print("\n public der \n", publicKey.der) | |
print("\n private der \n ",privateKey.der) | |
print("\n public pem \n", publicKey.pem) | |
print("\n private pem \n ",privateKey.pem) | |
let text = "This is awesome!" | |
do { | |
let pubKey = try ECPublicKey(pem: publicKey.pem) | |
let privKey = try ECPrivateKey(pem: privateKey.pem) | |
let encryptedData = pubKey.encrypt(msg: text.data(using: .utf8)!, cipher: .AES256) | |
let decryptedData = try privKey.decrypt(msg: encryptedData, cipher: .AES256) | |
print("\n EncryptedData \n", encryptedData.base64EncodedString()) | |
print("\n Decrypted \n", String(data: decryptedData, encoding: .utf8)!) | |
} catch { | |
print("\(error)") | |
} | |
// OWN | |
let converter = KeyPairConverter() | |
let privateKeyPEM = converter.privateKeyToPEM() | |
let publicKeyPEM = converter.publicKeyToPEM() | |
let privateKeyDER = converter.privateKeyToDER() | |
let publicKeyDER = converter.publicKeyToDER() | |
print("Private Key (PEM):\n\(privateKeyPEM)") | |
print("Public Key (PEM):\n\(publicKeyPEM)") | |
print("Private Key (DER):\n\(privateKeyDER.hexEncodedString())") | |
print("Public Key (DER):\n\(publicKeyDER.hexEncodedString())") | |
let encryptor = ECCKeyPairEncryptor() | |
do { | |
let originalData = "Sensitive data".data(using: .utf8)! | |
// Encrypt the data | |
let encryptedData = try encryptor.encrypt(data: originalData) | |
// Decrypt the data | |
let decryptedData = try encryptor.decrypt(encryptedData: encryptedData) | |
let originalString = String(data: originalData, encoding: .utf8)! | |
let decryptedString = String(data: decryptedData, encoding: .utf8)! | |
print("Original Data: \(originalString)") | |
print("Decrypted Data: \(decryptedString)") | |
} catch { | |
print("Error: \(error)") | |
} | |
} | |
} | |
extension Data { | |
func hexEncodedString() -> String { | |
return map { String(format: "%02hhx", $0) }.joined() | |
} | |
} | |
class KeyPairConverter { | |
private let privateKey: P256.KeyAgreement.PrivateKey | |
private let publicKey: P256.KeyAgreement.PublicKey | |
init() { | |
// Generate a new key pair | |
let keyPair = P256.KeyAgreement.PrivateKey() | |
self.privateKey = keyPair | |
self.publicKey = keyPair.publicKey | |
} | |
func privateKeyToPEM() -> String { | |
let privateKeyData = privateKey.rawRepresentation | |
return KeyPairConverter.privateKeyToPEM(privateKeyData) | |
} | |
func publicKeyToPEM() -> String { | |
let publicKeyData = publicKey.rawRepresentation | |
return KeyPairConverter.publicKeyToPEM(publicKeyData) | |
} | |
func privateKeyToDER() -> Data { | |
return privateKey.rawRepresentation | |
} | |
func publicKeyToDER() -> Data { | |
return publicKey.rawRepresentation | |
} | |
static func privateKeyToPEM(_ privateKeyData: Data) -> String { | |
let base64PrivateKey = privateKeyData.base64EncodedString() | |
let pemPrivateKey = """ | |
-----BEGIN PRIVATE KEY----- | |
\(base64PrivateKey) | |
-----END PRIVATE KEY----- | |
""" | |
return pemPrivateKey | |
} | |
static func publicKeyToPEM(_ publicKeyData: Data) -> String { | |
let base64PublicKey = publicKeyData.base64EncodedString() | |
let pemPublicKey = """ | |
-----BEGIN PUBLIC KEY----- | |
\(base64PublicKey) | |
-----END PUBLIC KEY----- | |
""" | |
return pemPublicKey | |
} | |
} | |
class ECCKeyPairEncryptor { | |
private let privateKey: P256.KeyAgreement.PrivateKey | |
private let publicKey: P256.KeyAgreement.PublicKey | |
init() { | |
// Generate a new key pair | |
let keyPair = P256.KeyAgreement.PrivateKey() | |
self.privateKey = keyPair | |
self.publicKey = keyPair.publicKey | |
} | |
func encrypt(data: Data) throws -> Data { | |
let sharedSecret = try privateKey.sharedSecretFromKeyAgreement(with: publicKey) | |
// Use the shared secret to derive an encryption key | |
let encryptionKey = sharedSecret.hkdfDerivedSymmetricKey(using: SHA256.self, | |
salt: Data(), sharedInfo: Data(), | |
outputByteCount: 32) | |
// Encrypt the data | |
let sealedBox = try AES.GCM.seal(data, using: encryptionKey) | |
return sealedBox.combined! | |
} | |
func decrypt(encryptedData: Data) throws -> Data { | |
let sharedSecret = try privateKey.sharedSecretFromKeyAgreement(with: publicKey) | |
// Use the shared secret to derive the same encryption key | |
let encryptionKey = sharedSecret.hkdfDerivedSymmetricKey(using: SHA256.self, | |
salt: Data(), sharedInfo: Data(), | |
outputByteCount: 32) | |
// Decrypt the data | |
let sealedBox = try AES.GCM.SealedBox(combined: encryptedData) | |
return try AES.GCM.open(sealedBox, using: encryptionKey) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment