title: C# to Java Encryption and Decryption
body: <p>I'm trying to do an <strong>Encryption</strong> and <strong>Decryption</strong> in Java Android. </p>
  
  <p>Below is my code for C#, I need to convert it to Java Android. Can someone help me how to do it?</p>
  
  <p>I've been doing this for almost 2 days yet can't find any solutions on how to convert it. I'm newbie in Android.</p>
  
  <pre><code>public static string Encrypt(string toEncrypt)
          {
              byte[] keyArray;
              byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
  
              System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
  
              string key = "KEY";
  
              MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
              keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
              hashmd5.Clear();
  
              TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
              tdes.Key = keyArray;
              tdes.Mode = CipherMode.ECB;
              tdes.Padding = PaddingMode.PKCS7;
  
              ICryptoTransform cTransform = tdes.CreateEncryptor();
              byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
              tdes.Clear();
              return Convert.ToBase64String(resultArray, 0, resultArray.Length);
          }
  
          public static string Decrypt(string cipherString)
          {
              try
              {
                  byte[] keyArray;
                  byte[] toEncryptArray = Convert.FromBase64String(cipherString);
  
                  string key = "KEY";
  
                  MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                  keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                  hashmd5.Clear();
  
                  TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
                  tdes.Key = keyArray;
                  tdes.Mode = CipherMode.ECB;
                  tdes.Padding = PaddingMode.PKCS7;
  
                  ICryptoTransform cTransform = tdes.CreateDecryptor();
                  byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
  
                  tdes.Clear();
                  return UTF8Encoding.UTF8.GetString(resultArray);
              }
              catch (Exception)
              {
                  return "Invalid";
              }
          }
  </code></pre>