2016-10-10 5 views
0

BouncyCastle을 사용하여 일부 데이터를 암호화하고 해독하지만 단어의 길이가 너무 길면 (정확하게 값을 알지 못함)이 오류가 발생합니다. "InvalidCipherTextException : 데이터가 잘못 시작 64"오류 Bouncy Castle의 "InvalidCipherTextException : data start wrong 64"

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

그리고 C :이 OAEPE 인코딩에 대한 코드가

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

    } 

입니다

이것은 Encription 내 클래스입니다 아가씨 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 포 Emissão 드 SEM Suficiente Provisão 드 Fundos 확인"을 Decriptar의 코드 라인에서 해독 brokes : 내가 뭘 잘못

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

를?

답변

0

CreatePair에 변경 라인 : rsaKeyPairGnr.Init (새 KeyGenerationParameters (새 SecureRandom의(), 2048)) 2,048에서 1,024 사이에서

! 이제 큰 문구가 해독됩니다.