1

I have a DLL in C# that encrypts and decrypts string texts (something basic), but now I need to implement the same encryption method in Java, so that some applications can encrypt data and send it to the library.

I can't modify the C# code, because it's already in production, but the Java don't, so please, any suggestion must be done at the Java side.

Basically I'm trying to implement the same C# encryption method in Java. Here are my C# codes:

NOTE: the passphrase, salt, etc. values obviously are just referential.

    const string PassPhrase = "IhDyHz6bgQyS0Ff1/1s="; 
    const string SaltValue = "0A0Qvv09OXd3GsYHVrA=";   
    const string HashAlgorithm = "SHA1";                
    const int PasswordIterations = 3;                 
    const string InitVector = "GjrlRZ6INgNckBqv";      
    const int KeySize = 256;


public static string Encrypt(string plainText)
    {

        byte[] initVectorBytes = Encoding.ASCII.GetBytes(InitVector);
        byte[] saltValueBytes = Encoding.ASCII.GetBytes(SaltValue);


        byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);


        PasswordDeriveBytes password = new PasswordDeriveBytes(
                                                        PassPhrase,
                                                        saltValueBytes,
                                                        HashAlgorithm,
                                                        PasswordIterations);


        byte[] keyBytes = password.GetBytes(KeySize / 8);


        RijndaelManaged symmetricKey = new RijndaelManaged();


        symmetricKey.Mode = CipherMode.CBC;


        ICryptoTransform encryptor = symmetricKey.CreateEncryptor(
                                                         keyBytes,
                                                         initVectorBytes);


        MemoryStream memoryStream = new MemoryStream();


        CryptoStream cryptoStream = new CryptoStream(memoryStream,
                                                     encryptor,
                                                     CryptoStreamMode.Write);

        cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);


        cryptoStream.FlushFinalBlock();


        byte[] cipherTextBytes = memoryStream.ToArray();


        memoryStream.Close();
        cryptoStream.Close();


        string cipherText = Convert.ToBase64String(cipherTextBytes);


        return cipherText;
    }


public static string Decrypt(string cipherText)
    {

        byte[] initVectorBytes = Encoding.ASCII.GetBytes(InitVector);
        byte[] saltValueBytes = Encoding.ASCII.GetBytes(SaltValue);


        byte[] cipherTextBytes = Convert.FromBase64String(cipherText);

        PasswordDeriveBytes password = new PasswordDeriveBytes(
                                                        PassPhrase,
                                                        saltValueBytes,
                                                        HashAlgorithm,
                                                        PasswordIterations);


        byte[] keyBytes = password.GetBytes(KeySize / 8);


        RijndaelManaged symmetricKey = new RijndaelManaged();


        symmetricKey.Mode = CipherMode.CBC;


        ICryptoTransform decryptor = symmetricKey.CreateDecryptor(
                                                         keyBytes,
                                                         initVectorBytes);


        MemoryStream memoryStream = new MemoryStream(cipherTextBytes);


        CryptoStream cryptoStream = new CryptoStream(memoryStream,
                                                      decryptor,
                                                      CryptoStreamMode.Read);


        byte[] plainTextBytes = new byte[cipherTextBytes.Length];


        int decryptedByteCount = cryptoStream.Read(plainTextBytes,
                                                   0,
                                                   plainTextBytes.Length);


        memoryStream.Close();
        cryptoStream.Close();


        string plainText = Encoding.UTF8.GetString(plainTextBytes,
                                                   0,
                                                   decryptedByteCount);


        return plainText;
    }

Here is my java code, it encrypts the data, but not at the same way as the C# encryption code, so when I try to decrypt it using the C# library it throws the exception: "Length of the data to decrypt is invalid"

    static final String PassPhrase = "IhDyHz6bgQyS0Ff1/1s=";   
    static final String SaltValue = "0A0Qvv09OXd3GsYHVrA=";    
    static final String HashAlgorithm = "SHA1";               
    static final int PasswordIterations = 3;                   
    static final String InitVector = "GjrlRZ6INgNckBqv";       
    static final int KeySize = 256;

