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