0

I have found a source code at stackoverflow on Rinjndael-256 encryption and decryption written in c#. It is using custom IV appending some extra string. Now I need a decryption method on Java platform. I have found some source code; tried to change and test that. Here is the encryption method on c#:

   public static string Encrypt(byte[] text, string key)
    {
        RijndaelManaged aes = new RijndaelManaged();
        aes.KeySize = 256;
        aes.BlockSize = 256;
        aes.Padding = PaddingMode.None;
        aes.Mode = CipherMode.CBC;

        aes.Key = Encoding.Default.GetBytes(key);
        aes.GenerateIV();

        string IV = ("-[--IV-[-" + Encoding.Default.GetString(aes.IV));

        ICryptoTransform AESEncrypt = aes.CreateEncryptor(aes.Key, aes.IV);
        byte[] buffer = text;

        return Convert.ToBase64String(Encoding.Default.GetBytes(Encoding.Default.GetString(AESEncrypt.TransformFinalBlock(buffer, 0, buffer.Length)) + IV));

    }

The decryption method on java which is not working for me is:

 public static String decrypt(byte[] cipherText, String encryptionKey) throws Exception{
    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
    SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
    cipher.init(Cipher.DECRYPT_MODE, key,new IvParameterSpec(IV.getBytes("UTF-8")));
    return new String(cipher.doFinal(cipherText),"UTF-8");
}

Edit 1: I have implemented the php code for decryption.

   function decrypt($text, $pkey)
{
    $key = $pkey;
    $text = base64_decode($text);
    $IV = substr($text, strrpos($text, "-[--IV-[-") + 9);
    $text = str_replace("-[--IV-[-" . $IV, "", $text);

    return rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, $IV), "\0");
}

Is there any manual way of implementing Rijndael-256 in Java? As someone said that

There is no support in any of the Sun JCE providers for anything other than Rijndael with the 128-bit blocksize

I don't have an option to use library

  • 1
    "not working for me", why? what is happening with it? errors? – Sayse Nov 05 '15 at 07:26
  • First problem: Don't use `Encoding.Default` anywhere if you want the code to be portable. The last line of your C# code should just be `return Convert.ToBase64String(AESEncrypt.TransformFinalBlock(buffer, 0, buffer.Length)) + IV;` Now, it's very unclear where you're getting the IV from in the Java code... there's no sign of you taking it off the end of the cipher-text... or indeed performing any base64 decoding. – Jon Skeet Nov 05 '15 at 07:32
  • Hint: if you provided a short but complete pair of programs demonstrating the problem, we'd be able to provide a complete solution... – Jon Skeet Nov 05 '15 at 07:32
  • Possible duplicate of [How to encrypt or decrypt with Rijndael and a block-size of 256 bits?](http://stackoverflow.com/questions/8083144/how-to-encrypt-or-decrypt-with-rijndael-and-a-block-size-of-256-bits) – Artjom B. Nov 05 '15 at 07:36
  • You can only "ping" one person with an @ in a comment so its probable that Jon Skeet didn't receive a notification. If you have other information relevant to your question then you should edit it to include it. – Sayse Nov 05 '15 at 08:13
  • Dear @ArtjomB. please check my edits. Hope it will make a clear sense of my trouble. – Mohammad Mahmudul Hasan Nov 05 '15 at 08:17
  • The question I linked to should be sufficient to answer your question. Have you tried it? – Artjom B. Nov 06 '15 at 09:51

0 Answers0