5 Replies Latest reply on Apr 22, 2010 11:08 AM by EJP

    AES Encryption In Java

    843811
      h3. Hello all,
      {color:#ff6600}
      I have a AES Encryption util class in C# code. I want to convert the same to Java code with all Parameters.I am sure that somebody did it already.
      Can any one can please give me the code for that.Thanks in advance........{color}
      #region Usings
      using System;
      using System.IO;
      using System.Security.Cryptography;
      using System.Text;
      #endregion
      
          /// <summary>
          /// Utility class that handles encryption
          /// </summary>
          public static class AESEncryption
          {
              #region Static Functions
              /// <summary>
              /// Encrypts a string
              /// </summary>
              /// <param name="PlainText">Text to be encrypted</param>
              /// <param name="Password">Password to encrypt with</param>
              /// <param name="Salt">Salt to encrypt with</param>
              /// <param name="HashAlgorithm">Can be either SHA1 or MD5</param>
              /// <param name="PasswordIterations">Number of iterations to do</param>
              /// <param name="InitialVector">Needs to be 16 ASCII characters long</param>
              /// <param name="KeySize">Can be 128, 192, or 256</param>
              /// <returns>An encrypted string</returns>
              public static string Encrypt(string PlainText, string Password, string Salt, string HashAlgorithm, int PasswordIterations, string InitialVector, int KeySize)
              {
                  try
                  {
                      byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector);
                      byte[] SaltValueBytes = Encoding.ASCII.GetBytes(Salt);
                      byte[] PlainTextBytes = Encoding.UTF8.GetBytes(PlainText);
                      PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations);
                      byte[] KeyBytes = DerivedPassword.GetBytes(KeySize / 8);
                      RijndaelManaged SymmetricKey = new RijndaelManaged();
                      SymmetricKey.Mode = CipherMode.CBC;
                      byte[] CipherTextBytes = null;
                      using (ICryptoTransform Encryptor = SymmetricKey.CreateEncryptor(KeyBytes, InitialVectorBytes))
                      {
                          using (MemoryStream MemStream = new MemoryStream())
                          {
                              using (CryptoStream CryptoStream = new CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write))
                              {
                                  CryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length);
                                  CryptoStream.FlushFinalBlock();
                                  CipherTextBytes = MemStream.ToArray();
                                  MemStream.Close();
                                  CryptoStream.Close();
                              }
                          }
                      }
                      return Convert.ToBase64String(CipherTextBytes);
                  }
                  catch (Exception a)
                  {
                      throw a;
                  }
              }
      
              /// <summary>
              /// Decrypts a string
              /// </summary>
              /// <param name="CipherText">Text to be decrypted</param>
              /// <param name="Password">Password to decrypt with</param>
              /// <param name="Salt">Salt to decrypt with</param>
              /// <param name="HashAlgorithm">Can be either SHA1 or MD5</param>
              /// <param name="PasswordIterations">Number of iterations to do</param>
              /// <param name="InitialVector">Needs to be 16 ASCII characters long</param>
              /// <param name="KeySize">Can be 128, 192, or 256</param>
              /// <returns>A decrypted string</returns>
              public static string Decrypt(string CipherText, string Password, string Salt, string HashAlgorithm, int PasswordIterations, string InitialVector, int KeySize)
              {
                  try
                  {
                      byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector);
                      byte[] SaltValueBytes = Encoding.ASCII.GetBytes(Salt);
                      byte[] CipherTextBytes = Convert.FromBase64String(CipherText);
                      PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations);
                      byte[] KeyBytes = DerivedPassword.GetBytes(KeySize / 8);
                      RijndaelManaged SymmetricKey = new RijndaelManaged();
                      SymmetricKey.Mode = CipherMode.CBC;
                      byte[] PlainTextBytes = new byte[CipherTextBytes.Length];
                      int ByteCount = 0;
                      using (ICryptoTransform Decryptor = SymmetricKey.CreateDecryptor(KeyBytes, InitialVectorBytes))
                      {
                          using (MemoryStream MemStream = new MemoryStream(CipherTextBytes))
                          {
                              using (CryptoStream CryptoStream = new CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read))
                              {
      
                                  ByteCount = CryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length);
                                  MemStream.Close();
                                  CryptoStream.Close();
                              }
                          }
                      }
                      return Encoding.UTF8.GetString(PlainTextBytes, 0, ByteCount);
                  }
                  catch (Exception a)
                  {
                      throw a;
                  }
              }
              #endregion
          }
      Thanks and Regards
      Vivek

      Edited by: Vivek.M.K on Apr 22, 2010 9:45 AM
        • 1. Re: AES Encryption In Java
          843811
          Vivek.M.K wrote:
          Can any one can please give me the code for that.
          :-) Subtlety is obviously not one of your strong points.

          You should note that though the class name is AESEncryption it actually performs Rijndael encryption and decryption. When the key size is 128 bits they they are the same but AES is not defined for 192 or 256 bit keys.

          You can create an equivalent to this class by using BouncyCastle libraries.
          • 2. Re: AES Encryption In Java
            EJP
            Can any one can please give me the code for that.
            This isn't a free coding service. You are expected to have read the Javadoc and made some sort of an effort to produce some Java code. If you want to hire a coder please feel free.
            • 3. Re: AES Encryption In Java
              843811
              Hi sabre,

              I have tried lots of codes. But tp provide more security we need all the parameters which we can see in C# code.I am looking for that.
              Can you please refer some sample codes?

              Thank You
              vivek
              • 4. Re: AES Encryption In Java
                843811
                Vivek.M.K wrote:

                I have tried lots of codes.
                You should do some reading first.
                But tp provide more security we need all the parameters which we can see in C# code.
                More security than what?
                I am looking for that.
                Can you please refer some sample codes?
                No! All the functionality you need can be constructed using the BouncyCastle libraries.
                • 5. Re: AES Encryption In Java
                  EJP
                  I have tried lots of codes.
                  So post it here, with specific questions, instead of asking people to do your work for you for nothing.