RSA encrypt and decrypt in Swift

Related tags

Security MZRSA_Swift
Overview

MZRSA_Swift

MZRSA_Swift是一个轻量级框架,框架功能包含RSA加密/解密Data、RSA加密/解密String,支持字符串密钥和证书密钥

公钥加密&私钥解密(字符串密钥)

  • 代码示例
let PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArdklK4kIsOMuxTZ8jG1PRPfXqSDmaCIQ+xEpIRSssQ6jiuvhYZTMUbV22osgtivuyKdnHm+cvzGuZCSB8QFyCcM7l09HZVs0blLkrBAU5CVSv+6BzPQTVJytoi/VO4mlf6me1Y9bXWrrPw1YtC1mnB2Ix9cuaxOLpucglfGbUaGEigsUZMTD2vuEODN5cJi39ap+G9ILitbrnt+zsW9354pokVnHw4Oq837Fd7ZtP0nAA5F6nE3FNDGQqLy2WYRoKC9clDecD8T933azUD98b5FSUGzIhwiuqHHeylfVbevbBW91Tvg9s7vUMr0Y2YDpEmPAf0q4PlDt8QsjctT9kQIDAQAB"

let PRIVATE_KEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCt2SUriQiw4y7FNnyMbU9E99epIOZoIhD7ESkhFKyxDqOK6+FhlMxRtXbaiyC2K+7Ip2ceb5y/Ma5kJIHxAXIJwzuXT0dlWzRuUuSsEBTkJVK/7oHM9BNUnK2iL9U7iaV/qZ7Vj1tdaus/DVi0LWacHYjH1y5rE4um5yCV8ZtRoYSKCxRkxMPa+4Q4M3lwmLf1qn4b0guK1uue37Oxb3fnimiRWcfDg6rzfsV3tm0/ScADkXqcTcU0MZCovLZZhGgoL1yUN5wPxP3fdrNQP3xvkVJQbMiHCK6ocd7KV9Vt69sFb3VO+D2zu9QyvRjZgOkSY8B/Srg+UO3xCyNy1P2RAgMBAAECggEAInVN9skcneMJ3DEmkrb/5U2yw2UwBifqcbk/C72LVTTvmZOTgsH5laCARGUbQMCIfeEggVniGcuBI3xQ/TIqJmE6KI2gOyjOxadMiAZ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4ytd3Mm/6CNbrg7xux2M4RDQmN//1UOY"

var ss = "fajsdajdadj"
MZLog(ss)
ss = MZRSA.encryptString(ss, publicKey: PUBLIC_KEY)!
MZLog(ss)
ss = MZRSA.decryptString(ss, privateKey: PRIVATE_KEY)!
MZLog(ss)
  • 运行结果
在文件(ViewController.swift)第(198)行
 fajsdajdadj


在文件(ViewController.swift)第(200)行
 Xj2jXTCsipJ3fvOjwS7mFs3xHV7aNmQe7Q0yjPC4Ai5iX6ymkvmQzgr9NlyOQIKq
pUex94ORmT5RuCL8jMt++y/g9NiVuS4mCdlb1m3ZwKyABTXTOxncK4QdFFXwy//F
XjOoNRgniq/RSvdO32qiPNCjGECODPMC4sJDsUkWX9SeeBPGIVqO/yorVD+s5Q1Z
1R6l6ju5u0r4cnBDEibN4/ZNeH9vAy7CwyJuThz2BNPmIedNofbH7c5+BbbxVS4H
2czR2O+uarPEXTZ1cVnCLAs71y1dg80ckqeSAVLBsBB4FcqcPUqCHm9MwatdLW3w
pBsk3WVfCxgMoJpcCzqF5Q==


在文件(ViewController.swift)第(202)行
 fajsdajdadj

私钥加密&公钥解密(字符串密钥)

  • 代码示例
var zz = "skmkfmaksfmaksfaadddfasdadfa"
MZLog(zz)
zz = MZRSA.encryptString(zz, privateKey: PRIVATE_KEY)!
MZLog(zz)
zz = MZRSA.decryptString(zz, publicKey: PUBLIC_KEY)!
MZLog(zz)
  • 运行结果
在文件(ViewController.swift)第(205)行
 skmkfmaksfmaksfaadddfasdadfa


在文件(ViewController.swift)第(207)行
 ACj8OCXFLfLWM58we5Vdr1jvWDXbG/X0rm7cG6zoilWjIXXvl5Fj6XNVVpkgpSuh
1vfU85b4RAIWmdBNXfvm4OcyPbNluYKtkFFTKlaZmPb4gm32dkZeGtwDx/NumzCE
mUAOkde0AAlVGM6+u1JJLZmD3FLL7xYdi4d+wuWVuz6WgLFZcqSrn8IVH7+ERI+l
cTGuse6wruv0SIYzzN5YGor3ViXpr2FEuvGryM8W61oPQUiJ6VpKGbR+0Y5Cz3im
ElJ/1549SgDR91LvLG1eQipfJvCxd/3TwCJ2LARB+BGJK6g+gepKSFdJnpeWhQAJ
SE6il79JYl3MUcTRxIT0rg==


