2016-10-17 10 views
0

私はC#のBouncycastleでブローフィッシング暗号化文字列を解読しようとしています。C#Bouncy Castle Blowfish Decryption - パッドブロックが壊れています

私は自分の文字列を簡単に暗号化して解読できますが、残念ながら別のシステムで生成された文字列を解読する必要があります。

私は、同じ文字列をC#/ Bouncycastleで次のように再作成することができますが、まだそれを正常に復号化する必要があります。以下は

using Org.BouncyCastle.Crypto.Engines; 
    using Org.BouncyCastle.Crypto.Paddings; 
    using Org.BouncyCastle.Crypto.Parameters; 

...

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 ""; 
     } 
    } 

私は、現時点では復号化に持っているものです。 「壊れたパッドブロック」エラーで失敗するラインはcipher.DoFinal(out2, len2);

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 < s2.Length; i++) { 
      char c = s2[i]; 
      if (c != 0) { 
       result.Append(c.ToString()); 
      } 
     } 

     return result.ToString(); 

    } 

私は)(BlowfishDecryptに間違っているかもしれないものの任意のアイデアですか?

注: 上記の(暗号化と復号化)を私がどこかで見つけたバウンシーキャスルJavaの例から変換しました。暗号化が機能します。私が見ることのできる唯一の違いは、Javaの例でStringBuilderを使用するStringBufferを使用していることです。

+0

'BlowfishDecrypt'の' name'は 'BlowfishEncrypt'の暗号化された暗号文です。その場合、エンコーディングの不一致があります。あなたは暗号化中に暗号文をHexにエンコードしていますが、復号化中はBase64から復号しようとします。 1つのエンコーディングを選択し、それに固執する。 –

+0

はい、申し訳ありません。 "name"の例は、BlowfishEncrypt()の結果になります。 あなたが言っていることは理にかなっていますが、私はそれを解決する方法が完全にはわかりません。私は 'byte [] out1 = Convert.FromBase64String(name);を' byte [] out1 = Encoding.GetBytes(name); 'に変更しましたが、同じエラーが発生します。 – p0laris

+0

これは長いことでしたが、明らかに 'Hex.Decode(name)'だったはずです。 ありがとうございました! – p0laris

答えて

0

ありがとう、Artjom B!

byte[] out1 = Convert.FromBase64String(name); 

はそこから

byte[] out1 = Hex.Decode(name); 

されている必要があり、私がしなければならなかったすべての文字列に六角を変換しました。

関連する問題