Skip to content
Advertisement

C# and Java DES Encryption value are not identical

I am trying to encrypt the same data using C# and Java. If the data is more than 7 bytes then Java and C#’s encrypted value are not identical.

  • Input 1: a
    java output: FrOzOp/2Io8=
    C# output: FrOzOp/2Io8=

  • Input 2: abc
    j : H9A/ahl8K7I=
    c#: H9A/ahl8K7I=

  • Input 3: aaaaaaaa (Problem)
    j : Gxl7e0aWPd7j6l7uIEuMxA==
    c#: Gxl7e0aWPd7sf1xR6hK4VQ==

Here is the implementation of C# and Java methods.
C# code:

JavaScript

Java Code:

JavaScript

Any idea why it’s not working as expected?

Advertisement

Answer

The problem was in mode of encryption.

SunJCE provider uses ECB as the default mode, and PKCS5Padding as the default padding scheme for DES, DES-EDE and Blowfish ciphers. (JCA Doc)

and

In .Net, The default operation mode for the symmetric algorithm is CipherMode.CBC and default padding is PaddingMode.PKCS7. (msdn..SymmetricAlgorithm)

The following changes resolve the problem.

JavaScript

or

JavaScript

don’t change in both sides.

Advertisement