如何从NSData获取字符串,反之亦然,通过QR码发送?

时间:2016-11-12 07:04:30

标签: ios swift encryption qr-code

我需要通过我的QR码发送加密数据,所以我加密了一些文字。我希望通过QR码将这些加密数据的字符串发送到另一个可以解密它的设备。我甚至无法从加密数据中获取字符串。我假设加密数据可能不是字符串(零)所以如何通过QR码传输数据?这是我试图获取加密数据的字符串:

app.get('/', function( req, res ) {
  // You can process the request here.
  // Eg. authentication
  meter.hi(req, res);
});

这是功能:

let data = (message as NSString).dataUsingEncoding(NSUTF8StringEncoding) as NSData!

        if let encryptedData = testCrypt(data, keyData:keyData, ivData:ivData, operation:UInt32(kCCEncrypt)) {
            let stringFromData = NSString(data: encryptedData, encoding: NSUTF8StringEncoding)
            print("stringFromData = \(stringFromData)") // nil
}

我尝试的另一个解决方案是将NSData插入到字符串中。这工作但是我很难尝试将字符串返回到WR代码接收端的NSData。我有加密/解密的代码我只需要弄清楚如何以某种方式通过QR码发送加密数据。这是我的插值:

func testCrypt(data:NSData, keyData:NSData, ivData:NSData, operation:CCOperation) -> NSData? {
        let keyBytes = UnsafePointer<UInt8>(keyData.bytes)            
        let ivBytes = UnsafePointer<UInt8>(ivData.bytes)
        let dataLength = Int(data.length)
        let dataBytes  = UnsafePointer<UInt8>(data.bytes)

        let cryptData: NSMutableData! = NSMutableData(length: Int(dataLength) + kCCBlockSizeAES128)
        let cryptPointer = UnsafeMutablePointer<UInt8>(cryptData.mutableBytes)
        let cryptLength  = size_t(cryptData.length)

        let keyLength              = size_t(kCCKeySizeAES128)
        let algoritm:  CCAlgorithm = UInt32(kCCAlgorithmAES128)
        let options:   CCOptions   = UInt32(kCCOptionPKCS7Padding)

        var numBytesEncrypted :size_t = 0

        let cryptStatus = CCCrypt(operation,
                                  algoritm,
                                  options,
                                  keyBytes, keyLength,
                                  ivBytes,
                                  dataBytes, dataLength,
                                  cryptPointer, cryptLength,
                                  &numBytesEncrypted)

        if UInt32(cryptStatus) == UInt32(kCCSuccess) {
            cryptData.length = Int(numBytesEncrypted)

        } else {
            print("Error: \(cryptStatus)")
        }

        return cryptData;
    }

0 个答案:

没有答案