在文件(ViewController.swift)第(209)行
 skmkfmaksfmaksfaadddfasdadfa

公钥加密&私钥解密(证书密钥)

var hh = "zwefdewfqfewfe"
MZLog(hh)
hh = MZRSA.encryptString(hh, publicKeyPath: Bundle.main.path(forResource: "public_key", ofType: "der")!)!
MZLog(hh)
hh = MZRSA.decryptString(hh, privateKeyPath: Bundle.main.path(forResource: "private_key", ofType: "p12")!)!
MZLog(hh)

私钥加密&公钥解密(证书密钥)

var kk = "sddskdksflss"
MZLog(kk)
kk = MZRSA.encryptString(kk, privateKeyPath: Bundle.main.path(forResource: "private_key", ofType: "p12")!)!
MZLog(kk)
kk = MZRSA.decryptString(kk, publicKeyPath: Bundle.main.path(forResource: "public_key", ofType: "der")!)!
MZLog(kk)
You might also like...
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

A wrapper to make it really easy to deal with iOS, macOS, watchOS and Linux Keychain and store your user's credentials securely.

A wrapper (written only in Swift) to make it really easy to deal with iOS, macOS, watchOS and Linux Keychain and store your user's credentials securely.

CryptoSwift is a growing collection of standard and secure cryptographic algorithms implemented in Swift
CryptoSwift is a growing collection of standard and secure cryptographic algorithms implemented in Swift

CryptoSwift Crypto related functions and helpers for Swift implemented in Swift. (#PureSwift) Note: The master branch follows the latest currently rel

A framework for the JOSE standards JWS, JWE, and JWK written in Swift.

JOSESwift is a modular and extensible framework for the JOSE standards JWS, JWE, and JWK written in Swift. 💡 Please note that this implementation of

CCCryptor (AES encryption) wrappers for iOS and Mac in Swift. -- For ObjC, see RNCryptor/RNCryptor-objc

RNCryptor Cross-language AES Encryptor/Decryptor data format. The primary targets are Swift and Objective-C, but implementations are available in C, C

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

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

Swift OpenSSL for OS X and Linux

OpenSSL OpenSSL for Swift 3.0. Installation import PackageDescription let package = Package( dependencies: [ .Package(url: "https://github.com/Zew

Owner
Learning a little everyday
null
Cloak Swift - a tool and Tuist plugin to encrypt secrets and then pass them in an obfuscated form into applications

This is Cloak Swift - a tool and Tuist plugin to encrypt secrets and then pass them in an obfuscated form into applications.

Andrew Lord 3 Nov 9, 2022
A tiny and easy to use Swift class to encrypt strings using HMAC algorithms.

#Sweet HMAC SweetHMAC is a tiny and easy to use Swift class to encrypt strings using HMAC algorithms. A special thanks to jernejstrasner for shared HM

Jan Cássio 37 Jul 27, 2022
RSA public/private key encryption, private key signing and public key verification in Swift using the Swift Package Manager. Works on iOS, macOS, and Linux (work in progress).

BlueRSA Swift cross-platform RSA wrapper library for RSA encryption and signing. Works on supported Apple platforms (using Security framework). Linux

Kitura 122 Dec 16, 2022
RSA public/private key encryption, private key signing and public key verification in Swift using the Swift Package Manager. Works on iOS, macOS, and Linux (work in progress).

BlueRSA Swift cross-platform RSA wrapper library for RSA encryption and signing. Works on supported Apple platforms (using Security framework). Linux

Kitura 122 Dec 16, 2022
Swift RSA Key Loader

Swift RSA Key Loader Requirements iOS 9.0+ Installation See the subsections below for details about the different installation methods. Swift Package

Dmytrii Golovanov 1 Jun 13, 2022
Swift HybridCrypto is simple customizable implementation of hybrid cryptography (AES+RSA+Hash) recommended by OWASP.

HybridCrypto (Swift) HybridCrypto is simple implementation of hybrid cryptography following recommendations by OWASP. Prerequisites: Minimum iOS SDK:

UTNGY Pisal 2 Sep 6, 2022
Wrapper class for handling all tasks related to RSA cryptography

RSAWrapper Wrapper class for handling all tasks related to RSA cryptography USAG

null 1 Dec 24, 2021
Virgil Core SDK allows developers to get up and running with Virgil Cards Service API quickly and add end-to-end security to their new or existing digital solutions to become HIPAA and GDPR compliant and more.

Virgil Core SDK Objective-C/Swift Introduction | SDK Features | Installation | Configure SDK | Usage Examples | Docs | Support Introduction Virgil Sec

Virgil Security, Inc. 27 Jul 26, 2022
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
Native and encrypted password manager for iOS and macOS.

Open Sesame Native and encrypted password manager for iOS and macOS. What is it? OpenSesame is a free and powerful password manager that lets you mana

OpenSesame 432 Jan 7, 2023