Salve a tutti io ho realizzato la seguente classe per poter criptare e decriptare stringhe secondo l'algortimo RSA:
Il mio problema è che quando mando in esecuzione il main mi genera questo errore:codice:import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.security.InvalidKeyException; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import sun.misc.BASE64Decoder; public class RSA { protected static Cipher cipher; protected static KeyPairGenerator keyGen; protected static KeyPair key; public static void generaChiave() throws NoSuchAlgorithmException, NoSuchPaddingException{ System.out.println("Genero chiavi.."); keyGen=KeyPairGenerator.getInstance("RSA"); keyGen.initialize(1024); key=keyGen.generateKeyPair(); cipher=Cipher.getInstance("RSA/ECB/PKCS1Padding"); System.out.println("Chiavi generate.."); } // effettua la cifratura utilizzando la chiave PUBBLICA public static String cripta(String s) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException{ System.out.print("Inizio cifratura.."); cipher.init(Cipher.ENCRYPT_MODE, key.getPublic()); byte[] str=s.getBytes("UTF8"); byte[] cipherText = cipher.doFinal(str); System.out.println("\nCifratura finita... "); return new String(cipherText,"UTF8"); } // effettua la decifratura utilizzando la chiave PRIVATA public static String decripta(String s) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, IOException{ System.out.println("\nInizio decifratura.."); cipher.init(Cipher.DECRYPT_MODE, key.getPrivate()); byte[] str=s.getBytes("UTF8"); byte[] cipherText = cipher.doFinal(str); System.out.println("Decifratura finita.. "); return new String(cipherText, "UTF8"); } public static void main (String[]args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, IOException{ String s="ciao"; RSA.generaChiave(); String sa=RSA.cripta(s); System.out.println(sa); s=RSA.decripta(sa); System.out.println(s); } }
Io credo che il problema si che quando il metodo decripta va a creare l'array di byte dalla stringa passata come argomento ,questa non sia uguale all'array di byte crearo dal metodo cipher.dofinal(str) creato nel metodo cripta e per questo da errore.C è un modo per ottenere l'array di byte della stringa criptata senza dover per forza passare al metodo l'array di byte non convertito in stringa?codice:Genero chiavi..Chiavi generate.. Inizio cifratura.. Cifratura finita... ???R?P???5?H?<{?/?'????I?A"C?8??\ ???:D=????Jh?iekC??t????O??6}?6]??i ?jS!I??`??\?H????qPl??Ou???,????] Inizio decifratura.. Exception in thread "main" javax.crypto.IllegalBlockSizeException: Data must not be longer than 128 bytes at com.sun.crypto.provider.RSACipher.a(DashoA13*.. at com.sun.crypto.provider.RSACipher.engineDoFinal(DashoA13*..) at javax.crypto.Cipher.doFinal(DashoA13*..) at dieciLuglio.RSA.decripta(RSA.java:49) at dieciLuglio.RSA.main(RSA.java:58)

Rispondi quotando
