与Bouncy Castle出现错误“InvalidCipherTextException:data start wrong 64”

时间:2016-10-10 16:22:44

标签: c# encryption bouncycastle encryption-asymmetric

我正在使用BouncyCastle加密和解密一些数据,但是当这个词的长度太长(我不确切知道该值)时,我收到此错误“InvalidCipherTextException:data start wrong 64”

这是我的铭文类:

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);

类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ãodeIncapazes”,解密就可以了。

当我加密这个单词时,例如“EstelionatoporEmissãodeCheck semSuficienteProvisãodeFundos”,解密在Decriptar代码行中显示:

byte[] deciphered = cipher.ProcessBlock(txtEncriptBytes, 0, txtEncriptBytes.Length);

我做错了什么?

1 个答案:

答案 0 :(得分:0)

在CreatePair上更改该行: rsaKeyPairGnr.Init(new KeyGenerationParameters(new SecureRandom(),2048))

从1024到2048 !!现在,重要的短语被解密了。