public static String encrypt(String plainText)
{
    char[] password = PassPhrase.toCharArray();
    byte[] salt = SaltValue.getBytes();
    byte[] iv = InitVector.getBytes();
    byte[] ciphertext = new byte[0];

    SecretKeyFactory factory;
    try {
        factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");

        KeySpec spec = new PBEKeySpec(password, salt, PasswordIterations, 256);
        SecretKey tmp;

        tmp = factory.generateSecret(spec);

        SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secret);
        AlgorithmParameters params = cipher.getParameters();
        //iv = params.getParameterSpec(IvParameterSpec.class).getIV();
        ciphertext = cipher.doFinal(plainText.getBytes("UTF-8"));

    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvalidKeySpecException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    //catch (InvalidParameterSpecException e) {
    //  // TODO Auto-generated catch block
    //  e.printStackTrace();
    //} 
    catch (IllegalBlockSizeException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (BadPaddingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return Base64.encode(new String(ciphertext));
}

EDIT 1: I fixed the final byte array conversion to String in the Java code, as Jon Skeet suggested.

0

2 Answers 2

3

This is what's wrong, in the Java code:

return Base64.encode(ciphertext.toString());

You're calling toString() on the byte array, which will always give a string such as [B@3e25a5.

EDIT: Ooh, just noticed that you can change the Java side. Hooray.

Basically, you need to use a Base64 API which allows:

return Base64.encode(ciphertext);

I'm always disappointed in Base64 APIs which allow you to "encode" a string, to be honest... base64 fundamentally encodes binary data to text, and decodes text data to binary. Oh well...

Anyway, use this API (the encodeBytes method) if you need one which allows you to pass in a byte array.

I haven't checked over the actual encryption part in detail, but the C# code at least looks like it's doing the right thing in terms of encodings. It could do with using statements though :)

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

3 Comments

Thank you, you were right, there was an error in that part of the Java code, I fixed it and now I get the right encoded text, but still it cannot be decrypted using the C# code (same exception), for example I encrypt: Hello World with the posted C# method and get ["fawkThB32M8FsbOxn05KEw=="] and in Java it's much longer ["bigIxbgKxaHDizUuP8OlR1bCsMOnw78="] why could this be happening? Am I missing the Initial Vector somewhere it should be?
Things to try: #1 check if the used base64 variants follow the same rules wrt adding newlines, etc. #2 check if the encoded text can be decoded by java.
I checked the decoding results and they match on both sides, I'm pretty sure the problem is that the keys are not matching because of this: "PBKDF2WithHmacSHA1", .Net use PBKDF1 by default. I've been searching the web and found out that Bouncycastle can handle this by creating a key with PBKDF1 using this method somehow: PKCS5S1ParametersGenerator but sadly the post doesn't elaborate anymore on the solution to this. Any clues here?
1

Here is a C# example, you need the IterationCount and PaddingMode.None

protected void Page_Load(object sender, EventArgs e)
{
    string value = "";
    string password = "";
    string salt = "";
    string iv = "";


    byte[] vectorBytes = Convert.FromBase64String(Server.UrlDecode(iv)); 
    byte[] cipherText = Convert.FromBase64String(Server.UrlDecode(value));

    Rfc2898DeriveBytes key1 = new Rfc2898DeriveBytes(password, StringToByteArray(salt)); //same as PBKDF2WithHmacSHA1
    key1.IterationCount = 32;
    byte[] keyBytes = key1.GetBytes(16);

    string Answer = DecryptDataAES(cipherText, keyBytes, vectorBytes); //vectorBytes is good

    //litAnswer.Text = Answer;
}

public static string DecryptDataAES(byte[] cipherText, byte[] key, byte[] iv)
{
    string plaintext = null;

    using (Rijndael rijndael = Rijndael.Create())
    {
        rijndael.Key = key;
        rijndael.IV = iv;
        rijndael.Padding = PaddingMode.None;

        ICryptoTransform decryptor = rijndael.CreateDecryptor(rijndael.Key, rijndael.IV);

        // Create the streams used for decryption. 
        using (MemoryStream msDecrypt = new MemoryStream(cipherText))
        {
            using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
            {
                using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                {
                    plaintext = srDecrypt.ReadToEnd();
                }
            }
        }
    }
    return plaintext;
}

public static byte[] StringToByteArray(String hex)
{
    int NumberChars = hex.Length / 2;
    byte[] bytes = new byte[NumberChars];
    using (var sr = new StringReader(hex))
    {
        for (int i = 0; i < NumberChars; i++)
            bytes[i] =
              Convert.ToByte(new string(new char[2] { (char)sr.Read(), (char)sr.Read() }), 16);
    }
    return bytes;
}

1 Comment

This example helped me out. Not sure why, but for my situation, I didn't need to set the Padding. Otherwise this worked as needed. Thx.

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.