how retrive publicKey (RSA) at client side

i am sending publicKey (RSA publikey) to the client.but it found this publicKey as string So how can i retrive publicKey through this String?or i need to apply some encoding process before send the public key to the client .
[252 byte] By [MahendraNandwanaa] at [2007-11-26 18:46:13]
# 1
> i am sending publicKey (RSA publikey) to the> client.> but it found this publicKey as string I don't understand what you men by "but it found this publicKey as string" . Can you provide a sample String?
sabre150a at 2007-7-9 6:20:07 > top of Java-index,Security,Cryptography...
# 2

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 :-)

MahendraNandwanaa at 2007-7-9 6:20:07 > top of Java-index,Security,Cryptography...
# 3
> in my program i follow this process> > Server side> > generate keypair> get publickey> send to the client> The detail matters. Please post your code.
sabre150a at 2007-7-9 6:20:07 > top of Java-index,Security,Cryptography...
# 4
and since when is raw content a String?
ejpa at 2007-7-9 6:20:07 > top of Java-index,Security,Cryptography...
# 5

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

MahendraNandwanaa at 2007-7-9 6:20:07 > top of Java-index,Security,Cryptography...
# 6

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

}

}

sabre150a at 2007-7-9 6:20:07 > top of Java-index,Security,Cryptography...