加密NSData到NSString在obj-c? [英] Encrypted NSData to NSString in obj-c?

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

问题描述

我有一个iPhone应用程序,使用CCCrypt(AES256)和一个明文密钥对输入的NSString进行加密。字符串和键被赋予返回NSData对象的加密方法。



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



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



更新:感谢Quinn,谁建议使用Base64编码来产生混乱的字符串。从我所理解的,Base64编码不仅仅是交换字符,而是字符交换取决于位置,所以没关系。



我唯一关心的是,我想能够使用密码加密邮件,并且要求在混淆的字符串需要时输入相同的密码解码 - 有人可以建议实现这个方法吗?

解决方案

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



我建议您尝试 Base64 encoding NSData ,因为Base64数据可以始终表示为ASCII字符串。 (当然,当你这样做的时候,你必须在解密之前从Base64解码。)



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








编辑:请将此与我的答案相结合,以您之前的问题 NSString对象的AES加密。将数据编码为Base64不对数据本身施加任何限制 - 它当然可以是AES加密数据本身。如果您只想输入字符串和输出,请执行以下操作:




  • 加密


    • 提供要加密的NSString和用于加密的密码。

    • 将该字符串转换为NSData并对其执行AES加密(请参阅上一个问题) li>
    • Base64编码NSData,然后创建并返回编码输出的NSString。


  • 解密


    • 提供加密和编码的字符串以及用于解密的密码。

    • 从第一个字符串,然后Base64解码数据。

    • 对数据执行AES解密,然后创建并返回一个NSString。




这真的只是将两个部分链接在一起,并在出路上反向执行。从我以前的答案,你可以修改来执行最后一步并返回一个字符串,并更改 decryptData:withKey: decryptString:withKey:并接受两个字符串。这很简单。


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.

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)".

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)"?

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?

解决方案

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.

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...


Edit: 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:

  • Encryption
    • Provide the NSString to be encrypted, and the passphrase to use for encrypting.
    • Convert the string to an NSData and perform AES encryption on it (see previous question).
    • Base64-encode the NSData, then create and return and NSString of the encoded output.
  • Decryption
    • Provide the encrypted and encoded string, and the passphrase to use for decrypting.
    • Create an NSData from the first string, then Base64-decode the data.
    • Perform AES decryption on the data, then create and return an NSString.

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到NSString在obj-c?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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