Crypting and decrypting with key public and private

S

strafacile

I want crypting a string (or file) with a generated keys public and
private, but this code return me the error "Invalid key length: 162
bytes".

Can you help me?

Thank you
Jack
*******************************************************************
package criptografia;
import javax.crypto.Cipher;
import java.security.KeyPairGenerator;
import java.security.KeyPair;

public class crypt
{
public static void main(String[] args) throws Exception
{
byte[] string = "HelloWorld".getBytes("UTF8");

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(1024);
KeyPair key = keyGen.generateKeyPair();

Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key.getPublic());

byte[] cipherText = cipher.doFinal(string);
cipher.init(Cipher.DECRYPT_MODE, key.getPrivate());
byte[] returnString = cipher.doFinal(cipherText);
System.out.println(new String(returnString, "UTF8"));
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top