在我的Windows Phone 8應用程序中,我需要使用DESede/CBC/PKCS5Padding和PBKDF2密鑰對數據進行加密 - 解密。
我發現瞭如何使用Bouncy Castle進行加密的示例:
http://www.go4expert.com/articles/bouncy-castle-net-implementation-triple-t24829/
我能夠加密我的數據(文本),但我無法使用此代碼解密我的加密數據。 當我試圖調用DoFinal()時,它會拋出異常「pad block corrupted」。
也許我錯過了一些解密的東西?
也許有人知道使用DESede/CBC/PKCS5Padding與PBKDF2密鑰進行加密的Windows Phone 8上的替代庫(方式)?C#Bouncy Castle不解密,例外:填充塊損壞
using Raksha.Crypto;
using Raksha.Crypto.Engines;
using Raksha.Crypto.Modes;
using Raksha.Crypto.Paddings;
using Raksha.Crypto.Parameters;
using System;
using System.Security.Cryptography;
using System.Text;
namespace MyNamespace
{
class EncryptDecrypt
{
Rfc2898DeriveBytes key;
BufferedBlockCipher cipherEncrypt;
BufferedBlockCipher cipherDecrypt;
private readonly int ITERACTIONCOUNT = 1000;
private readonly int KEY_LENGTH = 24;
public EncryptDecrypt(string passPhrase, string salt)
{
byte[] SALT = Encoding.UTF8.GetBytes(salt);
key = new Rfc2898DeriveBytes(passPhrase, SALT, ITERACTIONCOUNT);
DesEdeEngine desede = new DesEdeEngine();
cipherEncrypt = new PaddedBufferedBlockCipher(new CbcBlockCipher(desede));
cipherDecrypt = new PaddedBufferedBlockCipher(new CbcBlockCipher(desede));
DesEdeParameters p = new DesEdeParameters(key.GetBytes(KEY_LENGTH));
cipherEncrypt.Init(true, p);
cipherDecrypt.Init(false, p);
}
public byte[] Encrypt(byte[] dataToEncrypt)
{
try
{
byte[] outbytes = cipherEncrypt.DoFinal(dataToEncrypt);
return outbytes;
}
catch(Exception ex)
{
System.Diagnostics.Debug.WriteLine("Encrypt() Exception: " + ex.Message);
return new byte[] { 0 };
}
}
public byte[] Decrypt(byte[] dataToDecrypt)
{
try
{
byte[] result = cipherDecrypt.DoFinal(dataToDecrypt);
return result;
}
catch(CryptoException ex)
{
System.Diagnostics.Debug.WriteLine("Decrypt() Exception: " + ex.Message);
// ex.Message: pad block corrupted
return new byte[] { 0 };
}
}
}
您是否以某種方式在某處存儲或傳輸密文(加密函數的輸出)? –
@owlstead在這種情況下,我只是把加密的字節解密函數沒有任何改變(測試),如:加密解密加密=新的加密解密(「000000」,「Adobe Photoshop」); byte [] encrypted = encrypt.Encrypt(Encoding.UTF8.GetBytes(「some text」)); byte [] decrypted = encrypt.Decrypt(encrypted);並且,當我在解密函數中調用DoFinal()時,它會拋出異常「pad block corrupted」 – tsiganoff
嗯,雖然我不知道'Raksha.Crypto'是什麼,但我卻難倒了。 –