2017-03-06 9 views
0

서버 측에서 이진 파일을 암호화하고 클라이언트 측에서 C#으로 Unity에서 해독하려고합니다.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)