[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[iaik-jce] a question about encrypt/decrypt with RSA



The next program encrypt a decrypt a byte[] using RSA.  Usually go fine,
but sometimes depending on the pair of keys, the program don't go fine.  I
use the program with a certain pair key  go fine, but i have found other
pairs of keys that decrypt wrong and i obtain this result:

PETICIONACTA$1998/1999$104$1$C$5$42B910A30B133DB007467269FFE412BB0194F016438
B6DA06865284BC41A970D26913C1EF332DD61AFDEA2F6149CBE5/8'3ہ_.?
'͵)!TpfYo$IS[8<'&Ҫ
i>Ƨl^ms#qnO6=V!CD̨E%LwG}<glXF=*A
8,kyg֪H>&ج~kp
T
&/U)?
M2c
C3EUjG[9K
$Yҵ†

with other pairs of keys i obtain the proper result:
PETICIONACTA$1998/1999$104$1$C$5$42B910A30B133DB007467269FFE412BB0194F016438
B6DA06865284BC41A970D26913C1EF332DD61AFDEA2F6149CBE5AE5DBB98EA70A8D42D9FF5FA
B0FEF9E56E66F437DDCCED24BB79FE27B454EF360FF13437CF1CBB73FCA2C852B430F957F7EE
7DCDC477EDD87401685A436D3D200F


How can it be that decrypt sometimes good and sometimes bad?
The same pairs of keys than decrypt wrong the array of bytes of the
example, late decrypt correctly others array of bytes (also with the same
length)
Can anybody help me?

Thanks in advance

the code is here:


import java.io.*;
import java.util.*;
import iaik.security.rsa.*;
import java.security.*;
import iaik.security.provider.*;
import iaik.x509.X509Certificate;
import iaik.asn1.structures.*;
import iaik.pkcs.pkcs8.*;
import javax.crypto.*;
import java.math.*;
import iaik.utils.*;
class encriptacion2{
  static RSAPublicKey clavePublica=null;
  static RSAPrivateKey clavePrivada=null;
  public static void main(String args[]){
    try{
      IAIK proveedor=new IAIK();
	  Security.addProvider(proveedor);
      X509Certificate certServidor=new X509Certificate(new
FileInputStream("c:/servidor/servidor.cert"));
      clavePublica=(RSAPublicKey)certServidor.getPublicKey();
	  clavePrivada=new RSAPrivateKey(new
FileInputStream("c:/servidor/servidor.priv"));
      byte[]
texto=("PETICIONACTA$1998/1999$104$1$C$5$42B910A30B133DB007467269FFE412BB019

4F016438B6DA06865284BC41A970D26913C1EF332DD61AFDEA2F6149CBE5AE5DBB98EA70A8D4

2D9FF5FAB0FEF9E56E66F437DDCCED24BB79FE27B454EF360FF13437CF1CBB73FCA2C852B430

F957F7EE7DCDC477EDD87401685A436D3D200F").getBytes();
    //byte[]
texto=("PETICIONACTA$1998/1999$104$1$C$5$42B910A30B133DB007467269FFE412BB019

4F016438B6DA06865284BC41A970D26913C1EF332DD61AFDEA2F6149CBE5AE5DBB98EA70A8D4

2D9FF5FAB0FEF9E56E66F437DDCCED24BB79FE27B454EF360FF13437CF1CBB73FCA2C852B430

F957F7EE7DCDC477EDD87401685A436D3D200F86F9965D98933FD2546E14F67F34B0A").getB

ytes();
	  String nuevo=new String(texto);

	    byte[] textoEncriptado=encriptar(nuevo.getBytes(),clavePublica);
	    byte[] textoDesencriptado=desencriptar(textoEncriptado,clavePrivada);
        System.out.println("LONGITUD TEXTO:"+texto.length);
		System.out.println("LONGITUD ENCRIPTADO:"+textoEncriptado.length);
		System.out.println("LONGITUD DESENCRIPTADO:"+textoDesencriptado.length);
		System.out.println(new String(textoDesencriptado));
  
	}catch(Exception e){
      System.out.println(e);
    }
  }
    static byte[] copiarArray(byte[] cad){
    byte[] temp=new byte[cad.length];
    for (int i=0;i<cad.length;i++)
      temp[i]=cad[i];
    return temp;
  }
  static byte[] unirArrays(byte[] a1,byte[] a2){
    byte[] temp=new byte[a1.length+a2.length];
    int p=0;
    for (int i=0;i<(a1.length+a2.length);i++){
      if (i<a1.length)
        temp[i]=a1[i];
      else
        temp[i]=a2[p++];
    }
    return temp;
  }
   static byte[] encriptar(byte[] b,RSAPublicKey pub){
    int numblock=(b.length/128);
	if ((b.length%128)>0)
	  numblock++;
	byte[] txtEncriptado=new byte[0];
	for(int i=0;i<numblock;i++){
	  byte[] textoaEncriptar=new byte[0];
	  System.out.println("antes");
	  if (i==numblock-1){
	    textoaEncriptar=new byte[b.length-i*128];
		System.arraycopy(b,i*128,textoaEncriptar,0,b.length-i*128);
	  }else{
	    textoaEncriptar=new byte[128];
	    System.arraycopy(b,i*128,textoaEncriptar,0,128);
	  }
	  System.out.println("despues");
   	  byte[] blockEncriptado=encriptarblock(textoaEncriptar,pub);
	  txtEncriptado=unirArrays(txtEncriptado,blockEncriptado);
    }
	return txtEncriptado;
  }
  static byte[] encriptarblock(byte[] b,RSAPublicKey publ){
    byte[] blockencriptado=null;
    try{
      IAIK proveedor=new IAIK();
      Security.addProvider(proveedor);
      Cipher rsa=Cipher.getInstance("RSA");
      rsa.init(Cipher.ENCRYPT_MODE,publ);
      blockencriptado=rsa.doFinal(b);
    }catch(Exception e){
      System.out.println(e);
      return "ERROR".getBytes();
    }
    return blockencriptado;
  }
 static byte[] desencriptar(byte[] b,RSAPrivateKey clavePrivada){
    byte[] cDesenc=null;
    try{
      IAIK proveedor=new IAIK();
      Security.addProvider(proveedor);

      cDesenc=desenc(b,clavePrivada);
    }catch(Exception e){
      System.out.println(e);
      return ("ERROR".getBytes());
    }
    return cDesenc;
  }
  static byte[] desenc(byte[] b,RSAPrivateKey pri){
    int numblock=(b.length/128);
    byte[][] mat=new byte[numblock][128];
    int p=0;
	System.out.println("array:"+numblock*128);
    boolean res=false;
    byte[] residuo=null;
    if ((b.length%128)>0){
      int elementosRestantes=b.length-numblock*128;
      System.out.println("hay residuo de "+elementosRestantes);
      residuo=new byte[elementosRestantes];
      int finalb=b.length;
      int finalresiduo=residuo.length;
      for (int i=0;i<elementosRestantes;i++)
        residuo[--finalresiduo]=b[--finalb];
      res=true;
    }




    for (int i=0;i<numblock;i++)
      for (int j=0;j<128;j++)
        mat[i][j]=b[p++];
    byte[] definitivo=new byte[0];
    for (int i=0;i<numblock;i++){
      byte[] des=desencriptarblock(mat[i],pri);
      byte[] temporal=copiarArray(definitivo);
	  System.out.println("block "+(i+1)+"  "+new
String(temporal,0,temporal.length));
      definitivo=unirArrays(temporal,des);
    }
    if (res){
      byte[] desres=desencriptarblock(residuo,pri);
      System.out.println(new String(desres)+"REDIDUDDUUD");
      byte[] temporal=copiarArray(definitivo);
      definitivo=unirArrays(temporal,desres);
    }
    return definitivo;
  }
  static byte[] desencriptarblock(byte[] b,RSAPrivateKey pri){
    byte[] blockDesencriptado=null;
    try{
      IAIK proveedor=new IAIK();
      Security.addProvider(proveedor);
      Cipher rsa=Cipher.getInstance("RSA");
      rsa.init(Cipher.DECRYPT_MODE,pri);
      blockDesencriptado=rsa.doFinal(b);
    }catch(Exception e){
      System.out.println(e);
    }
    return blockDesencriptado;
  }
}



--
Mailinglist-archive at http://jcewww.iaik.tu-graz.ac.at/mailarchive/iaik-jce/maillist.html

To unsubscribe send an email to listserv@iaik.tu-graz.ac.at with the folowing content: UNSUBSCRIBE iaik-jce