Transport Layer Security为实现Objective-C中数据负载的加密提供安全措施。通过提供交易对称密钥的简单方法来保护数据安全。密钥实时生成,每条消息都将有新的密钥。
简化版 AES使用随机密钥加密数据,RSA加密密钥并两者都提供。
DBTransitEncryption将为传输中的数据提供类似于TLS握手协议的安全措施。
Security.framework
DBTransitEncryption
文件夹添加到您的项目中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"];
DBTransitEncryptor *encryptor = [[DBTransitEncryptor alloc]initWithX509PublicKey:keyPath];
NSString *publicKey = @"MIICs ... kT0=\n"; // Base64 encoded key
NSData *data = [[NSData alloc] initWithBase64EncodedString:publicKey options:NSDataBase64DecodingIgnoreUnknownCharacters];
DBTransitEncryptor *encryptor = [[DBTransitEncryptor alloc]initWithX509PublicKeyData:data];
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];
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];
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];
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];
DBTransitEncryptor 几个公共属性允许您修改加密算法,以适应您项目的需求。
@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
DBTransitEncryption 允许您定义自定义块来混合和分离初始化向量(IV)与密钥和/或加密数据。
ivMixer
提供在数据加密后立即访问数据、密钥和 IV 的方法,但在密钥加密之前。这允许在 RSA 加密之前将 IV 与密钥混合,以进一步增强 IV 的安全性。
ivSeparator
与 ivMixer
相反。应该以一种能够逆转混合算法并返回 IV 的方式实现 ivSeparator
。**ivSeparator
仅在解密时需要**。
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;
}];
MIT 许可证 (MIT)
版权所有 © 2014 David Benko
特此授予任何获得此软件及其相关文档副本(以下简称“软件”)的人无限制地使用、复制、修改、合并、发布、分发、许可和/或出售软件副本的权利,并允许向软件提供者分发软件的人执行上述操作,前提是受以下条件的约束
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
软件按“现状”提供,不考虑任何形式的保证,无论是明示的、暗示的,还是法律的,包括但不限于适销性、适用于特定目的和非侵权性保证。在任何情况下,作者或版权所有者不对因合同、侵权或其他行为而产生的任何索赔、损害或其他责任承担任何责任,无论该索赔、损害或其他责任是由于使用软件、软件的使用或其他方式而产生的。