CryptographicException

Options
alleopold
edited July 2012 in DotNet
I'm sorry to raise questions so often, but I'm really in trouble now.
I'm using UNITY to develop a web game with photon.
When the client establishes encryption with Photon server, an exception may appear:
System.Security.Cryptography.CryptographicException: Bad PKCS7 padding. Invalid length 182.
  at Mono.Security.Cryptography.SymmetricTransform.ThrowBadPaddingException (PaddingMode padding, Int32 length, Int32 position) [0x00000] in <filename unknown>:0 
  at Mono.Security.Cryptography.SymmetricTransform.FinalDecrypt (System.Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) [0x00000] in <filename unknown>:0 
  at Mono.Security.Cryptography.SymmetricTransform.TransformFinalBlock (System.Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) [0x00000] in <filename unknown>:0 
  at System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock (System.Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) [0x00000] in <filename unknown>:0 
  at Photon.SocketServer.Security.DiffieHellmanCryptoProvider.Decrypt (System.Byte[] data, Int32 offset, Int32 count) [0x00000] in <filename unknown>:0 
  at ExitGames.Client.Photon.PeerBase.DeserializeMessageAndCallback (System.Byte[] inBuff) [0x00000] in <filename unknown>:0 
UnityEngine.Debug:Log(Object)
PhotonClient:DebugReturn(String) (at Assets/Network/PhotonClient.cs:153)
PhotonClient:DebugReturn(DebugLevel, String) (at Assets/Network/PhotonClient.cs:138)
ExitGames.Client.Photon.PeerBase:DeserializeMessageAndCallback(Byte[])
ExitGames.Client.Photon.EnetPeer:DispatchIncomingCommands()
ExitGames.Client.Photon.PhotonPeer:DispatchIncomingCommands()
Game:Update() (at Assets/Network/Game.cs:143)
It does not appear every time, but often.
Anyone could help me with this? Thanks.

