0

我想通过javascript在服务器端加密一个二进制文件,并在Unity中通过c#在客户端解密。

代码服务器端:

    var reader = new FileReader();

    if(body.hasClass('encrypt')){

        // Encrypt the file!

        reader.onload = function(e){

            // Use the CryptoJS library and the AES cypher to encrypt the 
            // contents of the file, held in e.target.result

            var key = CryptoJS.enc.Utf8.parse('8080808080808080');
            var iv = CryptoJS.enc.Utf8.parse('8080808080808080');
            var encrypted = CryptoJS.AES.encrypt(e.target.result, key, {
                iv: iv,
                mode: CryptoJS.mode.CBC,
                padding: CryptoJS.pad.Pkcs7
            });

            a.attr('href', 'data:application/octet-stream,' + encrypted);
            a.attr('download', file.name + '.encrypted');

            step(4);
        };

        // This will encode the contents of the file into a data-uri.
        // It will trigger the onload handler above, with the result
        reader.readAsDataURL(file);
    }

客户端 c# 上的代码:

    var keybytes = Encoding.UTF8.GetBytes("8080808080808080");  
    var iv = Encoding.UTF8.GetBytes("8080808080808080");
    FileStream fsCrypt = new FileStream(inputFilePath, FileMode.Open);

    RijndaelManaged RMCrypto = new RijndaelManaged();
    //setting parameter for decrypting
    RMCrypto.Mode = CipherMode.CBC;
    RMCrypto.Padding = PaddingMode.PKCS7;
    RMCrypto.FeedbackSize = 128;


    RMCrypto.Key = keybytes;
    RMCrypto.IV = iv;

    CryptoStream cs = new CryptoStream(fsCrypt,
        RMCrypto.CreateDecryptor(RMCrypto.Key, RMCrypto.IV),
        CryptoStreamMode.Read);

    FileStream fsOut = new FileStream(outputFilePath, FileMode.Create);

    int data;
    while ((data = cs.ReadByte()) != -1)
        fsOut.WriteByte((byte)data);

    fsOut.Close();
    cs.Close();
    fsCrypt.Close();

我成功获得了加密二进制文件,但是当我在客户端解密它时,我得到了错误:

CryptographicException: Invalid input block size.
Mono.Security.Cryptography.SymmetricTransform.FinalDecrypt (System.Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/Mono.Security.Cryptography/SymmetricTransform.cs:462)
Mono.Security.Cryptography.SymmetricTransform.TransformFinalBlock (System.Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/Mono.Security.Cryptography/SymmetricTransform.cs:554)
System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock (System.Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Security.Cryptography/RijndaelManagedTransform.cs:94)
System.Security.Cryptography.CryptoStream.Read (System.Byte[] buffer, Int32 offset, Int32 count) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Security.Cryptography/CryptoStream.cs:205)
System.IO.Stream.ReadByte () (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/Stream.cs:168)
testCrypto.onBtnRunClick () (at Assets/TestCrypto/testCrypto.cs:51)
UnityEngine.Events.InvokableCall.Invoke (System.Object[] args)
UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters)
UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters)
UnityEngine.Events.UnityEvent.Invoke ()
UnityEngine.UI.Button.Press () (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44)
UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52)
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269)
UnityEngine.EventSystems.EventSystem:Update()

谁能为我提供一些解决这个问题的方法?谢谢!

4

1 回答 1

1

您的代码存在多个问题。主要问题是这'data:application/octet-stream,' + encrypted不是你想的那样。我不知道如何将密文转换为八位字节流数据,但这可能有效:

'data:application/octet-stream,' + CryptoJS.enc.Latin1.stringify(encrypted.ciphertext)
于 2017-03-06T19:44:31.350 回答