2012-06-08 61 views
1

我在解密中只有解密纔有完全解碼,加密它的工作完美。這個怎麼解決?河豚最後一塊不完整的解密

我使用充氣城堡。

這是我發現here

namespace BlowFishCS 

    { 
    public class BCEngine 
    { 
    private readonly Encoding _encoding; 
    private readonly IBlockCipher _blockCipher; 
    private PaddedBufferedBlockCipher _cipher; 
    private IBlockCipherPadding _padding; 

    public BCEngine(IBlockCipher blockCipher, Encoding encoding) 
    { 
     _blockCipher = blockCipher; 
     _encoding = encoding; 
    } 

    public BCEngine() 
    { 
     // TODO: Complete member initialization 
    } 

    public void SetPadding(IBlockCipherPadding padding) 
    { 
     if (padding != null) 
      _padding = padding; 
    } 

    public string Encrypt(string plain, string key) 
    { 
     byte[] result = BouncyCastleCrypto(true, _encoding.GetBytes(plain), key); 
     return Convert.ToBase64String(result); 
    } 

    public string Decrypt(string cipher, string key) 
    { 
     //cipher = cipher.Replace(' ', '+'); 
     byte[] result = BouncyCastleCrypto(false, Convert.FromBase64String(cipher), key); 
     return _encoding.GetString(result); 
    } 




    /// <summary> 
    /// 
    /// </summary> 
    /// <param name="forEncrypt"></param> 
    /// <param name="input"></param> 
    /// <param name="key"></param> 
    /// <returns></returns> 
    /// <exception cref="CryptoException"></exception> 


    private byte[] BouncyCastleCrypto(bool forEncrypt, byte[] input, string key) 
    { 

     try 
     { 

      _cipher = _padding == null ? 
     new PaddedBufferedBlockCipher(_blockCipher) : 
     new PaddedBufferedBlockCipher(_blockCipher, _padding); 

      // this line will make sure keyByte is 16 bytes long 
      byte[] keyByte = _encoding.GetBytes(key); 

      _cipher.Init(forEncrypt, new KeyParameter(keyByte)); 

      return _cipher.DoFinal(input); 
     } 
     catch (Org.BouncyCastle.Crypto.CryptoException ex) 
     { 
      throw new CryptoException(ex.Message); <here is exception last block incomplete in decryption 
     } 
    } 
} 

} 

我使用這種編碼和功能的代碼。

static Encoding _encoding = Encoding.ASCII; 
    static Pkcs7Padding pkcs = new Pkcs7Padding(); 
    static IBlockCipherPadding _padding = pkcs; 

public static string Encryption(string plain, string key, bool fips) 
    { 
     BCEngine bcEngine = new BCEngine(new BlowfishEngine(), _encoding); 
     bcEngine.SetPadding(_padding); 
     return bcEngine.Encrypt(plain, key); 
    } 

    public static string Decryption(string cipher, string key, bool fips) 
    { 
     BCEngine bcEngine = new BCEngine(new BlowfishEngine(), _encoding); 
     bcEngine.SetPadding(_padding); 
     return bcEngine.Decrypt(cipher, key); 
    } 

Exeption:

Org.BouncyCastle.Crypto.CryptoException was unhandled 
    Message=last block incomplete in decryption 
    Source=ID_Serwer 
    StackTrace: 
     at BlowFishCS.BCEngine.BouncyCastleCrypto(Boolean forEncrypt, Byte[] input, String key) in C:\Documents and Settings\WirtualOS\Pulpit\serever\ID_Serwer\Blowfish.cs:line 96 
     at BlowFishCS.BCEngine.Decrypt(String cipher, String key) in C:\Documents and Settings\WirtualOS\Pulpit\serever\ID_Serwer\Blowfish.cs:line 60 
     at ID_Serwer.ID_Serv_Main.Decryption(String cipher, String key, Boolean fips) in C:\Documents and Settings\WirtualOS\Pulpit\serever\ID_Serwer\ID_Serv_Main.cs:line 154 
     at ID_Serwer.ID_Serv_Main.process_cmd(String cmd) in C:\Documents and Settings\WirtualOS\Pulpit\serever\ID_Serwer\ID_Serv_Main.cs:line 163 
     at ID_Serwer.ID_Serv_Main.Main(String[] args) in C:\Documents and Settings\WirtualOS\Pulpit\serever\ID_Serwer\ID_Serv_Main.cs:line 137 
     at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
     at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() 
    InnerException: 

Exeption是表明解密後( 「測試」, 「HjcWtqDOBMo =」,真)

+0

如果您要求獲得有關例外的幫助,請發佈您獲得的例外。 –

+0

程序崩潰並在Visual Studio 2010 Express中解密時顯示此消息的最後一個塊不完整; P – user1445404

+0

您需要顯示異常,[這裏是一個很好的示例](http://stackoverflow.com/questions/5596510/getting-exception ),否則沒有人知道什麼是錯的。 –

回答

0
static Encoding _encoding = Encoding.ASCII; 

return Convert.ToBase64String(result); 

您應該加密使用相同的編碼和解密。使用System.Text.Encoding.Unicode來支持非ASCII字符。

+0

好吧,我將Encoding.ASCII更改爲Encoding.Unicode。 我應該添加/更改返回Convert.ToBase64String(result); ? – user1445404

+0

儘量使用無處不在_encoding.GetBytes和_encoding.GetString。 –

+0

執行它只是解密函數,當我將Convert.FromBase64String(密碼)更改爲_encoding.GetBytes(cipher)exeption message pad block corrupted – user1445404

相關問題