AES CryptoSwift密码后如何将字节转换为NSString [英] How to convert bytes to NSString after AES CryptoSwift cipher

查看:214
本文介绍了AES CryptoSwift密码后如何将字节转换为NSString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CryptoSwift加密将在URL中传递的数据.为此,我需要将数据的数据类型设置为String,以连接到NSURL请求中.加密数据后,将以字节为单位输出.如何将字节转换为无用的字符串以传递PHP脚本可以解密的URL?

I am using CryptoSwift to encrypt data I will be passing in a URL. To do this, I need the datatype of the piece of data to be a String to concatenate into the NSURL request. After encrypting the data it is output in bytes. How can I cast the bytes to a nonsense string to pass in the URL that a PHP script can decrypt?

我能够加密到UInt8中,但是我认为不可能将其通过URL传递给PHP脚本,因此我需要将其设为字符串.

I am able to encrypt into UInt8, however I do not think it is possible to pass it over a URL to PHP script so I need to make it a string.

代码:

let string = "hello"
let input: [UInt8] = Array(string.utf8)
let key: [UInt8] = [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00]
let iv: [UInt8] = AES.randomIV(AES.blockSize)
do {
    let encrypted: [UInt8] = try AES(key: key, iv: iv, blockMode: .CBC).encrypt(input, padding: PKCS7())
    print("Encrypted bytes: \(encrypted)")
    let str = String(bytes: encrypted, encoding: NSUTF8StringEncoding)
    print("String of encrypted data: \(str)") // prints a nil value
} catch AES.Error.BlockSizeExceeded {
    print("Block size exceeded")
} catch {
    print("Error encrypting or decrypting data")
}

非常感谢您的帮助,

Thank you very much for the help,

推荐答案

  1. 不要使用CryptoSwift,它既不快(比Common Crypto慢500-1000倍),也不安全(没有实质审查).

  1. Do not use CryptoSwift, it is neither fast (500-1000 times slower than Common Crypto) nor secure (it lacks substantial vetting).

加密数据与随机数据字节无法区分.

Encrypted data is undistinguishable from random data bytes.

并非所有数据数组都可以任何编码形式转换为字符串.有许多数据字节没有可打印的表示形式或字符串编码的任何表示形式.这就是为什么通常将原始数据字节编码为Base64或十六进制字符串的原因.

Not all data arrays are convertible to strings in any encoding. There are many data bytes that do not have a printable representation or any representation in a string encoding. That is why raw data bytes are generally encoded to Base64 or hexadecimal strings.

这篇关于AES CryptoSwift密码后如何将字节转换为NSString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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