2017-03-06 83 views
0

我想通過javascript在服務器端加密二進制文件,並在Unity中通過c#解密客戶端。CryptographicException:通過RijndaelManaged解密二進制文件時輸入塊大小無效

代碼服務器端:

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() 

任何人都可以爲我提供一些解決方案來解決這個問題嗎? 謝謝!

回答

1

您的代碼有多個問題。主要問題是'data:application/octet-stream,' + encrypted不是你認爲的。我不知道如何將密文轉換爲八位字節流傳輸數據,但這可能會起作用:

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