ObjectiveTLS 0.1.1

ObjectiveTLS 0.1.1

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最新发布2014年12月

David Benko维护。



概述

为 Objective-C 中的数据负载提供传输层安全(TLS)的库。通过为交易提供对称密钥来简化数据加密。密钥将实时生成,每条消息将具有新的密钥。

简要介绍 AES 使用随机密钥加密数据,RSA 加密密钥并提供两者。

它做了什么?

ObjectiveTLS 将保护数据传输类似于 TLS 握手协议。

  • 生成 AES 对称密钥
  • 使用 AES 密钥加密数据负载
  • 使用 X.509 RSA 公共密钥加密 AES 密钥
  • 返回 AES 加密的负载和 RSA 加密的对称密钥

安装

手动安装
  • 将项目链接到 Security.framework
  • ObjectiveTLS 文件夹添加到您的项目中
  • 导入头文件 (#import "ObjectiveTLS.h")

生成 X.509 RSA 密钥对

  • 运行以下命令以生成测试用的个人密钥对。
  • 您关心的文件是 public_key.derprivate_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

加密

使用捆绑的 X.509 公钥文件 (.der)

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

    ObjectiveTLS *otls = [[ObjectiveTLS alloc]initWithX509PublicKey:keyPath];

使用内存中的 X.509 公钥(推荐)

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

    ObjectiveTLS *otls = [[ObjectiveTLS alloc]initWithX509PublicKeyData:data];

加密 NSString

    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];

加密 NSData

    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];

解密

使用捆绑的 PKCS#12 RSA 私钥文件 (.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"

    ObjectiveTLS *otls = [[ObjectiveTLS alloc]initWithX509PublicKey:publicKeyPath];
    [otls setPrivateKey:privateKeyPath withPassphrase:privateKeyPassword];

解密 NSData

    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

IV混播块

ObjectiveTLS 允许您定义自定义块来混合和分离初始化向量(IV)与密钥和/或加密数据。

ivMixer 在数据被加密后立即提供对数据、密钥和IV的访问,但在密钥被加密之前。这使得您可以在IV被RSA加密之前将其与密钥混合,以进一步增强IV的安全性。

ivSeparatorivMixer的相反操作。应将ivSeparator实现为取消混合算法并返回IV的方式。ivSeparator仅用于解密。

IV混合示例

    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

本许可协议授予任何人免费获得此软件及其相关文档副本(以下简称“软件”)的权利,但请在不限制的情况下使用软件,包括但不限于使用、复制、修改、合并、发布、分发、再授权和/或出售软件副本,并允许软件提供者使用该软件,但受以下条件约束:

以上版权声明和本许可协议应包含在软件的所有副本或主要部分中。

本软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、针对特定目的的适用性和非侵权保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论源于合同行为、侵权或其他行为,均与软件或使用或操作软件有关。