繁体   English   中英

CryptographicException:RijndaelManaged解密二进制文件时输入块大小无效

[英]CryptographicException: Invalid input block size when decrypt a binary file by RijndaelManaged

我想通过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()

有人可以为我提供一些解决方案吗? 谢谢!

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

'data:application/octet-stream,' + CryptoJS.enc.Latin1.stringify(encrypted.ciphertext)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM