All Projects → DavidBenko → Dbtransitencryption

DavidBenko / Dbtransitencryption

Licence: mit
Secure data before sending it over the internet

Labels

Projects that are alternatives of or similar to Dbtransitencryption

Phpseclib
PHP Secure Communications Library
Stars: ✭ 4,627 (+23035%)
Mutual labels:  aes, rsa
dart crypto
[Flutter] 本项目基于flutter_macos_v0.5.8-dev版本采用Dart语言开发。`DYFCryptoProvider`集成了Base64, 32/16 Bits MD5, AES, RSA等算法。(This Flutter project is developed in Dart language based on flutter_macos_v0.5.8-dev. `DYFCryptoProvider` integrates Base64, 32/16 Bits MD5, AES and RSA algorithms.)
Stars: ✭ 30 (+50%)
Mutual labels:  aes, rsa
interesting-keys
Interesting collected (leaked) encryption/decryption keys
Stars: ✭ 33 (+65%)
Mutual labels:  aes, rsa
java-sdk
一些常用的java sdk和工具类(日期工具类,分布式锁,redis缓存,二叉树,反射工具类,线程池,对称/非对称/分段加解密,json序列化,http工具,雪花算法,字符串相似度,集合操作工具,xml解析,重试Retry工具类,Jvm监控等)
Stars: ✭ 26 (+30%)
Mutual labels:  aes, rsa
Swcrypt
RSA public/private key generation, RSA, AES encryption/decryption, RSA sign/verify in Swift with CommonCrypto in iOS and OS X
Stars: ✭ 632 (+3060%)
Mutual labels:  aes, rsa
webcrypto
A WebCrypto Polyfill for NodeJS
Stars: ✭ 111 (+455%)
Mutual labels:  aes, rsa
dtls
Datagram Transport Layer Security (DTLS) client.
Stars: ✭ 72 (+260%)
Mutual labels:  aes, rsa
jh-weapp-demo
微信小程序项目- 实现一些常用效果、封装通用组件和工具类
Stars: ✭ 60 (+200%)
Mutual labels:  aes, rsa
Aes Rsa Java
AES+RSA结合应用java示例
Stars: ✭ 295 (+1375%)
Mutual labels:  aes, rsa
Oscrypto
Compiler-free Python crypto library backed by the OS, supporting CPython and PyPy
Stars: ✭ 257 (+1185%)
Mutual labels:  aes, rsa
galois
A performant NumPy extension for Galois fields and their applications
Stars: ✭ 106 (+430%)
Mutual labels:  aes, rsa
Netcore.encrypt
NETCore encrypt and decrpty tool,Include aes,des,rsa,md5,sha1,sha256,sha384,sha512
Stars: ✭ 339 (+1595%)
Mutual labels:  aes, rsa
openssl
A functions wrapping of OpenSSL library for symmetric and asymmetric encryption and decryption.
Stars: ✭ 199 (+895%)
Mutual labels:  aes, rsa
common-secure
提供一些加密算法java代码封装 包括 RSA/AES/DES/3DES/MD5/SHA/HmacSHA256
Stars: ✭ 37 (+85%)
Mutual labels:  aes, rsa
Python-SecureHTTP
Make HTTP transmissions more secure via RSA+AES, encrypted communication for C/S architecture.
Stars: ✭ 19 (-5%)
Mutual labels:  aes, rsa
vue-apicloud-cli
基于vue的APICloud脚手架
Stars: ✭ 44 (+120%)
Mutual labels:  aes, rsa
oseid
Microchip AVR based smartcard/token with ECC and RSA cryptography
Stars: ✭ 17 (-15%)
Mutual labels:  aes, rsa
Qt-Secret
Simple encryption library supporting RSA and AES algorithms.
Stars: ✭ 196 (+880%)
Mutual labels:  aes, rsa
encryptlab
🔑 Comprehensive (and free) list of encryption and decryption in Node.js.
Stars: ✭ 80 (+300%)
Mutual labels:  aes, rsa
Goencrypt
go语言封装的各种对称加密和非对称加密,可以直接使用,包括3重DES,AES的CBC和CTR模式,还有RSA非对称加密,ECC椭圆曲线的加密和数字签名
Stars: ✭ 297 (+1385%)
Mutual labels:  aes, rsa

DBTransitEncryption

Overview

Transport Layer Security for securing data payloads in Objective-C. An easy way to secure data by providing a symmetric key for that transaction. Keys are generated on the fly and every message will have a new key.

TL;DR AES encrypts data with a random key, RSA encrypts key and provides both.

What does it do?

DBTransitEncryption will secure data for transit similar to the handshake protocol of TLS.

  • Generate AES symmetric key
  • Encrypt data payload with AES key
  • Encrypt AES key with X.509 RSA public key
  • Returns AES-encrypted payload and RSA-encrypted symmetric key

Installation

