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"));
}
}
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"));
}
}