2017-03-04 3511 views
0

我必须用bouncycastle api编码文件,加密效果很好,但是在第一次迭代中解密时出现错误 org.bouncycastle.crypto.InvalidCipherTextException:块不正确出现org.bouncycastle.crypto.InvalidCipherTextException:块不正确

这是对加密及其工作

public void cifrado() throws IOException, InvalidCipherTextException{ 
    RSAEngine motor = new RSAEngine(); 
    PKCS1Encoding padding = new PKCS1Encoding(motor); 

    BufferedReader fClave = new BufferedReader(new FileReader(clave)); 

    File Entrada = new File(ficheroEntrada); 
    BufferedInputStream bInEntrada = new BufferedInputStream(new FileInputStream(Entrada)); 

    File Destino = new File(ficheroSalida); 
    BufferedOutputStream bOutDestino = new BufferedOutputStream(new FileOutputStream(Destino)); 

    //Metemos en un String la clave leida del fichero con readline en la que tendremos modulo, caracter CRLF y exponente 
    String modulo = fClave.readLine(); 
    String exponente = fClave.readLine(); 
    //Decodificamos el Hexadecimal leido 
    byte[] bModulo = Hex.decode(modulo); 
    byte[] bExponente = Hex.decode(exponente); 

    //Ahora con el modulo y exponente ya podemos utilizar el RSAKeyParameter e inicializar el motor de padding en modo cifrado 
    padding.init(true, new RSAKeyParameters(false,new BigInteger(bModulo),new BigInteger(bExponente))); 
    System.out.println(padding.getInputBlockSize() + " " + padding.getOutputBlockSize()); 

    //Leemos de 117 bytes en 117 bytes que es lo maximo que admite los bloques de entrada 
    byte[] array117 = new byte[117]; 
    byte[] salida; 
    long longitudFichero = Entrada.length(); 
    System.out.println(longitudFichero); 
    long contador = longitudFichero; 
    while(contador>0){ 
     bInEntrada.read(array117, 0, array117.length); 
     salida= padding.processBlock(array117, 0, array117.length); 
     System.out.println(salida.length); 
     bOutDestino.write(salida, 0, salida.length); 
     contador = ((contador>0)?contador-=117:contador); 
    } 
     bInEntrada.close(); 
     bOutDestino.close(); 
     fClave.close(); 
} 

这是解密

public void descifrado() throws IOException, InvalidCipherTextException{ 
    RSAEngine motor = new RSAEngine(); 
    PKCS1Encoding padding = new PKCS1Encoding(motor); 

    BufferedReader fClave = new BufferedReader(new FileReader(clave)); 

    File Entrada = new File(ficheroEntrada); 
    BufferedInputStream bInCifrado = new BufferedInputStream(new FileInputStream(Entrada)); 

    File Destino = new File(ficheroSalida); 
    BufferedOutputStream bOutDescifrado = new BufferedOutputStream(new FileOutputStream(Destino)); 

    //Metemos en un String la clave leida del fichero con readline en la que tendremos modulo, caracter CRLF y exponente 
    String modulo = fClave.readLine(); 
    String exponente = fClave.readLine(); 
    //Decodificamos el Hexadecimal leido 
    byte[] bModulo = Hex.decode(modulo); 
    byte[] bExponente = Hex.decode(exponente); 
    System.out.println(bModulo.length + " " + bExponente.length); 
    //Ahora con el modulo y exponente ya podemos utilizar el RSAKeyParameter e inicializar el motor de padding en modo descifrado 
    padding.init(false, new RSAKeyParameters(false,new BigInteger(bModulo),new BigInteger(bExponente)));  
    System.out.println(padding.getInputBlockSize() + " " + padding.getOutputBlockSize()); 

    //Leemos de 128 bytes en 128 bytes que es lo maximo que admite los bloques de entrada cuando decodificamos 
    byte[] array128 = new byte[128]; 

    long longitudFichero = Entrada.length(); 
    long contador = longitudFichero; 
    System.out.println(longitudFichero); 
    while(contador!=0){ 
     bInCifrado.read(array128, 0, array128.length); 
     byte[] salida= padding.processBlock(array128, 0, array128.length); 
     System.out.println(salida.length); 
     /*for(byte i: salida){ 
      System.out.println(i); 
     }*/ 
     bOutDescifrado.write(salida, 0, salida.length); 
     contador -=128; 
    } 
     bInCifrado.close(); 
     bOutDescifrado.close(); 
     fClave.close(); 
} 
+0

加密和解密的正确的大小总是一起去。请编辑您的代码以包含加密代码以及示例输入和输出。另外,哪一行是例外来自? –

+0

嗨,我只是上传加密,谢谢 – Christian

+0

它看起来像你使用相同的输入和输出文件的加密和解密。但是,对于解密,输入文件应该是加密的输出文件。事实上,它看起来像解密函数几乎是加密函数的一个副本。但解密应该使用rsa解密指数。 –

回答

0

我刚修好复制的最后一块,该解决方案是在cifrado方法,我不得不检查最后一块是在输入

while(contador>0){ 
     if(contador>0 && contador<117){ 
      byte[] ultimo = new byte[(int) contador]; 
      bInEntrada.read(ultimo, 0, ultimo.length); 
      salida= padding.processBlock(ultimo, 0, ultimo.length); 
      System.out.println(salida.length); 
      bOutDestino.write(salida, 0, salida.length); 
      contador=0; 

     }else{ 
      bInEntrada.read(array117, 0, array117.length); 
      salida= padding.processBlock(array117, 0, array117.length); 
      System.out.println(salida.length); 
      bOutDestino.write(salida, 0, salida.length); 
      contador = ((contador>0)?contador-=117:contador); 
     } 
    }