Via CocoaPods
  • Add pod 'DBTransitEncryption' to your podfile
  • Run pod install
  • Import header (#import <DBTransitEncryption/DBTransitEncryption.h>)
Manual Installation
  • Link project against Security.framework
  • Add DBTransitEncryption folder to your project
  • Import header (#import "DBTransitEncryption.h")

Generate X.509 RSA Key Pair

  • Run the following commands to generate a personal key pair for testing.
  • The files you care about are public_key.der and private_key.p12
openssl req -x509 -out public_key.der -outform der -new -newkey rsa:1024 -keyout private_key.pem -days 3650
openssl x509 -inform der -outform pem -in public_key.der -out public_key.pem
openssl pkcs12 -export -in public_key.pem -inkey private_key.pem -out private_key.p12

Encryption

Using Bundled X.509 Public Key (.der)

   
    NSString *keyPath = [[NSBundle mainBundle] pathForResource:@"public_key"
                                                        ofType:@"der"];
    
    DBTransitEncryptor *encryptor = [[DBTransitEncryptor alloc]initWithX509PublicKey:keyPath];

Using in-memory X.509 Public Key (Recommended)

    
	NSString *publicKey = @"MIICs ... kT0=\n"; // Base64 encoded key
    NSData *data = [[NSData alloc] initWithBase64EncodedString:publicKey options:NSDataBase64DecodingIgnoreUnknownCharacters];
    
    DBTransitEncryptor *encryptor = [[DBTransitEncryptor alloc]initWithX509PublicKeyData:data];

Encrypt NSString

    
	DBTransitEncryptor *encryptor = [[DBTransitEncryptor alloc]initWithX509PublicKey:keyPath];
    NSError *err = nil;
    NSData *key = nil;  // AES Key, Encrypted with RSA public key
    NSData *iv = nil;   // Randomly Generated IV
    
    NSData *encryptedPayload = [encryptor encryptString:@"Hello World Text"
                                      rsaEncryptedKey:&key
                                                   iv:&iv
                                                error:&err];

Encrypt NSData

    
	NSString *string = @"Hello World Text";
    NSData *dataToEncrypt = [string dataUsingEncoding:kStringEncoding];
	
    DBTransitEncryptor *encryptor = [[DBTransitEncryptor alloc]initWithX509PublicKey:keyPath];
    NSError *err = nil;
    NSData *key = nil;  // AES Key, Encrypted with RSA public key
    NSData *iv = nil;   // Randomly Generated IV
    
    NSData *encryptedPayload = [encryptor encryptData:dataToEncrypt
                                      rsaEncryptedKey:&key
                                                   iv:&iv
                                                error:&err];

Decryption

Using Bundled PKCS#12 RSA Private Key (.p12)

	
	NSString *publicKeyPath = [[NSBundle mainBundle] pathForResource:@"public_key" ofType:@"der"];
	NSString *privateKeyPath = [[NSBundle mainBundle] pathForResource:@"private_key" ofType:@"p12"];
    NSString *privateKeyPassword = @"Password for .p12 file"
	
    DBTransitEncryptor *encryptor = [[DBTransitEncryptor alloc]initWithX509PublicKey:publicKeyPath];
	[encryptor setPrivateKey:privateKeyPath withPassphrase:privateKeyPassword];

Decrypt NSData

    NSData *aesEncryptedData; //some encrypted data
	NSData *rsaEncryptedKey; // some encrypted key
	NSData *iv = nil; // some iv
	
    DBTransitEncryptor *encryptor = [[DBTransitEncryptor alloc]initWithX509PublicKey:publicKeyPath];
	[encryptor setPrivateKey:privateKeyPath withPassphrase:@".p12 password"];
    NSError *err = nil;
	    
    NSData *decryptedPayload = [encryptor decryptData:dataToEncrypt
                                      rsaEncryptedKey:key
                                                   iv:iv
                                                error:&err];

Public Properties

DBTransitEncryptor has a few public properties which allow you to modify the encryption algorithms to suit your project's needs.

@property (nonatomic, assign) NSUInteger rsaKeySize;                        // RSA key size in bits
@property (nonatomic, assign) SecPadding rsaPadding;                        // RSA padding
@property (nonatomic, assign) CCAlgorithm encryptorAlgorithm;               // Data payload encryption algorithm
@property (nonatomic, assign) CCOptions encryptorAlgorithmOptions;          // Options (padding) for data payload encryptor
@property (nonatomic, assign) NSUInteger encryptorAlgorithmKeySize;         // Size of generated symmetric key
@property (nonatomic, assign) NSUInteger encryptorAlgorithmBlockSize;       // Block size of data payload encryption algorithm
@property (nonatomic, assign) NSUInteger encryptorAlgorithmIVSize;          // Size of generated initialization vector
@property (nonatomic, assign) NSStringEncoding encryptorStringEncoding;     // String encoding for encrypted/decrypted strings
@property (readwrite, copy) IVMixerBlock ivMixer;                           // Block to mix IV with key or data
@property (readwrite, copy) IVSeparatorBlock ivSeparator;                   // Block to separate IV from key or data

IV Mixer Blocks

DBTransitEncryption allows you to define custom blocks to mix and separate the initialization vector with the key and/or the encrypted data.

The ivMixer gives access to the data, key, and iv immediately after the data is encrypted, but before the key is encrypted. This allows you to mix the iv with key before it is RSA encrypted, to further secure the iv.

The ivSeparator is the opposite of the ivMixer. The ivSeparator should be implemented in a way which undoes the mixing algorithm and returns the iv. The ivSeparator is only needed for decryption.

IV Mixing Example

    DBTransitEncryptor *encryptor = [[DBTransitEncryptor alloc]initWithX509PublicKeyData:pubkeyb64data];
    
    // Prepends the iv to the key before the key is encrypted
    
    [encryptor setIvMixer:^(NSData **data,NSData **key, NSData *iv){
        NSMutableData *mutableKey = [iv mutableCopy];
        [mutableKey appendBytes:[*key bytes] length:[*key length]];
        *key = mutableKey;
    }];
    
    // Extracts the iv from the key before decryption
    
    [encryptor setIvSeparator:^NSData *(NSData **data, NSData **key){
        NSInteger ivSize = 16;
        NSMutableData *mutableKey = [*key mutableCopy];
        NSRange range = NSMakeRange(0, ivSize);
        NSData *iv = [mutableKey subdataWithRange:range];
        [mutableKey replaceBytesInRange:range withBytes:NULL length:0];
        *key = mutableKey;
        return iv;
    }];

License

The MIT License (MIT)

Copyright (c) 2014 David Benko

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].