in my program i follow this process
Server side
generate keypair
get publickey
send to the client
Client side
get data by getRawContent()
PublicKey pk= data; //ERROR- type mismatch.
//datatype of data is String and can't convert to PublicKey.
_
and i want to convert data to PublicKey datatype.
Thank for reply :-)
i merge client and server code into one program for debugging
and code is as follow
//
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class AsymmetricCipherTest {
private static byte[] encrypt(byte[] inpBytes, PublicKey key,
String xform) throws Exception {
Cipher cipher = Cipher.getInstance(xform);
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(inpBytes);
}
private static byte[] decrypt(byte[] inpBytes, PrivateKey key,
String xform) throws Exception{
Cipher cipher = Cipher.getInstance(xform);
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(inpBytes);
}
public static void main(String[] unused) throws Exception {
String xform = "RSA/ECB/PKCS1PADDING";
// Generate a key-pair
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(1024); // 1024 is the keysize.
KeyPair kp = kpg.generateKeyPair();
PublicKey pubk = kp.getPublic();
PrivateKey prvk = kp.getPrivate();
////////////////////////////////////////////////////////
String strPubk=null;
strPubk=pubk.toString();
System.out.println("public key in String format := "+strPubk);
//this is send to the client
/////////////////////////////////////////////////////////
//now at the client side
//because client only get the response as string so
// i assume client get the strPubk
//and again retrive public key form strPubk
PublicKey retrivePubk=null;
retrivePubk=strPubk;
//****ERROR**** :-) how to remove?
//***************** or you give any other posible solution !/////////////////////////////////////////////////////////
String input ="12345678901201234567012345616022007091123NeerajNandwana123456789012123456781234567812345";
System.out.println("input data= \n"+input);
System.out.println("\n\n");
byte[] dataBytes = input.getBytes();
byte[] encBytes = encrypt(dataBytes, pubk, xform);
System.out.println("Encrypted data= \n"+new String(encBytes));
System.out.println("\n\n");
byte[] decBytes = decrypt(encBytes, prvk, xform);
System.out.println("Decrypted data = \n"+new String(decBytes));
System.out.println("\n\n");
boolean expected = java.util.Arrays.equals(dataBytes, decBytes);
System.err.println("Test " + (expected ? "SUCCEEDED!" : "FAILED!"));
}
}
thank
import java.util.*;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import javax.crypto.Cipher;
import java.security.spec.*;
public class AsymmetricCipherTest
{
private static byte[] encrypt(byte[] inpBytes, PublicKey key,
String xform) throws Exception
{
Cipher cipher = Cipher.getInstance(xform);
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(inpBytes);
}
private static byte[] decrypt(byte[] inpBytes, PrivateKey key,
String xform) throws Exception
{
Cipher cipher = Cipher.getInstance(xform);
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(inpBytes);
}
public static void main(String[] unused) throws Exception
{
String xform = "RSA/ECB/PKCS1PADDING";
// Generate a key-pair
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(1024); // 1024 is the keysize.
KeyPair kp = kpg.generateKeyPair();
PublicKey pubk = kp.getPublic();
PrivateKey prvk = kp.getPrivate();
////////////////////////////////////////////////////////
byte[] publicKeyAsBytes =pubk.getEncoded();
System.out.println("public key in String format := "+ Arrays.toString(publicKeyAsBytes));
//this is send to the client
/////////////////////////////////////////////////////////
//now at the client side
//because client only get the response as string so
// i assume client get the strPubk
//and again retrive public key form strPubk
final X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyAsBytes);
final KeyFactory keyFactory = KeyFactory.getInstance("RSA");
final PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
//****ERROR**** :-) how to remove?
//***************** or you give any other posible solution !
/////////////////////////////////////////////////////////
String input ="12345678901201234567012345616022007091123NeerajNandwana123456789012123456781234567812345";
System.out.println("input data= \n"+input);
System.out.println("\n\n");
byte[] dataBytes = input.getBytes();
byte[] encBytes = encrypt(dataBytes, pubk, xform);
System.out.println("Encrypted data= \n"+ Arrays.toString(encBytes));
System.out.println("\n\n");
byte[] decBytes = decrypt(encBytes, prvk, xform);
System.out.println("Decrypted data = \n"+new String(decBytes));
System.out.println("\n\n");
boolean expected = java.util.Arrays.equals(dataBytes, decBytes);
System.err.println("Test " + (expected ? "SUCCEEDED!" : "FAILED!"));
}
}