title: Can I have equivalent dot net code from the java code
body: <p>I have one java code that needs to be converted to c# code.</p>
  <p><strong>Java Util class</strong></p>
  <pre><code> public class EncryptorDecryptorUtil {
       private int keySize;
        private Cipher cipher;
        
        public EncryptorDecryptorUtil(int keySize)
        {
          this.keySize = keySize;
          try
          {
            this.cipher = Cipher.getInstance(&quot;AES/CBC/PKCS5Padding&quot;);
          }
          catch (NoSuchAlgorithmException e)
          {
            e.printStackTrace();
          }
          catch (NoSuchPaddingException e)
          {
            e.printStackTrace();
          }
        }
        
        public String decrypt(String salt, String iv, String passphrase, String EncryptedText)
        {
          String decryptedValue = null;
          try
          {
            byte[] saltBytes = hexStringToByteArray(salt);
            SecretKeySpec sKey = (SecretKeySpec)generateKeyFromPassword(
              passphrase, saltBytes);
            byte[] ivBytes = hexStringToByteArray(iv);
            IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
            this.cipher.init(2, sKey, ivParameterSpec);
            byte[] decordedValue = new BASE64Decoder()
              .decodeBuffer(EncryptedText);
            byte[] decValue = this.cipher.doFinal(decordedValue);
            decryptedValue = new String(decValue);
          }
          catch (Exception e)
          {
            e.printStackTrace();
          }
          return decryptedValue;
        }
        
        public static SecretKey generateKeyFromPassword(String password, byte[] saltBytes)
          throws GeneralSecurityException
        {
          KeySpec keySpec = new PBEKeySpec(password.toCharArray(), saltBytes, 
            100, 128);
          SecretKeyFactory keyFactory = 
            SecretKeyFactory.getInstance(&quot;PBKDF2WithHmacSHA1&quot;);
          SecretKey secretKey = keyFactory.generateSecret(keySpec);
          return new SecretKeySpec(secretKey.getEncoded(), &quot;AES&quot;);
        }
        
        public static byte[] hexStringToByteArray(String s)
        {
          System.out.println(&quot;s:::::::&quot;+s);
          int len = s.length();
          byte[] data = new byte[len / 2];
          for (int i = 0; i &lt; len; i += 2) {
            data[(i / 2)] = 
              ((byte)((Character.digit(s.charAt(i), 16) &lt;&lt; 4) + Character.digit(s.charAt(i + 1), 16)));
          }
          return data;
        }
  }
  </code></pre>
  <p><strong>Java Main methods</strong></p>
  <pre><code>public class EncryptionDecryption {
      
      @SuppressWarnings(&quot;unchecked&quot;)
      public static void main(String[] args) {
          JSONObject json = new JSONObject();
          json.put(&quot;applicationNo&quot;,&quot;PNB00000000000004016&quot;);
          json.put(&quot;custID&quot;,&quot;PNB000000004016&quot;);
          
          
          String input = json.toJSONString();
          
          String password = &quot;46ea428a97ba4c3094fc66e112d1d678&quot;;
  
          EncryptionDecryption enc = new EncryptionDecryption();
  
          String encryptMessage= enc.encryptMessage(input, password);
  
          System.out.println(&quot;Encrypted Message :&quot;+encryptMessage);
  
          String encrypt2Message = &quot;8b7a1e90d701f55c49e22135eed31c94 89f9af1e83e8c83bdf603f5428ba6f14 0R2A2JDkY8tFR1FZojY7Su9CVI9zjw4yjr/lRElwU75MF5e0LxgOb+Y/DOyVFNd89Ra4YADIZdopLZ5a59Z2BgEjMpSn27tqnGfFvWtfm+eh+A/aVcB2YwfD9rdsd67x6xUb8kmfL7qnO/uxaHyQtqlvwpNRBVjyfRlk1wPfaxyOQa0oEiWRmUXYxEoJ651EhYtHeHKmII7qzDgcioIYUlsBgZUjOu0sNEdiwSvvHbw=&quot;;
          String decryptMessage = enc.decryptMessage(encrypt2Message, password);
          System.out.println(&quot;decryptMessage Message :&quot;+decryptMessage);
      }
  
  
       public String encryptMessage(String txtToEncrypt, String passphrase)
        {
           System.out.println(&quot;encryptMessage :txtToEncrypt :&quot;+txtToEncrypt);
          String combineData = &quot;&quot;;
          try
          {
            Cipher cipher = Cipher.getInstance(&quot;AES/CBC/PKCS5Padding&quot;);
            String saltHex = getRandomHexString(32);
            String ivHex = getRandomHexString(32);
            byte[] salt = hexStringToByteArray(saltHex);
            byte[] iv = hexStringToByteArray(ivHex);
            SecretKeySpec sKey = (SecretKeySpec)generateKeyFromPassword(
              passphrase, salt);
            cipher.init(1, sKey, new IvParameterSpec(iv));
            byte[] utf8 = txtToEncrypt.getBytes(&quot;UTF-8&quot;);
            byte[] enc = cipher.doFinal(utf8);
            combineData = saltHex + &quot; &quot; + ivHex + &quot; &quot; + 
              new BASE64Encoder().encode(enc);
          }
          catch (Exception e)
          {
            e.printStackTrace();
          }
          combineData = combineData.replaceAll(&quot;\n&quot;, &quot;&quot;).replaceAll(&quot;\t&quot;, &quot;&quot;).replaceAll(&quot;\r&quot;, &quot;&quot;);
          return combineData;
        }
        
  
  
        public String decryptMessage(String str, String myKey)
        {
          String decrypted = null;
          try
          {
            if ((str != null) &amp;&amp; (str.contains(&quot; &quot;)))
            {
              String salt = str.split(&quot; &quot;)[0];
              String iv = str.split(&quot; &quot;)[1];
              String encryptedText = str.split(&quot; &quot;)[2];
              
              EncryptorDecryptorUtil dec = new EncryptorDecryptorUtil(128);
              decrypted = dec.decrypt(salt, iv, myKey, encryptedText);
            }
            else
            {
              decrypted = str;
            }
          }
          catch (Exception e)
          {
            e.printStackTrace();
          }
          return decrypted;
        }
        
        public static String getRandomHexString(int numchars)
        {
          Random r = new Random();
          StringBuilder sb = new StringBuilder();
          while (sb.length() &lt; numchars) {
            sb.append(Integer.toHexString(r.nextInt()));
          }
          return sb.toString().substring(0, numchars);
        }
        
        public static byte[] hexStringToByteArray(String s)
        {
          int len = s.length();
          byte[] data = new byte[len / 2];
          for (int i = 0; i &lt; len; i += 2) {
            data[(i / 2)] = 
              ((byte)((Character.digit(s.charAt(i), 16) &lt;&lt; 4) + Character.digit(s.charAt(i + 1), 16)));
          }
          return data;
        }
        
  
  
        public static SecretKey generateKeyFromPassword(String password, byte[] saltBytes)
          throws GeneralSecurityException
        {
          KeySpec keySpec = new PBEKeySpec(password.toCharArray(), saltBytes, 
            100, 128);
          SecretKeyFactory keyFactory = 
            SecretKeyFactory.getInstance(&quot;PBKDF2WithHmacSHA1&quot;);
          SecretKey secretKey = keyFactory.generateSecret(keySpec);
          return new SecretKeySpec(secretKey.getEncoded(), &quot;AES&quot;);
        }
  }
  </code></pre>
  <p><strong>I have tried below code in .net</strong></p>
  <pre><code>public static byte[] hexStringToByteArray(string hexString)
          {
  
             
              byte[] data = new byte[hexString.Length / 2];
              for (int index = 0; index &lt; data.Length; index++)
              {
                  string byteValue = hexString.Substring(index * 2, 2);
                  data[index] = byte.Parse(byteValue, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
              }
  
              return data;
  
  
          }
  
  
   public string generateKey(String password, byte[] saltBytes)
          {
             
              int iterations = 100;
              var rfc2898 =
              new System.Security.Cryptography.Rfc2898DeriveBytes(password, saltBytes, iterations);
              byte[] key = rfc2898.GetBytes(16);
              String keyB64 = Convert.ToBase64String(key);
              return keyB64;
          }
  </code></pre>
  <p><strong>Decrypt method in c#</strong></p>
  <pre><code>public string DecryptAlter(string salt, string iv, string passphrase, string EncryptedText)
      {
      string decryptedValue = null;
      try
      {
          byte[] saltBytes = hexStringToByteArray(salt);
  
          string sKey = generateKey(passphrase, saltBytes);
          byte[] ivBytes = hexStringToByteArray(iv);
  
  
          byte[] keyBytes = System.Convert.FromBase64String(sKey);
  
  
          AesManaged aesCipher = new AesManaged();
          aesCipher.IV = ivBytes;
          aesCipher.KeySize = 128;
          aesCipher.BlockSize = 128;
          aesCipher.Mode = CipherMode.ECB;
          aesCipher.Padding = PaddingMode.PKCS7;
          byte[] b = System.Convert.FromBase64String(EncryptedText);
          ICryptoTransform decryptTransform = aesCipher.CreateDecryptor(keyBytes, ivBytes);
          byte[] plainText = decryptTransform.TransformFinalBlock(b, 0, b.Length);
  
          var res = System.Text.Encoding.UTF8.GetString(plainText);
          return res;
      }
      catch (Exception e)
      {
          var k = e.Message;
      }
      return &quot;&quot;;
  }
  </code></pre>
  <p><strong>Its not working.  Please help me</strong></p>
  <p>The Encryption password is  “46ea428a97ba4c3094fc66e112d1d678”</p>
  <p>Encrypted Text -    4cdf7b17b7db00d7911498dec913d3e4 1e55c4e950b772685ccfdb831c82fede SCQXvM7GeKxP0jLtX5xbuF0WvBC/C81wwxtYNduUe9lVzaYztaJ8ifivjaCBWd7O2zSa+/A+vtFfdSWSnN5+RcjWka42QQl4f+yZ8C1Y/efIsUlDVXBXmSEjSUp/4sflXNz7qg62Ka+atpj0aiG6QvU+T5tnafmsDhx/M3zE+Tg=</p>
  <p><strong>Now need to decrypt it.</strong></p>