Bouncy Castle 错误 "InvalidCipherTextException: data start wrong 64"
Error "InvalidCipherTextException: data start wrong 64" with Bouncy Castle
我正在使用 BouncyCastle 加密和解密一些数据,但是当单词的长度太长时(我不知道确切的值),我得到了这个错误 "InvalidCipherTextException: data start wrong 64"
这是我的 Class 的 Encription:
public static class Crypto
{
public static IAsymmetricBlockCipher CriarCipher(byte[] encodingParam)
{
// Creating the RSA algorithm object
IAsymmetricBlockCipher cipher = new OaepEncoding(new RsaEngine(), new Sha256Digest(), encodingParam);
return cipher;
}
public static AsymmetricCipherKeyPair CreatePair()
{
RsaKeyPairGenerator rsaKeyPairGnr = new RsaKeyPairGenerator();
rsaKeyPairGnr.Init(new KeyGenerationParameters(new SecureRandom(), 1024));
AsymmetricCipherKeyPair keyPair = rsaKeyPairGnr.GenerateKeyPair();
return keyPair;
}
public static byte[] Encriptar(RsaKeyParameters publicKey, string texto, byte[] encodingParam)
{
// Creating the RSA algorithm object
IAsymmetricBlockCipher cipher = new OaepEncoding(new RsaEngine(), new Sha256Digest(), encodingParam);
var palavrabyte = Encoding.UTF8.GetBytes(texto);
// Initializing the RSA object for Encryption with RSA public key. Remember, for encryption, public key is needed
cipher.Init(true, publicKey);
byte[] ciphered = cipher.ProcessBlock(palavrabyte, 0, palavrabyte.Length);
return ciphered;
}
public static string Decriptar(RsaKeyParameters privateKey, string txtEncript, byte[] encodingParam)
{
// Creating the RSA algorithm object
IAsymmetricBlockCipher cipher = new OaepEncoding(new RsaEngine(), new Sha256Digest(), encodingParam);
// Initializing the RSA object for Encryption with RSA public key. Remember, for encryption, public key is needed
cipher.Init(false, privateKey);
byte[] txtEncriptBytes = Convert.FromBase64String(txtEncript);
byte[] deciphered = cipher.ProcessBlock(txtEncriptBytes, 0, txtEncriptBytes.Length);
string decipheredText = Encoding.UTF8.GetString(deciphered, 0, deciphered.Length);
return decipheredText;
}
}
这是 OAEPE 编码的代码:
SHA256Managed Hash = new SHA256Managed();
byte[] ParamOEAP = Hash.ComputeHash("Example" + anotherdata);
和 class SHA256Managed:
public class SHA256Managed
{
public byte[] ComputeHash(string text)
{
Sha256Digest dig = new Sha256Digest();
byte[] msgBytes = Encoding.UTF8.GetBytes(text);
dig.BlockUpdate(msgBytes, 0, msgBytes.Length);
byte[] result = new byte[dig.GetDigestSize()];
dig.DoFinal(result, 0);
return result;
}
}
当我加密单词时,例如 "Subtracão de Incapazes",解密没问题。
当我加密单词时,例如 "Estelionato por Emissão de Cheque sem Suficiente Provisão de Fundos",Decriptar 代码行中的解密中断:
byte[] deciphered = cipher.ProcessBlock(txtEncriptBytes, 0, txtEncriptBytes.Length);
我做错了什么?
在 CreatePair 上更改行:
rsaKeyPairGnr.Init(新密钥生成参数(新 SecureRandom(), 2048))
从 1024 到 2048 !!现在,大短语被解密。
我正在使用 BouncyCastle 加密和解密一些数据,但是当单词的长度太长时(我不知道确切的值),我得到了这个错误 "InvalidCipherTextException: data start wrong 64"
这是我的 Class 的 Encription:
public static class Crypto
{
public static IAsymmetricBlockCipher CriarCipher(byte[] encodingParam)
{
// Creating the RSA algorithm object
IAsymmetricBlockCipher cipher = new OaepEncoding(new RsaEngine(), new Sha256Digest(), encodingParam);
return cipher;
}
public static AsymmetricCipherKeyPair CreatePair()
{
RsaKeyPairGenerator rsaKeyPairGnr = new RsaKeyPairGenerator();
rsaKeyPairGnr.Init(new KeyGenerationParameters(new SecureRandom(), 1024));
AsymmetricCipherKeyPair keyPair = rsaKeyPairGnr.GenerateKeyPair();
return keyPair;
}
public static byte[] Encriptar(RsaKeyParameters publicKey, string texto, byte[] encodingParam)
{
// Creating the RSA algorithm object
IAsymmetricBlockCipher cipher = new OaepEncoding(new RsaEngine(), new Sha256Digest(), encodingParam);
var palavrabyte = Encoding.UTF8.GetBytes(texto);
// Initializing the RSA object for Encryption with RSA public key. Remember, for encryption, public key is needed
cipher.Init(true, publicKey);
byte[] ciphered = cipher.ProcessBlock(palavrabyte, 0, palavrabyte.Length);
return ciphered;
}
public static string Decriptar(RsaKeyParameters privateKey, string txtEncript, byte[] encodingParam)
{
// Creating the RSA algorithm object
IAsymmetricBlockCipher cipher = new OaepEncoding(new RsaEngine(), new Sha256Digest(), encodingParam);
// Initializing the RSA object for Encryption with RSA public key. Remember, for encryption, public key is needed
cipher.Init(false, privateKey);
byte[] txtEncriptBytes = Convert.FromBase64String(txtEncript);
byte[] deciphered = cipher.ProcessBlock(txtEncriptBytes, 0, txtEncriptBytes.Length);
string decipheredText = Encoding.UTF8.GetString(deciphered, 0, deciphered.Length);
return decipheredText;
}
}
这是 OAEPE 编码的代码:
SHA256Managed Hash = new SHA256Managed();
byte[] ParamOEAP = Hash.ComputeHash("Example" + anotherdata);
和 class SHA256Managed:
public class SHA256Managed
{
public byte[] ComputeHash(string text)
{
Sha256Digest dig = new Sha256Digest();
byte[] msgBytes = Encoding.UTF8.GetBytes(text);
dig.BlockUpdate(msgBytes, 0, msgBytes.Length);
byte[] result = new byte[dig.GetDigestSize()];
dig.DoFinal(result, 0);
return result;
}
}
当我加密单词时,例如 "Subtracão de Incapazes",解密没问题。
当我加密单词时,例如 "Estelionato por Emissão de Cheque sem Suficiente Provisão de Fundos",Decriptar 代码行中的解密中断:
byte[] deciphered = cipher.ProcessBlock(txtEncriptBytes, 0, txtEncriptBytes.Length);
我做错了什么?
在 CreatePair 上更改行: rsaKeyPairGnr.Init(新密钥生成参数(新 SecureRandom(), 2048))
从 1024 到 2048 !!现在,大短语被解密。