2016-10-10 1 views
0

Je crypte et décrypte certaines données en utilisant BouncyCastle, mais quand la longueur du mot est trop longue (je ne sais pas exactement la valeur), j'ai cette erreur "InvalidCipherTextException: les données commencent à mal 64"Erreur "InvalidCipherTextException: les données commencent mal 64" avec Bouncy Castle

Voici ma classe de 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; 
     } 

    } 

Voici le code pour OAEPE encodage:

SHA256Managed Hash = new SHA256Managed(); 
byte[] ParamOEAP = Hash.ComputeHash("Example" + anotherdata); 

Et c lass 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;    
     } 
    } 

Quand je crypter le mot, par exemple, "Subtracão de Incapazes", le décryptage de son ok.

Quand je crypter le mot, par exemple, "Estelionato por Emissão de Check Suficiente Provisão de ETM Fundos", les décryptage dans cassés la codeline Decriptar:

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

Qu'est-ce que je fais mal?

Répondre

0

Changement sur la ligne CreatePair: rsaKeyPairGnr.Init (nouveaux KeyGenerationParameters (nouveau SecureRandom(), 2048))

de 1024 à 2048 !! Maintenant, les grandes phrases sont décryptées.