HuangJo
asked on
Generating RSA keys and doing RSA encryption (use Java Cryptix provider)
Hi friends,
I would like to generate pair of keys using the RSA algorithm in cryptix toolkit. However got a prob, Could you pls give some advices? Thank you very much.
Below is the code I used to generate the key pair:
import java.security.*;
import cryptix.provider.*;
import cryptix.provider.rsa.*;
import java.io.*;
class GenKey {
public static void main(String[] args)
{
KeyPairGenerator keys = null;
try{
keys = KeyPairGenerator.getInstan ce("RSA");
}
catch(NoSuchAlgorithmExcep tion e){
System.err.println(e);
}
((BaseRSAKeyPairGenerator) keys).init ialize(102 4,new SecureRandom());
KeyPair pair = keys.generateKeyPair();
System.out.println(pair.ge tPublic(). toString() );
System.out.println(pair.ge tPrivate() .toString( ));
}
}
Output:
<--------RSAPublicKey:
n:Multi-Precision Integer 1024 bits long...
sign:Positive
magnitude:Hexadecimal dump of 128
bytes....
0 : 9d.....
32: 0D.....
64: AD....
96: 94...
e:Multi-Precision
sign:Positive
Magnitude:010001
--->
<BaseRSAPrivateKey>
From the output, it seem that NO private key is generated, only the public key is generated. Does I done anything wrong? Or the cryptix library cannot generate the private key?
As I need both the keys to do encryption and decryption, could you pls give some advice on using RSA encryption using Cryptix, since Java JCE is not exportable and the exportable JCE do not have the RSA algorithm?
Thank you very much...
regards HuangJo
I would like to generate pair of keys using the RSA algorithm in cryptix toolkit. However got a prob, Could you pls give some advices? Thank you very much.
Below is the code I used to generate the key pair:
import java.security.*;
import cryptix.provider.*;
import cryptix.provider.rsa.*;
import java.io.*;
class GenKey {
public static void main(String[] args)
{
KeyPairGenerator keys = null;
try{
keys = KeyPairGenerator.getInstan
}
catch(NoSuchAlgorithmExcep
System.err.println(e);
}
((BaseRSAKeyPairGenerator)
KeyPair pair = keys.generateKeyPair();
System.out.println(pair.ge
System.out.println(pair.ge
}
}
Output:
<--------RSAPublicKey:
n:Multi-Precision Integer 1024 bits long...
sign:Positive
magnitude:Hexadecimal dump of 128
bytes....
0 : 9d.....
32: 0D.....
64: AD....
96: 94...
e:Multi-Precision
sign:Positive
Magnitude:010001
--->
<BaseRSAPrivateKey>
From the output, it seem that NO private key is generated, only the public key is generated. Does I done anything wrong? Or the cryptix library cannot generate the private key?
As I need both the keys to do encryption and decryption, could you pls give some advice on using RSA encryption using Cryptix, since Java JCE is not exportable and the exportable JCE do not have the RSA algorithm?
Thank you very much...
regards HuangJo
Dear HuangJo,
I can email JCE to you if you need.
I can email JCE to you if you need.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi kollegov, thank you very much...However if that is the case, how do I check the format of the private key? From what I know, that although the different provider use the same algorithm, they might not use the same key format, am I right? Do you know ways to check the format of the keys?
Hi hpchong7, thanks alot for offering to email the JCE to me. But does the JCE you mentioned contain the RSA implementation? The new international JCE from Sun, do not contain the RSA algorithm. If yes, pls EMail them to me. Thanks a million. My Email address is:
huangJo@allomail.com
regards HuangJo
Hi hpchong7, thanks alot for offering to email the JCE to me. But does the JCE you mentioned contain the RSA implementation? The new international JCE from Sun, do not contain the RSA algorithm. If yes, pls EMail them to me. Thanks a million. My Email address is:
huangJo@allomail.com
regards HuangJo
PrivateKey interface extends Key Interface, you can use some methods from it interface...
public String getAlgorithm()
public String getFormat()
public byte[] getEncoded()
what do you mean under 'check format'???
If you want to look into raw key data , than
you probably would like to use
byte[] bytes=pair.getPrivate().ge tEncoded() ;
and than output byte array
You also can try to cast it into
RSAPrivateKey and
RSAPrivateCrtKey iterfaces
(not sure does cryptix use this interface too) and try methods of this interfaces to get some more data on key parameters..
public String getAlgorithm()
public String getFormat()
public byte[] getEncoded()
what do you mean under 'check format'???
If you want to look into raw key data , than
you probably would like to use
byte[] bytes=pair.getPrivate().ge
and than output byte array
You also can try to cast it into
RSAPrivateKey and
RSAPrivateCrtKey iterfaces
(not sure does cryptix use this interface too) and try methods of this interfaces to get some more data on key parameters..
ASKER
Thank you kollegov.
Now I can convert the private key to byte[] by using your method. Apart from checking the format I need to store the keys in .key file and then retrieve it to do encryption and decryption, but it seem that there is no function to covert it from encoded format back to private/public key format. do you know any? I try to write the byte[] to file and then retrieve them thru fileinputstream, but could not convert it back to keys again. could you pls advise?
Could you all pls provide some sample RSA encryption code? I will greatly appreciate it.
thank you very much..
regards HuangJo
bytes=pair.getPrivate().ge tEncoded() ;
Now I can convert the private key to byte[] by using your method. Apart from checking the format I need to store the keys in .key file and then retrieve it to do encryption and decryption, but it seem that there is no function to covert it from encoded format back to private/public key format. do you know any? I try to write the byte[] to file and then retrieve them thru fileinputstream, but could not convert it back to keys again. could you pls advise?
Could you all pls provide some sample RSA encryption code? I will greatly appreciate it.
thank you very much..
regards HuangJo
bytes=pair.getPrivate().ge
1.in frameset rename frame from 'main' to 'myMain' (main can be reserved word on some systems)
2. The problem can be in duplicated
</frameset> tag at the end of your frameset html...
Now your frameset will be:
<html>
<head>
<title>Cummins Northwest,inc Information Pages</title>
</head>
<frameset cols="20%,*">
<frame src="nav.html" scrolling="auto" name="nav" frameborder=0>
<frame src="main.html" name="myMain" scrolling="auto" frameborder=0>
</frameset>
</html>
3. Some additional check required in order to avoid errors, so arrange the function..
4. remove </option>'s
And, well, finally your navigation frame html will be:
<html>
<head>
<script>
function doGo(){
inx=document.gotolocation. dropdown.s electedInd ex;
if(inx>=0){
top.myMain.location = document.gotolocation.drop down.optio ns[inx].va lue+"";
}
return false;
}
</script>
</head>
<body>
<FORM name="gotolocation" method="POST">
<SELECT name="dropdown" size=1>
<OPTION VALUE="Acct.html">Accounti ng
<OPTION VALUE="brocr.html">Brocr's
<OPTION VALUE="ChoR.html">Chart of Requests
<OPTION VALUE="CheckR.html">Check Requests
<OPTION VALUE="Exp.html">Expense Reports
</SELECT>
<INPUT type="button" onClick="return doGo()" value="Go">
</body>
</html>
If this wouldn't work I'll eat my hat :)
2. The problem can be in duplicated
</frameset> tag at the end of your frameset html...
Now your frameset will be:
<html>
<head>
<title>Cummins Northwest,inc Information Pages</title>
</head>
<frameset cols="20%,*">
<frame src="nav.html" scrolling="auto" name="nav" frameborder=0>
<frame src="main.html" name="myMain" scrolling="auto" frameborder=0>
</frameset>
</html>
3. Some additional check required in order to avoid errors, so arrange the function..
4. remove </option>'s
And, well, finally your navigation frame html will be:
<html>
<head>
<script>
function doGo(){
inx=document.gotolocation.
if(inx>=0){
top.myMain.location = document.gotolocation.drop
}
return false;
}
</script>
</head>
<body>
<FORM name="gotolocation" method="POST">
<SELECT name="dropdown" size=1>
<OPTION VALUE="Acct.html">Accounti
<OPTION VALUE="brocr.html">Brocr's
<OPTION VALUE="ChoR.html">Chart of Requests
<OPTION VALUE="CheckR.html">Check Requests
<OPTION VALUE="Exp.html">Expense Reports
</SELECT>
<INPUT type="button" onClick="return doGo()" value="Go">
</body>
</html>
If this wouldn't work I'll eat my hat :)
Oppsss, sorry, I messed windows, and posted other comment here..
Sorry again..
The question is do you need to save bytes? or key in any predefined format?
If not that you can use serialization
features to store/restore objects..
yourObjectOutputStream.wri teObject(k ey)
key=(PrivateKey)yourObject InputStrea m.readObje ct();
Wouldn't it be easier than restoring
key object from bytes????
Sorry again..
The question is do you need to save bytes? or key in any predefined format?
If not that you can use serialization
features to store/restore objects..
yourObjectOutputStream.wri
key=(PrivateKey)yourObject
Wouldn't it be easier than restoring
key object from bytes????
I can email JCE to you if you need.