Ho sviluppato l'encrypt e tutto funziona alla perfezione. adesso ho problemi nel decrypt.
in sostanza mi da l'errore:
Exception in thread "main" javax.crypto.IllegalBlockSizeException: Data must not be longer than 128 bytes
at com.sun.crypto.provider.RSACipher.doFinal(RSACiphe r.java:338)
at com.sun.crypto.provider.RSACipher.engineDoFinal(RS ACipher.java:383)
at javax.crypto.Cipher.doFinal(Cipher.java:1813)
at Rsa_file.decrypt_file(Rsa_file.java:146)
at Rsa_file.main(Rsa_file.java:192)
ecco il codice:
Dal punto di vista teorico nell'encrypt la funz dofinal() accetta un array di keysize/8-11 byte (per via del tipo di padding) e ritorna un array di keysize/8 byte.codice:import java.security.*; import java.util.Scanner; import javax.crypto.*; import java.io.*; import java.lang.Object; public class Rsa_file{ public static KeyPairGenerator kpg=null; public static byte[] file_tobyte(File f)throws IOException { int size = ((int) f.length()); byte[] data = new byte[size]; FileInputStream freader = new FileInputStream(f); System.out.println("\nDimensione in byte : " + freader.read(data, 0, size)); freader.close(); return data; } public static String getText (byte[] arr)throws UnsupportedEncodingException { String s = new String( arr, "ISO-8859-1" ); return s; } public static void encrypt(String path_file,PublicKey pubblica) throws Exception { File ff2=new File("/home/dario/Scrivania/utenti_critt.txt"); FileWriter fw=new FileWriter(ff2,true);// append sul file File ff=new File(path_file); byte[] plain=file_tobyte(ff); Cipher c=null; c = Cipher.getInstance("RSA/ECB/PKCS1Padding"); c.init(Cipher.ENCRYPT_MODE, pubblica); byte[] encodeFile=new byte[128]; int iteraz=plain.length/117; int resto=plain.length%117; int i=0; int dim=117; long a=System.currentTimeMillis(); while(i<=iteraz){ // entro nell' IF quando rimangono solo *** plain.lenght%117 *** if(i==iteraz){ int offset=dim*i+1; byte[] temp=new byte[dim]; System.out.println("if"+offset+" "+(plain.length-offset)); temp=c.update(plain, offset, plain.length-offset); encodeFile=c.doFinal(temp); fw.write(getText(encodeFile)); i++; } else{ byte[] temp=new byte[dim]; System.out.println("else"+i*(dim+1)); temp=c.update(plain, (i*(dim+1)), dim); encodeFile=c.doFinal(temp); fw.write(getText(encodeFile)); i++; //System.out.println("else i < iteraz"+i); } } fw.close(); long b=System.currentTimeMillis()-a; System.out.println("Tempo per criptare XXX utenti "+b+"ms"); } public static void decrypt_file(String path,PrivateKey privata) throws Exception { File ff2=new File("/home/dario/Scrivania/utenti_critt.txt"); FileInputStream fr=new FileInputStream(ff2); File ff3=new File("/home/dario/Scrivania/utenti_decritt.txt"); FileWriter fw=new FileWriter(ff3,true); byte[] ciphered=file_tobyte(ff2); int iteraz=ciphered.length/117; int resto=ciphered.length%117; Cipher c=null; c = Cipher.getInstance("RSA/ECB/PKCS1Padding"); c.init(Cipher.DECRYPT_MODE, privata); int i=0; int dim=117; byte[] decodeFile=new byte[128]; long a=System.currentTimeMillis(); while(i<=iteraz){ // entro nell' IF quando rimangono solo *** plain.lenght%117 *** if(i==iteraz){ int offset=dim*i+1; byte[] temp=new byte[dim]; System.out.println("if"+offset+" "+(ciphered.length-offset)); System.out.println("else"+i*(dim+1)); temp=c.update(ciphered, (i*(dim+1)), dim); decodeFile=c.doFinal(temp); fw.write(getText(decodeFile)); i++; } else{ int offset=dim*i+1; byte[] temp=new byte[dim]; temp=c.update(ciphered, offset, ciphered.length-offset); decodeFile=c.doFinal(temp); fw.write(getText(decodeFile)); i++; } } long b=System.currentTimeMillis()-a; System.out.println("Tempo per criptare 100 utenti "+b+"ms"); fw.close(); fr.close(); } public static void main(String[] args) throws Exception { /*KeyPair kp=gen_key(kpg); PublicKey pubblica=kp.getPublic(); PrivateKey privata=kp.getPrivate();*/ //ISTANZIO UN OGGETTO LOADKEYS CHE MI PERMETTE DI CARICARE LE CHIAVI MEMORIZZATE NEL KEYSTORE. LoadKeys aa=new LoadKeys(); KeyPair kp=aa.getkey_fromkeystore("/home/dario/prova.jks", "storepass", "keypass", "alias", "JKS"); PublicKey pubblica=kp.getPublic(); PrivateKey privata=kp.getPrivate(); File a=new File("/home/dario/Scrivania/utenti"); long len=a.length(); //CODIFICA //Encrypt accetta un pathname e una PublicKey. Cripta il file e lo scrive. encrypt("/home/dario/Scrivania/utenti",pubblica); //DECODIFICA //Decrypt accetta un pathname e una PrivateKey. Decripta il file e lo scrive. decrypt_file("/home/dario/Scrivania/utenti_critt.txt",privata); } }
Nel decrypt dovrebbe essere invertito il tutto, quindi dovrebbe accettare in ingresso un array da keysize/8 byte e tornare un array di keysize/8 -11.
Nel mio caso ho una key da 1024bit.
La riga incriminata è quella in grassetto.

Rispondi quotando