How to create an RSA key pair so that it can be stored in a database?

I am trying to implement a mutation encryption algorithm (theoretically developed by me), and as part of the requirements I need to create an RSA key pair and store it in the database so that it can be found later as part (used only to encrypt the session key so that it can be securely transmitted with an encrypted message).

My attempt to create an RSA key pair seems to work, but it continues to generate the same values โ€‹โ€‹over and over, instead of giving new pairs every time the code runs. What have I done wrong? also, if these values โ€‹โ€‹depend on the device itself (the same values โ€‹โ€‹are displayed), is there a way to associate the generation of a key pair with a specified email address so that every time a new email address is entered, another RSA key pair is issued

The following code is my attempt to generate a key pair:

import java.security.*; import java.security.*; /** * @author Speedy gonzales */ public class test { public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchProviderException { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(1024); byte[] publicKey = keyGen.genKeyPair().getPublic().getEncoded(); StringBuffer retString1 = new StringBuffer(); retString1.append("["); for (int puk = 0; puk < publicKey.length; ++puk) { retString1.append(publicKey[puk]); // retString1.append(", "); } retString1 = retString1.delete(retString1.length()-2,retString1.length()); retString1.append("]"); System.out.println(retString1); byte[] privateKey = keyGen.genKeyPair().getPrivate().getEncoded(); StringBuffer retString2 = new StringBuffer(); retString2.append("["); for (int pri = 0; pri < privateKey.length; ++pri) { retString2.append(privateKey[pri]); // retString2.append(", "); } retString2 = retString2.delete(retString2.length()-2,retString2.length()); retString2.append("]"); System.out.println(retString2); } } 

thanks

+4
source share
1 answer

Well, your first problem:

 keyGen.genKeyPair().getPublic().getEncoded(); keyGen.genKeyPair().getPrivate().getEncoded(); 

You do not save the key pair, so you generate public and private keys that do not match. From javadocs, genKeyPair() behaves as follows:

This will generate a new key pair every time it is called.

Secondly, getEncoded () simply returns the key as an array of bytes. If your database can store binary values, just save it that way. Otherwise, you would probably be lucky to turn it into a String. For example, you can use base 64 with this neat little trick (perhaps more reliable than you do):

 String keyAsString = new BigInteger(publicKey.getEncoded()).toString(64); 

Subsequently, you can return the original bytes with:

 byte[] bytes = new BigInteger(keyAsString, 64).toByteArray(); 

You say you get the same value every time you start (and make sure you worry about it before), and I'm not 100% sure why. You must have access to the parameters of the algorithm (you may need to impose a key on another type), try printing them to see if they match. Someone mentioned checking your random number generator, which might also be a good idea.

+5
source

All Articles