convert RSA keys

C

CP

Hi all,

I'm writing a client and server application. For the security purpose, i'm
using RSA encryption and decryption in C at the client side, whereas the
encrytion was wrinting in Java at server side.

My problem here is, at client side i've created a pair of RSA public and
private key in unsigned char[] format. But in java for cipher RSA, all the
public and private key are in object format. How can i convert the existing
keys in unsigned char[] to java object.

Hope anyone can help. Thanks in advance!

Chiew peng
 
C

Chris Smith

CP said:
I'm writing a client and server application. For the security purpose, i'm
using RSA encryption and decryption in C at the client side, whereas the
encrytion was wrinting in Java at server side.

My problem here is, at client side i've created a pair of RSA public and
private key in unsigned char[] format. But in java for cipher RSA, all the
public and private key are in object format. How can i convert the existing
keys in unsigned char[] to java object.

Assuming you've got instances of RSAPublicKey or RSAPrivateKey and are
using the java.security package, try calling getEncoded(). If you're
doing something else, please let us know what the Java code looks like,
or at least what API you're using.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
C

CP

I've created a pair of keys in c and use in java RSA encryption.

//public and private key which created in C
byte[] pubKey = {
7,116,78,18, (byte)252, 61, (byte)157, 16,
(byte)232, (byte)133, 103, 106, (byte)130,
(byte)129, 77, (byte)154, 41, (byte)144,
(byte)243,
105, 5, 101, 89, 89, 90,40,64,(byte)178,114,
64, (byte)200
};
byte[] priKey = {
(byte)241, (byte)227,102, (byte)189,
(byte)202,25,
30, 84, (byte)153, 97, (byte)138, 122, 27,
(byte)227,
6, 106, 112, 35, 31, (byte)191, 93, 9, (byte)254,
61,
93, (byte)140, (byte)165, 35, 61, (byte)221,
(byte)228, (byte)170
};

//Add provider
try {
//Security.addProvider(new BouncyCastleProvider());
Security.addProvider (new
org.bouncycastle.jce.provider.BouncyCastleProvider());
} catch (Exception e)
{
e.printStackTrace(System.out);
}

// initiate a cipher
Cipher rsaCipher = Cipher.getInstance("RSA", "BC");// Initialize the cipher
for encryption

//PROBLEM! how to convert the above key into publicKey object?
rsaCipher.init(Cipher.ENCRYPT_MODE, pub);

// Cleartext
byte[] cleartext = "test".getBytes();

// Encrypt the cleartext
byte[] ciphertext = rsaCipher.doFinal(cleartext);


// Initialize the same cipher for decryption
//PROBLEM! how to convert the above key into privateKey object?
rsaCipher.init(Cipher.DECRYPT_MODE, priv);

// Decrypt the ciphertext
byte[] cleartext1 = rsaCipher.doFinal(ciphertext);

if ( true == Arrays.equals(cleartext, cleartext1 ))
System.out.println("equal");
else
System.out.println("failed");


Thanks
Chiew Peng
Chris Smith said:
CP said:
I'm writing a client and server application. For the security purpose,
i'm
using RSA encryption and decryption in C at the client side, whereas the
encrytion was wrinting in Java at server side.

My problem here is, at client side i've created a pair of RSA public and
private key in unsigned char[] format. But in java for cipher RSA, all
the
public and private key are in object format. How can i convert the
existing
keys in unsigned char[] to java object.

Assuming you've got instances of RSAPublicKey or RSAPrivateKey and are
using the java.security package, try calling getEncoded(). If you're
doing something else, please let us know what the Java code looks like,
or at least what API you're using.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
S

Sudsy

CP wrote:
//PROBLEM! how to convert the above key into publicKey object?
rsaCipher.init(Cipher.ENCRYPT_MODE, pub);

Public key and modulus in BigInteger form -> RSAPublicKeySpec ->
KeyFactory -> PublicKey.
Anything more and I'll have to charge you. I won't apologize for that
either. Crypto is one of the most difficult and time-consuming areas of
Java I've yet encountered. There's a huge amount of background knowledge
you have to acquire before the myriad of classes begin to make sense.
 
C

Chris Smith

CP said:
I've created a pair of keys in c and use in java RSA encryption.

//public and private key which created in C
byte[] pubKey = { [...] };
// initiate a cipher
Cipher rsaCipher = Cipher.getInstance("RSA", "BC");// Initialize the cipher
for encryption

//PROBLEM! how to convert the above key into publicKey object?
rsaCipher.init(Cipher.ENCRYPT_MODE, pub);

Okay, gotcha. You will need a KeySpec for your key. You can create a
RSAPublicKeySpec with the public constructor, which takes a modulus and
exponent. Alternatively, if you have your key in an X.509 encoded form,
then use X509EncodedKeySpec instead. The latter is probably the easiest
code by far, since you don't need to convert anything; so if you can get
whatever key representation you need, then I'd shoot for X.509.

Once you have the KeySpec, just do:

pub = KeyFactory.getInstance("RSA").generatePublic(keySpec);
// Initialize the same cipher for decryption
//PROBLEM! how to convert the above key into privateKey object?
rsaCipher.init(Cipher.DECRYPT_MODE, priv);

Same answer as above, but just use RSAPrivateKeySpec instead of
RSAPublicKeySpec if you go with modulus and exponent, use
PKCS8EncodedKeySpec instead of X509EncodedKeySpec if you encode your
keys, and use generatePrivate instead of generatePublic.

I've only done this with DSA, but I see no reason it won't work with
RSA. Here is some code for both sides and using encoded keys and DSA:

public static PublicKey getDSAPublicKey(String resource)
throws IOException, InvalidKeySpecException,
NoSuchAlgorithmException
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = LicenseUtil.class.getClassLoader()
.getResourceAsStream(resource);

if (in == null) throw new IllegalArgumentException(
"Key resource not found");

try
{
byte[] buffer = new byte[32768];
int len;
while ((len = in.read(buffer)) != -1)
{
out.write(buffer, 0, len);
}
}
finally
{
in.close();
}

X509EncodedKeySpec keySpec = new X509EncodedKeySpec(
out.toByteArray());
return KeyFactory.getInstance("DSA").generatePublic(keySpec);
}

public static PrivateKey getDSAPrivateKey(String resource)
throws IOException, InvalidKeySpecException,
NoSuchAlgorithmException
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = LicenseManager.class.getClassLoader()
.getResourceAsStream(resource);

try
{
byte[] buffer = new byte[32768];
int len;
while ((len = in.read(buffer)) != -1)
{
out.write(buffer, 0, len);
}
}
finally
{
in.close();
}

PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(
out.toByteArray());
PrivateKey key = KeyFactory.getInstance("DSA")
.generatePrivate(keySpec);
return key;
}

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top