Jump to content

Recommended Posts

Posted

Boas pessoal, eu tou com um problema a cifrar com o Algoritmo RSA,eu utilizei num programa ao inicio o algoritmo DES/CBC/PKCS5Padding e akilo funcionava na boa agora quero utilizar o RSA nao consegui.Pensei que fosse do meu programa.Fiz entao um pequeno programa e nao funciona, vou por aki esse programa:

	KeyPairGenerator keyPairGenerator =    KeyPairGenerator.getInstance("RSA");
	keyPairGenerator.initialize(1024);
	KeyPair keyPair = keyPairGenerator.generateKeyPair();
	PublicKey publicKey = keyPair.getPublic();
	PrivateKey privateKey = keyPair.getPrivate();
	Cipher c = Cipher.getInstance("RSA");
	Cipher d = Cipher.getInstance("RSA");
	c.init(Cipher.ENCRYPT_MODE,publicKey  );
	Message m = new Message ("buceta",40,23156487);
	byte [] forEncrypted = ObjectToByteConverter.convertToBytes(m);
	byte [] encrypted = c.doFinal(forEncrypted);
	Socket socket = new Socket ("Localhost",12345);
	OutputStream out = new ObjectOutputStream(socket .getOutputStream());
	CipherOutputStream bd = new CipherOutputStream(out,c); 
	bd.write(forEncrypted);
	ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
	Message help =(Message)in.readObject();
	d.init(Cipher.DECRYPT_MODE,privateKey );
	byte [] decrypted = d.doFinal(help.getData());
	Message n =(Message)ObjectToByteConverter.convertToObject(decrypted);
	System.out.println(n.toString());

e o  que me da sempre é :

Exception in thread "main" javax.crypto.IllegalBlockSizeException: Data must not be longer than 117 bytes
at com.sun.crypto.provider.RSACipher.a(DashoA13*..)
at com.sun.crypto.provider.RSACipher.engineDoFinal(DashoA13*..)
at javax.crypto.Cipher.doFinal(DashoA13*..)
at rsapackage.Rsa.main(Rsa.java:34)

alguem sabe como cifrar em RSA com cripto assimetrica?

muito obrigado

  • 5 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.