我有一个例外,最后一个块在解密中不完整,只有解密,加密它的工作完美。如何解决这个问题?
我用充气城堡。
这是我在这里找到的代码
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);
}
例外:
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("test", "HjcWtqDOBMo=", true)