2016-10-10 2 views
0

Я шифрую и дешифруя некоторые данные с помощью BouncyCastle, но когда длина слова слишком длинная (я точно не знаю значение), я получил эту ошибку "InvalidCipherTextException: данные начинают неправильно 64"Ошибка «InvalidCipherTextException: data start wrong 64» с Bouncy Castle

Это мой класс Шифрование:

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 Encoding:

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ão де Incapazes", дешифровка его ок.

Когда я зашифровать слово, на пример, "Estelionato Por Emissão де Проверить Suficiente Provisão сем де Fundos", дешифровка Brokes в codeline Decriptar:

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

Что я делаю неправильно?

ответ

0

Изменение на CreatePair линии: rsaKeyPairGnr.Init (новые KeyGenerationParameters (новый SecureRandom(), 2048))

От 1024 до 2048 !! Теперь большие фразы дешифруются.

Смежные вопросы