swift中的AES加密 [英] AES encryption in swift

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

问题描述

我试图在swift中实现AES加密。 Android和C#的加密解密正常工作。我需要在swift中实现它。这是针对Android和C#的当前代码,后面紧跟着。



我尝试使用


  1. CryptoSwift

  2. Cross平台AES加密

但它们都无效。当我在服务器上发送加密的字符串时,它不会被解密。



任何帮助将不胜感激。

解决方案

确保使用相同的参数,似乎是与CBC模式的AES iv, PKCS5Padding (实际上是PKCS#7)填充和16 -byte(128-bit)key。



PKCS#5填充和 PKCS#7 填充本质上是相同的,有时由于历史原因PKCS#5填充指定与AES一起使用,但实际填充是PKCS#7。



确保密钥,iv和加密数据的编码全部匹配。 Hex在两个平台上转储它们,以确保它们是相同的。加密功能不难使用,如果所有的输入参数都正确,输出将是正确的。



为了更安全,iv应该是随机字节,

跨平台AES 加密使用256位密钥,因此不能按原样工作。

/ p>

示例:

  //操作:kCCEncrypt或kCCDecrypt 
func testCrypt(数据数据:[UInt8],keyData:[UInt8],ivData:[UInt8],operation:Int) [UInt8]? {
let cryptLength = size_t(data.count + kCCBlockSizeAES128)
var cryptData = [UInt8](count:cryptLength,repeatedValue:0)

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

var numBytesEncrypted:size_t = 0

let cryptStatus = CCCrypt(CCOperation(operation),
algoritm,
options,
keyData,keyLength,
ivData,
data,data.count,
& cryptData,cryptLength,
& numBytesEncrypted)
$ b如果UInt32(cryptStatus)== UInt32(kCCSuccess){
cryptData.removeRange(numBytesEncrypted ..< cryptData.count)

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

return cryptData;
}

let message =不要尝试读取这个文本
let messageData = Array(message.utf8)
let keyData = Array(12345678901234567890123456789012.utf8)
let ivData = Array(abcdefghijklmnop.utf8)
let encryptedData = testCrypt(data:messageData,keyData:keyData,ivData:ivData,operation:kCCEncrypt)!
let decryptptedData = testCrypt(data:encryptedData,keyData:keyData,ivData:ivData,operation:kCCDecrypt)!
var decryptpted = String(bytes:decryptedData,encoding:NSUTF8StringEncoding)!

print(message:\(message));
print(messageData:\(NSData(bytes:messageData,length:messageData.count)));
print(keyData:\(NSData(bytes:keyData,length:keyData.count)));
print(ivData:\(NSData(bytes:ivData,length:ivData.count)));
print(encryptedData:\(NSData(bytes:encryptedData,length:encryptedData.count)));
print(decryptedData:\(NSData(bytes:decryptptedData,length:decryptptedData.count)));
print(decrypt:\(String(bytes:decryptptedData,encoding:NSUTF8StringEncoding)!));

输出:

 
消息:不要尝试阅读此文本。绝密的东西
messageData:446f6ec2 b4742074 72792074 6f207265 61642074 68697320 74657874 2e20546f 70205365 63726574 20537475 6666
KEYDATA:31323334 35363738 39303132 33343536 37383930 31323334 35363738 39303132
ivData:61626364 65666768 696a6b6c 6d6e6f70
的EncryptedData :b1b6dc17 62eaf3f8 baa1cb87 21ddc35c dee803ed fb320020 85794848 21206943 a85feb5b c8ee58fc d6fb664b 96b81114
decryptedData:446f6ec2 b4742074 72792074 6f207265 61642074 68697320 74657874 2e20546f 70205365 63726574 20537475 6666
解密:鸵鸟政策尝试读取这个文本。绝密的东西


I'm trying to implement AES encryption in swift. The encryption decryption for Android and C# is working properly. I need to implement it in swift. It's current code for android and C# is followed by this.

I tried to use

  1. CryptoSwift
  2. Cross platform AES encryption

But none of it work. When I send the encrypted string on server it's not been decrypted.

Any help will be appreciated

解决方案

Be sure to use the same parameters which seem to be AES with CBC mode with iv, PKCS5Padding (actually PKCS#7) padding and a 16-byte (128-bit) key.

PKCS#5 padding and PKCS#7 padding are essentially the same, sometimes for historic reasons PKCS#5 padding is specified for use with AES but the actual padding is PKCS#7.

Make sure the encodings of the key, iv and encrypted data all match. Hex dump them on both platforms to ensure they are identical. Encryption functions are not difficult to use, if all the input parameters are correct the output will be correct.

To make this more secure the iv should be random bytes and prepended to the encrypted data for use during decryption.

The Cross platform AES encryption uses a 256-bit key so will not work as-is.

Example:

// operation: kCCEncrypt or kCCDecrypt
func testCrypt(data data:[UInt8], keyData:[UInt8], ivData:[UInt8], operation:Int) -> [UInt8]? {
    let cryptLength  = size_t(data.count+kCCBlockSizeAES128)
    var cryptData    = [UInt8](count:cryptLength, repeatedValue:0)

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

    var numBytesEncrypted :size_t = 0

    let cryptStatus = CCCrypt(CCOperation(operation),
                              algoritm,
                              options,
                              keyData, keyLength,
                              ivData,
                              data, data.count,
                              &cryptData, cryptLength,
                              &numBytesEncrypted)

    if UInt32(cryptStatus) == UInt32(kCCSuccess) {
        cryptData.removeRange(numBytesEncrypted..<cryptData.count)

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

    return cryptData;
}

let message       = "Don´t try to read this text. Top Secret Stuff"
let messageData   = Array(message.utf8)
let keyData       = Array("12345678901234567890123456789012".utf8)
let ivData        = Array("abcdefghijklmnop".utf8)
let encryptedData = testCrypt(data:messageData,   keyData:keyData, ivData:ivData, operation:kCCEncrypt)!
let decryptedData = testCrypt(data:encryptedData, keyData:keyData, ivData:ivData, operation:kCCDecrypt)!
var decrypted     = String(bytes:decryptedData, encoding:NSUTF8StringEncoding)!

print("message:       \(message)");
print("messageData:   \(NSData(bytes:messageData,   length:messageData.count))");
print("keyData:       \(NSData(bytes:keyData,       length:keyData.count))");
print("ivData:        \(NSData(bytes:ivData,        length:ivData.count))");
print("encryptedData: \(NSData(bytes:encryptedData, length:encryptedData.count))");
print("decryptedData: \(NSData(bytes:decryptedData, length:decryptedData.count))");
print("decrypted:     \(String(bytes:decryptedData,encoding:NSUTF8StringEncoding)!)");

Output:

message:       Don´t try to read this text. Top Secret Stuff  
messageData:   446f6ec2 b4742074 72792074 6f207265 61642074 68697320 74657874 2e20546f 70205365 63726574 20537475 6666  
keyData:       31323334 35363738 39303132 33343536 37383930 31323334 35363738 39303132  
ivData:        61626364 65666768 696a6b6c 6d6e6f70  
encryptedData: b1b6dc17 62eaf3f8 baa1cb87 21ddc35c dee803ed fb320020 85794848 21206943 a85feb5b c8ee58fc d6fb664b 96b81114  
decryptedData: 446f6ec2 b4742074 72792074 6f207265 61642074 68697320 74657874 2e20546f 70205365 63726574 20537475 6666  
decrypted:     Don´t try to read this text. Top Secret Stuff  

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

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