2

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:

public String saltTxt = "12345678";
public String Encrypt(String txt)
{
        byte[] data = Encrypt(Encoding.UTF8.GetBytes(txt));
        
        DESCryptoServiceProvider alg = new DESCryptoServiceProvider();

        alg.Key = Encoding.UTF8.GetBytes(saltTxt.ToCharArray(), 0, cprovider.KeySize / 8);
        alg.IV = new byte[8];

        MemoryStream ms = new MemoryStream();
        CryptoStream stem = new CryptoStream( ms, cprovider.CreateEncryptor(),CryptoStreamMode.Write);

        stem.Write(txt, 0, txt.Length);
        stem.FlushFinalBlock();

        data = ms.ToArray();
        
        return Convert.ToBase64String(data);
 }

Java Code:

public String saltTxt = "12345678";
public String Encrypt(String str) {
    try {
        KeySpec myKey = new DESKeySpec(saltTxt.getBytes("UTF8"));
        SecretKey key = SecretKeyFactory.getInstance("DES").generateSecret(myKey);
        Cipher ecipher = Cipher.getInstance("DES");
        ecipher.init(Cipher.ENCRYPT_MODE, key);

        byte[] data = str.getBytes("UTF8");

        byte[] crypt = ecipher.doFinal(data);
        
        return new BASE64Encoder().encode(crypt);
    } catch (Exception ex) {
    }

    return null;
} 

Any idea why it's not working as expected?

5
  • 1
    Sounds like a padding problem. See msdn.microsoft.com/en-us/library/… Commented Oct 15, 2012 at 11:34
  • The important thing is that roundtripping between the two should work. Does it? Commented Oct 15, 2012 at 11:39
  • @JonSkeet yes it is padding problem. what is the default padding mechanism of java? if i specify DES/CBC/PKCS5Padding in java part then both value are getting identical. Commented Oct 15, 2012 at 12:13
  • @NiksonKantiPaul: So doesn't that solve your problem? Commented Oct 15, 2012 at 12:27
  • yes, problem is solved. thanks Commented Oct 15, 2012 at 12:53

3 Answers 3

9

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.

// in C# 
DESCryptoServiceProvider alg = new DESCryptoServiceProvider();
alg.Mode = CipherMode.ECB;  // specified 

or

// in java
chiper = Cipher.getInstance("DES/CBC/PKCS5Padding");

don't change in both sides.

Sign up to request clarification or add additional context in comments.

Comments

1

You're probably seeing ISO 10126 padding, which appends random bytes to the plaintext to fill it up t oa multiple of the block size.
This behavior is by design.

1 Comment

@NiksonKantiPaul: Not in Java.
0

The code (Java/Android) bellow worke for me. I used the same approach on C#.

public static String Cripto(String Password)
{
    String PasswordCripto = "";
    try
    {
        String encryptionKey = "anyEncryptionString";
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.update(encryptionKey.getBytes("UTF-8"), 0, encryptionKey.length());
        byte[] encryptionKeyBytes = messageDigest.digest();

        SecretKeySpec Key = new SecretKeySpec(encryptionKeyBytes,"DESede");
        Cipher cipher = Cipher.getInstance("DESEDE/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, Key);
        byte[] encryptedBytes = cipher.doFinal(Password.getBytes("UTF-8"));

        PasswordCripto = new String(Base64.encode(encryptedBytes, Base64.DEFAULT), "UTF-8");
    } catch(Exception e) { }
    return PasswordCripto ;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.