如何用cryptojs加密AES 256并在swift中解密

时间:2018-04-03 05:41:50

标签: swift aes cryptoswift

我正在尝试解密由cryptojs解密的字符串,但不是运气......我尝试使用cryptoswift但仍然是faileu

 <!-- WhatsHelp.io widget -->
    <script type="text/javascript">
(function () {
    var options = {
        facebook: "1629986620602076",// Facebook page ID
        whatsapp: "+123456789",
        company_logo_url: "//storage.whatshelp.io/widget/89/89b1/89b1c88a9066163bf664719188cc8a08/12115533_1629987150602023_6988602795927087362_n.png", // URL of company logo (png, jpg, gif)
        greeting_message: "Hello, how may we help you? Just send us a message now to get assistance.", // Text of greeting message
        call_to_action: "Message us", // Call to action
        position: "right", // Position may be 'right' or 'left'
        order: "facebook,whatsapp,email"
    };
    var proto = document.location.protocol, host = "whatshelp.io", url = proto + "//static." + host;
    var s = document.createElement('script'); s.type = 'text/javascript'; s.async = true; s.src = url + '/widget-send-button/js/init.js';
    s.onload = function () { WhWidgetSendButton.init(host, proto, options); };
    var x = document.getElementsByTagName('script')[0]; x.parentNode.insertBefore(s, x);
})();
    </script>
    <!-- /WhatsHelp.io widget -->

迅速的部分:

var encrypted = CryptoJS.AES.encrypt("mystringforencryption", "secret");

 console.log(encrypted);
console.log("encrypted.ciphertext");
console.log(encrypted.ciphertext);
console.log(encrypted.ciphertext.toString(CryptoJS.enc.Base64));
console.log("encrypted.key");
console.log(encrypted.key);
console.log(encrypted.key.toString(CryptoJS.enc.Base64));
console.log("encrypted.iv");
console.log(encrypted.iv);
console.log(encrypted.iv.toString(CryptoJS.enc.Base64));
console.log("encrypted.salt");
console.log(encrypted.salt);
console.log(encrypted.salt.toString(CryptoJS.enc.Base64));
console.log("encrypted.padding.pad");
console.log(encrypted.padding);

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我找到了使用https://github.com/etienne-martin/WebCrypto.swift

的解决方案

所有关于java脚本方面都很清楚,但是对于swift方面需要包含WebCrypto.js,因为它将从swift加载到加密和解密。

在swift中为web中的ecnrypted传递字节64:

data: Data(base64Encoded: "U2FsdGVkX1+4tZicpsJNw9TetmFZ/r+AVQu/g7WPq7Zeen/z2RWfGmlluOeKZgpy", options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)!

完整示例:

<强>迅速

 crypto.decrypt(data: Data(base64Encoded: "U2FsdGVkX1+4tZicpsJNw9TetmFZ/r+AVQu/g7WPq7Zeen/z2RWfGmlluOeKZgpy", options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)!, password: password, callback: {(decrypted: Data?, error: Error?) in
                print(String(data: decrypted!, encoding: .utf8)!)
            })

<强>的javascript

WebCrypto.encrypt({
        data: input,
        password: password,
        callback: function(response){
            if( !response.error ){
                console.log("encrypt base64")
                console.log(response.result);
            }else{
                console.error(response.error);
            }
        }
    });