将 NSData 加密到 obj-c 中的 NSString? [英] Encrypted NSData to NSString in obj-c?

查看:24
本文介绍了将 NSData 加密到 obj-c 中的 NSString?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 iPhone 应用程序,它使用 CCCrypt (AES256) 和一个明文密钥加密输入的 NSString.字符串和密钥被提供给返回 NSData 对象的加密方法.

I have an iPhone app which encrypts an inputted NSString using CCCrypt (AES256) and a plaintext key. The string and key are given to the encryption method which returns an NSData object.

请求 [数据描述] 其中 'data' 是加密的字符串数据会给出一个 NSString,如:<0b368353 a707e7de 3eee5992 ee69827e e3603dc2 b0dbbc0b 861ca87d f39ce72a>"但是当我尝试转换为 NSString 时null)".

Requesting [data description] where 'data' is the encrypted string data gives an NSString like: "<0b368353 a707e7de 3eee5992 ee69827e e3603dc2 b0dbbc0b 861ca87d f39ce72a>" but when I try to convert that to an NSString, I get "(null)".

我需要将一个 NSString 返回给用户,它可以用于使用相同的明文密钥解密回原始字符串.如果 NSData 对象的 'description' 属性可以返回一个字符串,有什么方法可以从 NSData 对象中生成一个 NSString 而不会得到 "(null)" 吗?

I need to return an NSString to the user, which can be used to decrypt back to the original string using the same plaintext key. If the 'description' property of the NSData object can return a string, is there any way I can produce an NSString from the NSData object without getting "(null)"?

更新:感谢 Quinn,他建议使用 Base64 编码来生成混乱的字符串.据我了解,Base64 编码并不是简单地交换字符,而是字符交换取决于位置,所以这很好.

UPDATE: Thanks to Quinn, who suggests using Base64 encoding to produce the muddled string. From what I understand, Base64 encoding does not simply swap characters, but the character exchange depends on the position, so that's fine.

我唯一担心的是,我希望能够使用密码"对消息进行加密,并要求在需要解码混乱的字符串时输入相同的密码 - 任何人都可以提出实现这一点的方法吗?

My only concern is that I want to be able to encrypt the message with a 'passphrase', and require the identical passphrase to be entered when the muddled string needs to be decoded - can anybody suggest ways to implement this?

推荐答案

首先,请勿使用 -[NSData description] 为此类目的创建 NSString.(最好将 -description 视为调试输出.如果 我之前的回答 误导了你,我只是打印说明来证明 NSData 可以加密和解密.)相反,使用 NSString 的 -dataUsingEncoding:-initWithData:encoding: 在 NSData 和 NSString 之间转换的方法.即使有这些,请注意 AES 加密的数据可能无法按原样很好地转换为字符串——某些字节序列无法很好地播放,因此在创建字符串之前对数据进行编码是个好主意.

First off, DO NOT use -[NSData description] to create an NSString for such purposes. (It's best to treat -description as debugging output. I apologize if my previous answer misled you, I was merely printing the description to demonstrate that the NSData can be encrypted and decrypted.) Instead, use NSString's -dataUsingEncoding: and -initWithData:encoding: methods to convert between NSData and NSString. Even with these, note that AES-encrypted data will probably not translate well into strings as-is — some byte sequences just won't play nicely, so it's a good idea to encode the data before creating the string.

我建议您尝试 Base64 编码 NSData,因为 Base64 数据总是可以表示为 ASCII 字符串.(当然,当你这样做时,你必须在解密之前从 Base64 解码.)

I'd suggest you try Base64 encoding the NSData, since Base64 data can always be represented as an ASCII string. (Of course, when you do that, you'll have to decode from Base64 before decrypting.)

这里有一些有用的资源...

Here are some helpful resources...

  • Colloquy has some code that does encoding/decoding on NSData (header and implementation)
  • Google Toolbox for Mac has similar functionality (header and implementation)
  • A Cocoa With Love blog post on the topic.
  • A CocoaDev.com wiki page on the topic.

我假设你会将此与我对 您之前的问题 关于 NSString 对象的 AES 加密.将数据编码为 Base64 不会对数据本身施加任何限制——它当然可以是 AES 加密的数据本身.如果您只想输入和输出字符串,请执行以下操作:

I was assuming you'd combine this with my answer to your previous question on AES encryption of NSString objects. Encoding data as Base64 doesn't place any restrictions on the data itself — it can certainly be AES-enrypted data itself. Here's what to do if you just want string input and output:

  • 加密
    • 提供要加密的 NSString,以及用于加密的密码.
    • 将字符串转换为 NSData 并对其执行 AES 加密(请参阅上一个问题).
    • 对 NSData 进行 Base64 编码,然后创建并返回编码输出的 NSString.
    • 提供加密和编码的字符串,以及用于解密的密码.
    • 从第一个字符串创建一个 NSData,然后对数据进行 Base64 解码.
    • 对数据执行 AES 解密,然后创建并返回一个 NSString.

    这实际上只是将两个部分链接在一起并在输出时反向执行的问题.根据我之前的回答,您可以修改 encryptString:withKey: 以执行最后一步并返回一个字符串,并将 decryptData:withKey: 更改为 decryptString:withKey: 并接受两个字符串.非常简单.

    It's really just a matter of chaining the two parts together and performing them in reverse on the way out. From my previous answer, you can modify encryptString:withKey: to perform the last step and return a string, and change decryptData:withKey: to be decryptString:withKey: and accept two strings. It's pretty straightforward.

    这篇关于将 NSData 加密到 obj-c 中的 NSString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