A simple way of doing both symmetric and asymmetric crypto without the headache

Overview

Simple Swift Crypto

I needed a simple way of doing both symmetric and asymmetric crypto without the headache. I also didn't need to store it in the Keychain. So I made this.

Works on MacOS, iOS, WatchOS and TvOS.

Example Usage

AES Symmetric Encryption and Decryption

let testString: String = "Hello World!"
let testData: Data = testString.data(using: .utf8)!

let aesPrivateKey: AES256Key = SimpleSwiftCrypto.generateRandomAES256Key()!
let encryptedData: Data = aesPrivateKey.encrypt(data: testData)!

let decryptedData: Data = aesPrivateKey.decrypt(data: encryptedData)!
let decryptedString: String = String(data: decryptedData, encoding: .utf8)! // "Hello World!"

RSA Asymmetric Encryption and Decryption

let testString: String = "Hello World!"
let testData: Data = testString.data(using: .utf8)!

let rsaPrivateKey: RSAKeyPair = SimpleSwiftCrypto.generateRandomRSAKeyPair()!
let rsaPublicKey: RSAPublicKey = rsaPrivateKey.extractPublicKey()
let encryptedData: Data = rsaPublicKey.encrypt(data: testData)!

let decryptedData: Data = rsaPrivateKey.decrypt(data: encryptedData)!
let decryptedString: String = String(data: decryptedData, encoding: .utf8)! // "Hello World!"

Exporting and Loading AES Secret Key

let aesPrivateKey: AES256Key = SimpleSwiftCrypto.generateRandomAES256Key()!
let aesPrivateKeyAsData: Data = aesPrivateKey.exportIvAndPrivateAES256Key()

// send `aesPrivateKeyAsData` to another device securely, then...  
let sameAesPrivateKeyFromData: AES256Key = .loadIvAndPrivateAES256Key(ivAndPrivateAES256Key: aesPrivateKeyAsData)!

Exporting and Loading RSA Public Key

let rsaPrivateKey: RSAKeyPair = SimpleSwiftCrypto.generateRandomRSAKeyPair()!
let rsaPublicKey: RSAPublicKey = rsaPrivateKey.extractPublicKey()
let rsaPublicKeyAsData: Data = rsaPublicKey.export()!


// send `rsaPublicKeyAsData` to another device, then...  
let sameRsaPublicKeyFromData: RSAPublicKey = .load(rsaPublicKeyData: rsaPublicKeyAsData)!

Add as Swift Package

// swift-tools-version:5.5
import PackageDescription

let package = Package(
    name: "Example",
    dependencies: [
        .package(name: "SimpleSwiftCrypto", url: "https://github.com/joehinkle11/SimpleSwiftCrypto.git", from: "1.0.0"),
    ],
    targets: [
        "SimpleSwiftCrypto"
    ]
)

Why did I make this?

I'm working on an IDE for iPad/iOS called App Maker Professional. A new feature I'm adding is multiplayer, and I don't want to have my users' files on the server. So I made this to be able to encrypt their files locally on device so the server can't read them.

Contributions

If someone wants to add Keychain support, that'd be great. I'll take a look at a PR.

License

MIT

Sources

You might also like...
Simple Swift wrapper for Keychain that works on iOS, watchOS, tvOS and macOS.
Simple Swift wrapper for Keychain that works on iOS, watchOS, tvOS and macOS.

KeychainAccess KeychainAccess is a simple Swift wrapper for Keychain that works on iOS and OS X. Makes using Keychain APIs extremely easy and much mor

A simple Swift Keychain Wrapper for iOS, watchOS, and OS X.

Latch A simple Swift 2.0 Keychain Wrapper for iOS, watchOS 2, and OS X. Usage A proper example of how to use Latch can be seen in the tests. import La

Simple Objective-C wrapper for the keychain that works on Mac and iOS

SAMKeychain SAMKeychain is a simple wrapper for accessing accounts, getting passwords, setting passwords, and deleting passwords using the system Keyc

TouchEncryptedJson - Simple project that accepts an input and encrypts it with the TouchID on a Mac

