title: Decrypting a pan number encrypted in Java using c# Using a 48 character key
body: <p>I have been given a card pan which is encrypted by a third party. they have used java to encrypt the pan using a key of 48 characters.</p>
  
  <p>Using an online tool i can get the same result as they are seeing. but in code i am getting an "Specified key is not a valid size for this algorithm." error message.</p>
  
  <p>Can you please advise how i can get round this issue?</p>
  
  <p>code snippet below</p>
  
  <pre><code>    private void button1_Click(object sender, EventArgs e)
      {
          byte[] Results;
          UTF8Encoding UTF8 = new UTF8Encoding();
  
          MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
          byte[] TDESKey = UTF8Encoding.UTF8.GetBytes(key.Text);
  
          TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
  
          TDESAlgorithm.IV = new byte[TDESAlgorithm.BlockSize / 8];
          TDESAlgorithm.Key = TDESKey;
          TDESAlgorithm.Mode = CipherMode.ECB;
          TDESAlgorithm.Padding = PaddingMode.PKCS7;
  
          byte[] DataToDecrypt = Convert.FromBase64String(decrypt.Text);
  
          try
          {
              ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();
              Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length);
          }
          finally
          {
              TDESAlgorithm.Clear();
  
          }
  
        decryptedText.Text =  UTF8.GetString(Results);
  
  
      }
  </code></pre>
  
  <p>code used to Encrypt below </p>
  
  <pre><code>public static String encryptData(String toEncrypt, byte[] sharedKeyBytes){
      byte[] encrypted = new byte[0];
      try {
          DESedeKeySpec keySpec = new DESedeKeySpec(sharedKeyBytes);
  
          SecretKey key = SecretKeyFactory.getInstance(ALGORITHM_DESEDE).generateSecret(keySpec);
          Cipher encrypter = Cipher.getInstance(TRANSFORMSATION_DESEDE_PADDED);
          encrypter.init(Cipher.ENCRYPT_MODE, key);
  
          byte[] input = toEncrypt.getBytes(UTF_8);
  
          encrypted = encrypter.doFinal(input);
      } catch (Exception e) {
          throw new CryptoException("Exception caught when encrypting", e);
      }
      return new String(Base64.encodeBase64(encrypted));
  }
  </code></pre>
  
  <p>This is the equivalent i  need to produce in c#. this code works in java</p>
  
  <pre><code>public static String decryptTripleDES(String toDecrypt, byte[] customerSharedKeyBytes){
      byte[] decrypted = new byte[0];
  
      System.out.println(customerSharedKeyBytes.length);
      try {
          byte[] decodedValue = Base64.decodeBase64(toDecrypt.getBytes(UTF_8));
  
          DESedeKeySpec keySpec = new DESedeKeySpec(customerSharedKeyBytes);
  
          SecretKey key = SecretKeyFactory.getInstance(ALGORITHM_DESEDE).generateSecret(keySpec);
          Cipher decrypter = Cipher.getInstance(TRANSFORMSATION_DESEDE_PADDED);
          decrypter.init(Cipher.DECRYPT_MODE, key);
  
          decrypted = decrypter.doFinal(decodedValue);
      } catch (Exception e) {
          throw new CryptoException("Exception caught when decrypting", e);
      }
  
      return new String(decrypted);
  }
  </code></pre>
  
  <p>Please advise on how to best get round this issue.</p>
  
  <p>thanks</p>