title: c# Bouncy Castle Blowfish Decryption - Pad block corrupted
body: <p>I am trying to decrypt a blowfish encrypted string with Bouncycastle in C#. </p>
  
  <p>I am able to easily encrypt and decrypt my own string but, unfortunately, I have to decrypt a string that is generated by another system. </p>
  
  <p>I AM able to recreate that same string with C# / Bouncycastle using the following but I have yet to decrypt it successfully.</p>
  
  <pre><code>    using Org.BouncyCastle.Crypto.Engines;
      using Org.BouncyCastle.Crypto.Paddings;
      using Org.BouncyCastle.Crypto.Parameters;
  </code></pre>
  
  <p>...</p>
  
  <pre><code>    static readonly Encoding Encoding = Encoding.UTF8;
  
      public string BlowfishEncrypt(string strValue, string key)
      {
          try
          {
              BlowfishEngine engine = new BlowfishEngine();
  
              PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(engine);
  
              KeyParameter keyBytes = new KeyParameter(Encoding.GetBytes(key));
  
              cipher.Init(true, keyBytes);
  
              byte[] inB = Encoding.GetBytes(strValue);
  
              byte[] outB = new byte[cipher.GetOutputSize(inB.Length)];
  
              int len1 = cipher.ProcessBytes(inB, 0, inB.Length, outB, 0);
  
              cipher.DoFinal(outB, len1);
  
              return BitConverter.ToString(outB).Replace("-", "");
          }
          catch (Exception)
          {
              return "";
          }
      }
  </code></pre>
  
  <p>Below is what I have for decryption at the moment. The line that fails with error "pad block corrupted" is <code>cipher.DoFinal(out2, len2);</code></p>
  
  <pre><code>    public string BlowfishDecrypt(string name, string keyString)
      {
  
  
          BlowfishEngine engine = new BlowfishEngine();
          PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(engine);
  
          StringBuilder result = new StringBuilder();
  
          cipher.Init(false, new KeyParameter(Encoding.GetBytes(keyString)));
  
          byte[] out1 = Convert.FromBase64String(name);
          byte[] out2 = new byte[cipher.GetOutputSize(out1.Length)];
  
          int len2 = cipher.ProcessBytes(out1, 0, out1.Length, out2, 0);
  
          cipher.DoFinal(out2, len2); //Pad block corrupted error happens here
  
          String s2 = BitConverter.ToString(out2);
  
          for (int i = 0; i &lt; s2.Length; i++) {
              char c = s2[i];
              if (c != 0) {
                  result.Append(c.ToString());
              }
          }
  
          return result.ToString();
  
      }
  </code></pre>
  
  <p>Any idea what I might be doing wrong in BlowfishDecrypt()?</p>
  
  <p>Note: 
  I converted the above (encrypt and decrypt) from a bouncycastle Java example I found somewhere; the encrypt works. The only difference I can see is that the Java example uses a StringBuffer where I use a StringBuilder.</p>