首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift中的图像加密

Swift中的图像加密
EN

Stack Overflow用户
提问于 2017-07-20 06:26:50
回答 1查看 1.3K关注 0票数 3

我使用IDZSwiftCommonCrypto进行图像加密,使用StreamCryptor描述为GitHub页面上的示例:https://github.com/iosdevzone/IDZSwiftCommonCrypto

我无法成功解密。这是我的加密和解密代码(imageData来自UIImageView)。加密后的输出与输入不同(imageData与xx不同)。

加密:

代码语言:javascript
复制
func performImageEncryption(imageData: Data) -> Void {

        var inputStream = InputStream(data: imageData)

        let key = arrayFrom(hexString: "2b7e151628aed2a6abf7158809cf4f3c")

        var sc = StreamCryptor(operation:.encrypt, algorithm:.aes, options:.PKCS7Padding, key:key, iv:Array<UInt8>())

        var inputBuffer = Array<UInt8>(repeating:0, count:1024)
        var outputBuffer = Array<UInt8>(repeating:0, count:1024)

        inputStream.open()

        var cryptedBytes = 0

        var xx = Data()

        var count = 0

        while inputStream.hasBytesAvailable
        {
            count = count + 1024
            let bytesRead = inputStream.read(&inputBuffer, maxLength: inputBuffer.count)
            let status = sc.update(bufferIn: inputBuffer, byteCountIn: bytesRead, bufferOut: &outputBuffer, byteCapacityOut: outputBuffer.count, byteCountOut: &cryptedBytes)

            xx.append(contentsOf: outputBuffer)
        }

        let status = sc.final(bufferOut: &outputBuffer, byteCapacityOut: outputBuffer.count, byteCountOut: &cryptedBytes)


        xx.append(contentsOf: outputBuffer)


        inputStream.close()

        performImageDecryption(encryptedImageData: xx)


    }

解密:

代码语言:javascript
复制
func performImageDecryption(encryptedImageData: Data) -> Void {

        let key = arrayFrom(hexString: "2b7e151628aed2a6abf7158809cf4f3c")

        var sc = StreamCryptor(operation:.decrypt, algorithm:.aes, options:.PKCS7Padding, key:key, iv:Array<UInt8>())

        var inputStreamD = InputStream(data: encryptedImageData)

        var inputBuffer = Array<UInt8>(repeating:0, count:1024)
        var outputBuffer = Array<UInt8>(repeating:0, count:1024)

        inputStreamD.open()

        var cryptedBytes = 0


        var xx = Data()
        while inputStreamD.hasBytesAvailable
        {
            let bytesRead = inputStreamD.read(&inputBuffer, maxLength: inputBuffer.count)
            let status = sc.update(bufferIn: inputBuffer, byteCountIn: bytesRead, bufferOut: &outputBuffer, byteCapacityOut: outputBuffer.count, byteCountOut: &cryptedBytes)
            xx.append(contentsOf: outputBuffer)
        }

        let status = sc.final(bufferOut: &outputBuffer, byteCapacityOut: outputBuffer.count, byteCountOut: &cryptedBytes)
        xx.append(contentsOf: outputBuffer)

        inputStreamD.close()

    }
EN

回答 1

Stack Overflow用户

发布于 2018-07-30 17:40:48

代码语言:javascript
复制
xx.append(outputBuffer, count: cryptedBytes)

应该能帮上忙。

下面是用于获取加密图像文件并返回数据的示例代码。

代码语言:javascript
复制
func decryptImage(from path:URL)-> Data? {
    var decryptData = Data()

    let sc = StreamCryptor(operation:.decrypt, algorithm:.aes, options:.PKCS7Padding, key:key, iv:iv)

    guard let encryptedInputStream = InputStream(fileAtPath: path.relativePath) else {
        return nil
    }

    var inputBuffer = [UInt8](repeating: 0, count: Int(1024))
    var outputBuffer = [UInt8](repeating: 0, count: Int(1024))

    encryptedInputStream.open()

    var cryptedBytes : Int = 0
    while encryptedInputStream.hasBytesAvailable
    {
        let bytesRead = encryptedInputStream.read(&inputBuffer, maxLength: inputBuffer.count)

        let status = sc.update(bufferIn: inputBuffer, byteCountIn: bytesRead, bufferOut: &outputBuffer, byteCapacityOut: outputBuffer.count, byteCountOut: &cryptedBytes)

        if (status != Status.success) {
        encryptedInputStream.close()
            return nil
        }

        if(cryptedBytes > 0)
        {
            decryptData.append(outputBuffer, count: cryptedBytes)
        }
    }

    let status = sc.final(bufferOut: &outputBuffer, byteCapacityOut: outputBuffer.count, byteCountOut: &cryptedBytes)
    if (status != Status.success) {
        encryptedInputStream.close()
        return nil
    }

    if(cryptedBytes > 0)
    {
        decryptData.append(outputBuffer, count: cryptedBytes)
    }
    encryptedInputStream.close()
    return decryptData
}

快乐编码:)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45206786

复制
相关文章

相似问题

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