TouchEncryptedJson Simple project that accepts an input and encrypts it with the

A simple wrapper for the iOS Keychain to allow you to use it in a similar fashion to User Defaults. Written in Swift.

SwiftKeychainWrapper A simple wrapper for the iOS / tvOS Keychain to allow you to use it in a similar fashion to User Defaults. Written in Swift. Prov

Simple command line to generate random password.

pwgen Simple command line to generate random password. ➜ Bootstrap pwgen n5aR[[email protected]@fj ➜ Bootstrap pwgen 32 f0)th54[wpX.Zf99nj

A really simple key-value wrapper for keychain.

PlainKeychain A really simple key-value wrapper for keychain. Features ✅ Key-value pairs using kSecClassGenericPassword. ❌ Internet passwords (kSecCla

Very simple swift wrapper for Biometric Authentication Services (Touch ID) on iOS.

SimpleTouch Very simple swift wrapper for Biometric Authentication Services (Touch ID) on iOS. Sample Project There is a SimpleTouchDemo target define

Simple class to check if app has been cracked, being debugged or enriched with custom dylib

iOS-App-Security-Class Simple class to check if iOS app has been cracked, being debugged or enriched with custom dylib and as well detect jailbroken e

Owner
Joe Hinkle
App and game developer. Working on https://appmakerios.com and https://crate.as
Joe Hinkle
Safe and easy to use crypto for iOS and macOS

Swift-Sodium Swift-Sodium provides a safe and easy to use interface to perform common cryptographic operations on macOS, iOS, tvOS and watchOS. It lev

Frank Denis 483 Jan 5, 2023
Swift cross-platform crypto library using CommonCrypto/libcrypto

BlueCryptor Swift cross-platform crypto library derived from IDZSwiftCommonCrypto. IMPORTANT NOTE: This release is NOT entirely source code compatible

Kitura 183 Oct 15, 2022
A wrapper for Apple's Common Crypto library written in Swift.

IDZSwiftCommonCrypto A Swift wrapper for Apple's CommonCrypto library. IDZSwiftCommonCrypto works with both CocoaPods and Cathage. For more details on

idz 472 Dec 12, 2022
Swift cross-platform crypto library using CommonCrypto/libcrypto

BlueCryptor Swift cross-platform crypto library derived from IDZSwiftCommonCrypto. IMPORTANT NOTE: This release is NOT entirely source code compatible

Kitura 183 Oct 15, 2022
CryptoTrack - iOS app using API to receive updated crypto prices

CryptoTrack Tools used: Swift Xcode by Jose Sahagun jsahagun.io.

Jose Sahagun 0 Jan 3, 2022
Valet lets you securely store data in the iOS, tvOS, or macOS Keychain without knowing a thing about how the Keychain works.

Valet Valet lets you securely store data in the iOS, tvOS, watchOS, or macOS Keychain without knowing a thing about how the Keychain works. It’s easy.

Square 3.8k Jan 4, 2023
Swift Package for fetching approximate user location without asking for their permission 👺 .

Earendil Swift Package for fetching approximate user location without asking for their permission ?? . Get their country, subregion or continent with

Przemysław Jabłoński 4 Sep 3, 2021
Simple, secure password and data management for individuals and teams

Padloc Simple, secure password and data management for individuals and teams (formerly known as Padlock). This repo is split into multiple packages: P

Padloc 2.1k Jan 8, 2023
PGPro can encrypt and decrypt messages as well as manage all your OpenPGP keys. It is free, simple and lightweight. Everything stays on your device. PGPro is made in Switzerland.

PGPro can encrypt and decrypt messages as well as manage all your OpenPGP keys. It is free, simple and lightweight. Everything stays on your device. P

Luca Näf 250 Jan 4, 2023
Simple and secure hashing in Swift with the SipHash algorithm

SipHash ⚠️ WARNING This package has been obsoleted by the Hasher type and the Hashable.hash(into:) requirement introduced in Swift 4.2. Using this pac

null 262 Dec 19, 2022