iOS Swift 4:如何使用DES-ECB-PKCS5Padding执行加密/解密?

时间:2018-12-25 07:29:58

标签: ios encryption cryptography swift4

我想在iOS Swift中使用 DES-ECB-PKCS5Padding 进行加密/解密。

我从服务器端(很可能是在ActionScript中)获得了一些代码来帮助您,如下所示:

private static const type:String='simple-des-ecb';

public static function encrypt(txt:String, salt:String): String
{
    var key:ByteArray = Hex.toArray(Hex.fromString(salt));
    var data:ByteArray = Hex.toArray(Hex.fromString(txt));
    var pad:IPad = new PKCS5;
    var mode:ICipher = Crypto.getCipher(type, key, pad);
    pad.setBlockSize(mode.getBlockSize());
    mode.encrypt(data);
    data.position = 0;
    return Base64.encodeByteArray(data);
}

public static function decrypt(txt:String, salt:String): String
{
    var key:ByteArray = Hex.toArray(Hex.fromString(salt));
    var data:ByteArray = Base64.decodeToByteArray(txt);
    var pad:IPad = new PKCS5;
    var mode:ICipher = Crypto.getCipher(type, key, pad);
    pad.setBlockSize(mode.getBlockSize());
    try
    {
        mode.decrypt(data);
    }
    catch (e:Error)
    {
        trace(e.message);
        trace(e.getStackTrace());
    }
    return Hex.toString(Hex.fromArray(data));
}

我已经尝试在 Swift 中实现上述功能,但是我无法获得预期的结果

我的Swift代码如下:

func encrypt(text: String, salt: String) -> String {

    let strHexKey = hexFromString(string: salt)
    let key = strHexKey.hexaBytes

    let strHexData = hexFromString(string: text)
    let data = strHexData.hexaBytes

    let cryptor = Cryptor(operation: .encrypt, algorithm: .des, options: .PKCS7Padding, key: key, iv: Array<UInt8>())
    let cipherText = cryptor.update(byteArray: data)?.final()

    let strBase64 = cipherText!.data.base64EncodedString()

    return strBase64

}

func decrypt(text: String, salt: String) -> String {

    let strHexKey = hexFromString(string: salt)
    let key = arrayFrom(hexString: strHexKey)

    let data = text.fromBase64()?.data(using: .utf8)

    let cryptor = Cryptor(operation: .decrypt, algorithm: .des, options: .PKCS7Padding, key: key, iv: Array<UInt8>())
    let cipherText = cryptor.update(data: data!)!.final()

    return (cipherText?.hexa)!

}

我一直在使用https://github.com/iosdevzone/IDZSwiftCommonCrypto作为加密库。

1 个答案:

答案 0 :(得分:4)

快捷代码

将ActionScript代码转换为Swift可能是这样的:

func encrypt(text: String, salt: String) -> String? {
    let key = Array(salt.utf8)
    let bytes = Array(text.utf8)
    let cryptor = Cryptor(operation: .encrypt, algorithm: .des, options: [.ECBMode, .PKCS7Padding], key: key, iv:[UInt8]())
    if let encrypted = cryptor.update(byteArray: bytes)?.final() {
        return Data(encrypted).base64EncodedString()
    }
    return nil
}

func decrypt(text: String, salt: String) -> String? {
    let key = Array(salt.utf8)
    let bytes = [UInt8](Data(base64Encoded: text)!)
    let cryptor = Cryptor(operation: .decrypt, algorithm: .des, options: [.ECBMode, .PKCS7Padding], key: key, iv:[UInt8]())
    if let decrypted = cryptor.update(byteArray: bytes)?.final() {
        return String(bytes: decrypted, encoding: .utf8)
    }
    return nil
}

快速测试

在您用作测试用例的注释中,带有文本 dimusco@yopmail.com 和盐: 123456 。这样称呼:

let salt = "123456"
if let encrypted = self.encrypt(text: "dimusco@yopmail.com", salt: salt) {
    print ("encrypted: " + encrypted)
    if let decrypted = self.decrypt(text: encrypted, salt: salt) {
        print ("decrypted: " + decrypted)
    }
}

正确的输出为:

encrypted: +rptz3Ss8zh2j0VXN7CICsi2jkzYoAPx
decrypted: dimusco@yopmail.com

我不知道您从哪里得到结果 eLnJvMUKApg = ,但这是错误的。同样,您在问题中引用的ActionScript代码也会给出结果 + rptz3Ss8zh2j0VXN7CICsi2jkzYoAPx

与ActionScript比较

Flex

您可以从此处下载免费的flex sdk:

https://download.macromedia.com/pub/flex/sdk/flex_sdk_4.6.zip

as3crypto库

引用的ActionScript似乎使用了一个名为as3crypto的库,可以从此处下载该文件:

https://code.google.com/archive/p/as3crypto/

Flex取决于已安装的Java环境。它似乎无法立即使用最新版本的Java,但是您可以使用Java 1.6。

您可以创建一个小的ActionScript程序,该程序调用as3crypto例程。如果文件名为Main.as,可以使用以下命令进行编译:

flex_sdk_4.6/bin/mxmlc Main.as

然后,输出为Main.swf文件,可以使用浏览器中的Flash插件执行该文件。

结果

ActionScript代码的结果与Swift代码相同,请参见此屏幕截图:

Comparison with ActionScript Code