为 Objective-C 中的数据负载提供传输层安全(TLS)的库。通过为交易提供对称密钥来简化数据加密。密钥将实时生成,每条消息将具有新的密钥。
简要介绍 AES 使用随机密钥加密数据,RSA 加密密钥并提供两者。
ObjectiveTLS 将保护数据传输类似于 TLS 握手协议。
Security.framework
ObjectiveTLS
文件夹添加到您的项目中#import "ObjectiveTLS.h"
)public_key.der
和 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
NSString *keyPath = [[NSBundle mainBundle] pathForResource:@"public_key"
ofType:@"der"];
ObjectiveTLS *otls = [[ObjectiveTLS alloc]initWithX509PublicKey:keyPath];
NSString *publicKey = @"MIICs ... kT0=\n"; // Base64 encoded key
NSData *data = [[NSData alloc] initWithBase64EncodedString:publicKey options:NSDataBase64DecodingIgnoreUnknownCharacters];
ObjectiveTLS *otls = [[ObjectiveTLS alloc]initWithX509PublicKeyData:data];
ObjectiveTLS *otls = [[ObjectiveTLS alloc]initWithX509PublicKey:keyPath];
NSError *err = nil;
NSData *key = nil; // AES Key, Encrypted with RSA public key
NSData *iv = nil; // Randomly Generated IV
NSData *encryptedPayload = [otls aesEncryptString:@"Hello World Text"
rsaEncryptedKey:&key
iv:&iv
error:&err];
NSString *string = @"Hello World Text";
NSData *dataToEncrypt = [string dataUsingEncoding:kStringEncoding];
ObjectiveTLS *otls = [[ObjectiveTLS alloc]initWithX509PublicKey:keyPath];
NSError *err = nil;
NSData *key = nil; // AES Key, Encrypted with RSA public key
NSData *iv = nil; // Randomly Generated IV
NSData *encryptedPayload = [otls aesEncryptData:dataToEncrypt
rsaEncryptedKey:&key
iv:&iv
error:&err];
NSString *publicKeyPath = [[NSBundle mainBundle] pathForResource:@"public_key" ofType:@"der"];
NSString *privateKeyPath = [[NSBundle mainBundle] pathForResource:@"private_key" ofType:@"p12"];
NSString *privateKeyPassword = @"Password for .p12 file"
ObjectiveTLS *otls = [[ObjectiveTLS alloc]initWithX509PublicKey:publicKeyPath];
[otls setPrivateKey:privateKeyPath withPassphrase:privateKeyPassword];
NSData *aesEncryptedData; //some encrypted data
NSData *rsaEncryptedKey; // some encrypted key
NSData *iv = nil; // some iv
ObjectiveTLS *otls = [[ObjectiveTLS alloc]initWithX509PublicKey:publicKeyPath];
[otls setPrivateKey:privateKeyPath withPassphrase:@".p12 password"];
NSError *err = nil;
NSData *decryptedPayload = [otls aesDecryptData:dataToEncrypt
rsaEncryptedKey:key
iv:iv
error:&err];
ObjectiveTLS 有几个公开属性,允许您修改加密算法以满足项目的需求。
@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
ObjectiveTLS 允许您定义自定义块来混合和分离初始化向量(IV)与密钥和/或加密数据。
ivMixer
在数据被加密后立即提供对数据、密钥和IV的访问,但在密钥被加密之前。这使得您可以在IV被RSA加密之前将其与密钥混合,以进一步增强IV的安全性。
ivSeparator
是ivMixer
的相反操作。应将ivSeparator
实现为取消混合算法并返回IV的方式。ivSeparator
仅用于解密。
ObjectiveTLS *otls = [[ObjectiveTLS alloc]initWithX509PublicKeyData:pubkeyb64data];
// Prepends the iv to the key before the key is encrypted
[otls 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
[otls 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;
}];
MIT许可证(MIT)
版权所有(c)2014 David Benko
本许可协议授予任何人免费获得此软件及其相关文档副本(以下简称“软件”)的权利,但请在不限制的情况下使用软件,包括但不限于使用、复制、修改、合并、发布、分发、再授权和/或出售软件副本,并允许软件提供者使用该软件,但受以下条件约束:
以上版权声明和本许可协议应包含在软件的所有副本或主要部分中。
本软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、针对特定目的的适用性和非侵权保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论源于合同行为、侵权或其他行为,均与软件或使用或操作软件有关。