Comments

  • Tobias
    Options
    This might be related to a change we introduced in Photon 3 but I didn't know this was available to anyone already.
    Please let us know which server and client you are using.
  • Tobias wrote:
    This might be related to a change we introduced in Photon 3 but I didn't know this was available to anyone already.
    Please let us know which server and client you are using.
    I'm using Photon-Server-SDK_v3-0-6-2188-RC3, Instanceloadbalancing, and Photon-Unity3D_v3-0-0-0_SDK-RC1 as client.
  • Tobias
    Options
    Ok, so that has nothing to do with our latest changes then.
    How often does this happen and under which conditions? Is there a way we can reproduce this?
  • It does not happen regularly.
    As I remember, yesterday it happened several times, but today it hasn't happened even once until now.

    The flow of the connect operations of my client is as follows:
    1.Start client,when click "Connect" button, call Peer.Connect(),
    2.When receiving StatusCode.Connect in OnStatusChanged(),call Peer.EstablishEncryption(),
    3.When receiving StatusCode.EncryptionEstablished in OnStatusChanged(), raise other operations using encryption.
    4.If not receiving StatusCode.EncryptionEstablished or StatusCode.EncryptionFailedToEstablish in 5 seconds after the first calling of Peer.EstablishEncryption(), it will call Peer.EstablishEncryption() again.
    (I'm not sure if I have described it clearly..my English is not very good.)

    The CryptographicException may happen on step 2.(I cannot remember it clearly, and I cannot reproduce it now, but most possibly on step 2)
    The connect flow never changes, but the exception just comes out after clicking the "Connect" button, sometimes.

    Is there any possibility that the second Peer.EstablishEncryption() call cause the exception?
    But I tried to call Peer.EstablishEncryption() twice at a time like this:
    Peer.EstablishEncryption();
    Peer.EstablishEncryption();
    and the result is receiving the correct callbacks of both establish calls, the exception still not coming out.
  • Tobias
    Options
    Thanks for the feedback. I will try to reproduce this. It might be a racing condition between setting up the shared secret and receiving the first encrypted message (which would explain the "Decrypt" listed in the stacktrace).
  • Hello guys!
    We've encountered very similar problem trying to communicate from unity client to our Photon MMO implementation.
    Operation flows as a usual -
    1. Connect()
    2. Received OnPeerStatusCallback(Connect)
    3. Call Peer.EstablishEncryption()
    4. Received OnPeerStatusCallback(EncryptionEstablished)
    5. Imediately after 4. we send operation with SendReliable=true and doEncryption=true
    And... Sometimes in debug mode (approx. once per 10-20 connects) i catch error on a server as follows:

    {"Padding is invalid and cannot be removed."}
    at System.Security.Cryptography.RijndaelManagedTransform.DecryptData(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[]& outputBuffer, Int32 outputOffset, PaddingMode paddingMode, Boolean fLast)
    at System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
    at Photon.SocketServer.Security.RijndaelCryptoProvider.Decrypt(Byte[] data, Int32 offset, Int32 count) in c:\build\photon-socketserver-sdk_3.0\src\Photon.SocketServer\Security\RijndaelCryptoProvider.cs:line 147
    at Photon.SocketServer.Rpc.Protocols.GpBinaryByte.GpBinaryByteProtocolV16.TryParseOperationRequestEncrypted(Byte[] data, ICryptoProvider cryptoProvider, OperationRequest& operationRequest) in c:\build\photon-socketserver-sdk_3.0\src\Photon.SocketServer\Rpc\Protocols\GpBinaryByte\GpBinaryByteProtocolV16.cs:line 500
    at Photon.SocketServer.PeerBase.OnReceiveInternal(Byte[] data, SendParameters sendParameters) in c:\build\photon-socketserver-sdk_3.0\src\Photon.SocketServer\PeerBase.cs:line 623
    at Photon.SocketServer.PeerBase.<>c__DisplayClass3.<Photon.SocketServer.IManagedPeer.Application_OnReceive>b__2() in c:\build\photon-socketserver-sdk_3.0\src\Photon.SocketServer\PeerBase.cs:line 554
    at (Object )
    at ExitGames.Concurrency.Core.DefaultExecutor.Execute(Action toExecute) in c:\Dev\exitgames-libs\src\Core\Concurrency\Core\DefaultExecutor.cs:line 35
    at ExitGames.Concurrency.Core.DefaultExecutor.Execute(List`1 toExecute) in c:\Dev\exitgames-libs\src\Core\Concurrency\Core\DefaultExecutor.cs:line 19
    at ExitGames.Concurrency.Fibers.PoolFiber.Flush(Object ) in c:\Dev\exitgames-libs\src\Core\Concurrency\Fibers\PoolFiber.cs:line 16777215
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
    at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
    at System.Threading.ThreadPoolWorkQueue.Dispatch()
    at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

    Without debugging it just looks like server listens commands and then just does nothing... =\
    We've increased time lag between onEncryptionEstablished and SendOperation to 1 sec. To give client and server some time to communicate and avoid Race Effect. It does not helps.
  • Hello all,

    I also get also the same exception sporadically on the client side, although I use the Unity plugin (server side is Photon v2 current version).
    Can someone help? That would be really great.

    Greetings

    alleopold wrote:
    I'm sorry to raise questions so often, but I'm really in trouble now.
    I'm using UNITY to develop a web game with photon.
    When the client establishes encryption with Photon server, an exception may appear:
    System.Security.Cryptography.CryptographicException: Bad PKCS7 padding. Invalid length 182.
      at Mono.Security.Cryptography.SymmetricTransform.ThrowBadPaddingException (PaddingMode padding, Int32 length, Int32 position) &#91;0x00000&#93; in &lt;filename unknown&gt;:0 
      at Mono.Security.Cryptography.SymmetricTransform.FinalDecrypt (System.Byte&#91;&#93; inputBuffer, Int32 inputOffset, Int32 inputCount) &#91;0x00000&#93; in &lt;filename unknown&gt;:0 
      at Mono.Security.Cryptography.SymmetricTransform.TransformFinalBlock (System.Byte&#91;&#93; inputBuffer, Int32 inputOffset, Int32 inputCount) &#91;0x00000&#93; in &lt;filename unknown&gt;:0 
      at System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock (System.Byte&#91;&#93; inputBuffer, Int32 inputOffset, Int32 inputCount) &#91;0x00000&#93; in &lt;filename unknown&gt;:0 
      at Photon.SocketServer.Security.DiffieHellmanCryptoProvider.Decrypt (System.Byte&#91;&#93; data, Int32 offset, Int32 count) &#91;0x00000&#93; in &lt;filename unknown&gt;:0 
      at ExitGames.Client.Photon.PeerBase.DeserializeMessageAndCallback (System.Byte&#91;&#93; inBuff) &#91;0x00000&#93; in &lt;filename unknown&gt;:0 
    UnityEngine.Debug:Log(Object)
    PhotonClient:DebugReturn(String) (at Assets/Network/PhotonClient.cs:153)
    PhotonClient:DebugReturn(DebugLevel, String) (at Assets/Network/PhotonClient.cs:138)
    ExitGames.Client.Photon.PeerBase:DeserializeMessageAndCallback(Byte&#91;&#93;)
    ExitGames.Client.Photon.EnetPeer:DispatchIncomingCommands()
    ExitGames.Client.Photon.PhotonPeer:DispatchIncomingCommands()
    Game:Update() (at Assets/Network/Game.cs:143)
    
    It does not appear every time, but often.
    Anyone could help me with this? Thanks.
  • Tobias
    Options
    We finally found the cause for this issue and think it's solved. The special Unity implementation of BigInteger (needed for encryption) produced the wrong byte-array in some cases, which led to this issues.

    Photon Unity Networking v1.7 is already released with a new library and the "stand alone" Unity SDK will be released as v3.0.0.9 today.
  • Morton
    Options
    Hi again!
    Well... Another project. Now without Unity at all. Just .Net client and Photon server. Same problem.
    System.Security.Cryptography.CryptographicException: Padding is invalid and cann
    ot be removed.
       at System.Security.Cryptography.RijndaelManagedTransform.DecryptData(Byte&#91;&#93; i
    nputBuffer, Int32 inputOffset, Int32 inputCount, Byte&#91;&#93;& outputBuffer, Int32 out
    putOffset, PaddingMode paddingMode, Boolean fLast)
       at System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(
    Byte&#91;&#93; inputBuffer, Int32 inputOffset, Int32 inputCount)
       at Photon.SocketServer.Security.DiffieHellmanCryptoProvider.Decrypt(Byte&#91;&#93; da
    ta, Int32 offset, Int32 count) in C:\dev\dotNet-sdks\PhotonDotNet\DiffieHellmanC
    ryptoProvider.cs:line 131
       at ExitGames.Client.Photon.PeerBase.DeserializeMessageAndCallback(Byte&#91;&#93; inBu
    ff) in C:\dev\dotNet-sdks\PhotonDotNet\PeerBase.cs:line 484
       at System.Security.Cryptography.RijndaelManagedTransform.DecryptData(Byte&#91;&#93; i
    nputBuffer, Int32 inputOffset, Int32 inputCount, Byte&#91;&#93;& outputBuffer, Int32 out
    putOffset, PaddingMode paddingMode, Boolean fLast)
       at System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(
    Byte&#91;&#93; inputBuffer, Int32 inputOffset, Int32 inputCount)
       at Photon.SocketServer.Security.DiffieHellmanCryptoProvider.Decrypt(Byte&#91;&#93; da
    ta, Int32 offset, Int32 count) in C:\dev\dotNet-sdks\PhotonDotNet\DiffieHellmanC
    ryptoProvider.cs:line 131
       at ExitGames.Client.Photon.PeerBase.DeserializeMessageAndCallback(Byte&#91;&#93; inBu
    ff) in C:\dev\dotNet-sdks\PhotonDotNet\PeerBase.cs:line 484
    
    Photon3DotNet.dll version 3.0.1.5
    PhotonDotNet.dll version 6.3.1.0
    ExitGamesLibs.dll version 0.9.9.3
    Server Win7 64bit - Photon Version: 3.0.15.1343
    :?
    Now i'm trying to catch this error and try completely destroy and reestablish connection... But it will not solve the problem...
  • Tobias
    Options
    This should be a fixed issue of older versions.

    You are using Unity? Then please update to the latest Photon Unity SDK v3.0.1.11.
    Also get rid of PhotonDotNet.dll version 6.3.1.0. This is not needed in a Unity project.
  • Morton
    Options
    No, we are not using Unity. Only .Net
  • Tobias
    Options
    DotNet in windows or with Mono?
    Which client SDK?
  • Morton
    Options
    Client is Win32 Application written on C# (.Net)
    Libraries included in client are libraries from Windows DotNet SDK:
    Photon3DotNet 3.0.1.5
    ExitGamesLibs 0.9.9.3
    ExitGames.Logging.Log4Net 0.9.9.3
    log4net 1.2.10.1

    Now i am catching client Event OnStatusChanged and if status is StatusCode.EncryptionFailedToEstablish i force client to disconnect, connect, and establish encryption again.
  • Tobias
    Options
    You could update the Photon3DotNet lib to the lastest one from the DotNet client SDK. I'm not 100% sure this is fixed then but there's a good chance. At least when you use the latest server SDK (or the cloud) as well.