首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS中的AES256 NSString Encryption

iOS中的AES256 NSString Encryption
EN

Stack Overflow用户
提问于 2011-11-28 02:05:47
回答 2查看 37.6K关注 0票数 14

我的应用程序使用另一个使用aes 256位加密的NSString (关键字)来加密和解密(或者它应该)一个NSString (要加密/解密的文本)。当我运行我的项目并运行encrypt方法时,没有任何东西被加密,文本字段只会自动清除。下面是我的代码:

代码语言:javascript
复制
-(void)EncryptText {
    //Declare Keyword and Text
    NSString *plainText = DataBox.text;
    NSString *keyword = Keyword.text;

    //Convert NSString to NSData
    NSData *plainData = [plainText dataUsingEncoding:NSUTF8StringEncoding];

    //Encrypt the Data
    NSData *encryptedData = [plainData AESEncryptWithPassphrase:keyword];

    //Convert the NSData back to NSString
    NSString* cypherText = [[NSString alloc] initWithData:encryptedData encoding:NSUTF8StringEncoding];

    //Place the encrypted sting inside the Data Box
    NSLog(@"Cipher Text: %@", cypherText);
}

可以通过单击以下链接下载头文件:ZIP File containing AES Implementation

有人告诉我,我的字符串需要使用Base-64编码才能得到结果。如果这是真的,那我该怎么做呢?

我还被告知,iOS 5中的加密发生了变化,而我的应用程序是一个仅限iOS 5+的应用程序。如果这是真的,那么我必须做什么才能使这种加密在iOS 5上工作,或者我在哪里可以找到另一个可以在NSString上工作的AES256位实现。

为什么这段代码不能产生结果呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-11-28 02:20:48

编辑:下面的链接引用了一个较旧的实现。最新版本称为。

您的代码没有使用iOS的内置AES实现。它有自己的自定义实现。AESEncryptWithPassphrase:还错误地生成了密钥,丢弃了密码短语中的大部分熵。

在iOS上,您应该对AES使用CCCrypt*()函数。您还应该确保了解加密和解密例程中发生的情况。很容易编写看起来正确的加密代码(因为您无法通过检查读取输出),但却非常不安全。

有关上述实现的问题以及如何在iOS上正确使用Properly encrypting with AES with CommonCrypto的说明,请参阅AES。请注意,iOS 5现在提供了CCKeyDerivationPBKDF

在加密之前,不需要对字符串进行Base-64编码。Base-64编码用于需要将二进制数据转换为可通过电子邮件或其他控制字符出现问题的地方轻松发送的格式的情况。它将8位二进制数据转换为7位ASCII数据。这在这里没有必要,也没有用处。

编辑:仔细阅读有关如何使用这段代码的说明是非常重要的。简单地剪切和粘贴安全代码并希望它能工作是很危险的。也就是说,RNCryptManager的完整源代码可以作为iOS 5 Programming Pushing the Limits的第11章示例代码的一部分,并可能有助于编辑:这是旧代码;我现在推荐RNCryptor,链接在答案的顶部。这本书(不管网站怎么说,应该在下周就可以买到了)包括了关于如何使用这些代码的更长的讨论,包括如何提高性能和处理非常大的数据集。

票数 11
EN

Stack Overflow用户

发布于 2011-11-28 02:20:15

NSData的类别刚刚好的AES加密,我没有检查压缩文件,但这应该对你工作;

代码语言:javascript
复制
#import <CommonCrypto/CommonCryptor.h>
@implementation NSData (AESAdditions)
- (NSData*)AES256EncryptWithKey:(NSString*)key {
    // 'key' should be 32 bytes for AES256, will be null-padded otherwise
    char keyPtr[kCCKeySizeAES256 + 1]; // room for terminator (unused)
    bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)

    // fetch key data
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];

    NSUInteger dataLength = [self length];

    //See the doc: For block ciphers, the output size will always be less than or
    //equal to the input size plus the size of one block.
    //That's why we need to add the size of one block here
    size_t bufferSize           = dataLength + kCCBlockSizeAES128;
    void* buffer                = malloc(bufferSize);

    size_t numBytesEncrypted    = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                          keyPtr, kCCKeySizeAES256,
                                          NULL /* initialization vector (optional) */,
                                          [self bytes], dataLength, /* input */
                                          buffer, bufferSize, /* output */
                                          &numBytesEncrypted);

    if (cryptStatus == kCCSuccess)
    {
        //the returned NSData takes ownership of the buffer and will free it on deallocation
        return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
    }

    free(buffer); //free the buffer;
    return nil;
}

- (NSData*)AES256DecryptWithKey:(NSString*)key {
    // 'key' should be 32 bytes for AES256, will be null-padded otherwise
    char keyPtr[kCCKeySizeAES256 + 1]; // room for terminator (unused)
    bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)

    // fetch key data
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];

    NSUInteger dataLength = [self length];

    //See the doc: For block ciphers, the output size will always be less than or
    //equal to the input size plus the size of one block.
    //That's why we need to add the size of one block here
    size_t bufferSize           = dataLength + kCCBlockSizeAES128;
    void* buffer                = malloc(bufferSize);

    size_t numBytesDecrypted    = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                          keyPtr, kCCKeySizeAES256,
                                          NULL /* initialization vector (optional) */,
                                          [self bytes], dataLength, /* input */
                                          buffer, bufferSize, /* output */
                                          &numBytesDecrypted);

    if (cryptStatus == kCCSuccess)
    {
        //the returned NSData takes ownership of the buffer and will free it on deallocation
        return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
    }

    free(buffer); //free the buffer;
    return nil;
}
@end

使用它的包装器函数,如;

代码语言:javascript
复制
- (NSData*) encryptString:(NSString*)plaintext withKey:(NSString*)key {
        return [[plaintext dataUsingEncoding:NSUTF8StringEncoding] AES256EncryptWithKey:key];
}

- (NSString*) decryptData:(NSData*)ciphertext withKey:(NSString*)key {
        return [[[NSString alloc] initWithData:[ciphertext AES256DecryptWithKey:key]
                                      encoding:NSUTF8StringEncoding] autorelease];
}
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8287727

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档