Hi,
i have developed a code for encryption and decryption of credit card number.
For this i am using java.security.KeyStore.
I have created public and private keys and a certificate and stored them in this KeyStore. I am able to retreive the keys and certificate from the keystore,.
BUT i a not able to store this keystore ( or make it persist).
I need to persist this keystore so that i can load it in other file and extract the information from it.
Please tell me how to store this keystore.
I am using this code :
com.ibm.security.x509.CertAndKeyGen keypair;
keypair = new com.ibm.security.x509.CertAndKeyGen("RSA",
"MD5WithRSA");
com.ibm.security.x509.X500Name x500Name;
x500Name = new com.ibm.security.x509.X500Name("IGEL RemoteManager",
"IGEL Technology GmbH", "IGEL Technology GmbH", "DE");
keypair.generate(1024);
PrivateKey thisPrivateKey = keypair.getPrivateKey();
PublicKey thisPublicKey = keypair.getPublicKey();
System.out.println("Private Key : " + thisPrivateKey);
System.out.println("Private Key : " + thisPublicKey);
java.security.cert.X509Certificate[] chain = new java.security.cert.X509Certificate[1];
chain[0] = keypair
.getSelfCertificate(x500Name, 7000 * 24 * 60 * 60);
System.out.println("chain " + chain[0]);
Key key = (Key) (thisPrivateKey);
//Certificate cert = (Certificate)(chain[0]);
String str = "ABC";
char ch[] = str.toCharArray();
KeyStore ks;
ks = KeyStore.getInstance(KeyStore.getDefaultType());
// String keyStoreType = KeyStore.getDefaultType();
// System.out.println("keyStoreType is ---"+keyStoreType);
// Provider provider = ks.getProvider();
// System.out.println("Provider***********"+provider) ;
ks.load(null, ch);
ks.setKeyEntry("alias", key, ch, chain);
System.out.println(ks.getCertificate("alias"));
FileOutputStream fos = new FileOutputStream("d:/Girish2.keystore");
ks.store(fos, ch);
fos.close();
System.out.println("KeyStore is **************" +ks.toString());
X509Certificate car = (com.ibm.security.x509.X509CertImpl) ks
.getCertificate("alias");
//X509CertImpl car = ( X509CertImpl) ks.getCertificate("alias");
System.out.println("Certificate is +++++++++++++++" + car);
System.out.println("******************" + ks.getKey("alias", ch));
SecureRandom srandom = new SecureRandom();
Cipher cp;
cp = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cp.init(Cipher.ENCRYPT_MODE, car, srandom);
// PrivateKey pkEntry = (PrivateKey)ks.getKey("testalias",ch1);
String input = "1234567890123456";
byte[] inputBytes = input.getBytes();
byte[] encryptedBytes;
encryptedBytes = cp.doFinal(inputBytes);
System.out.println("certificate--" + car);
System.out.println("Encrypted Code-----" + encryptedBytes);
Random rand = new Random(1000);
Integer ixd = new Integer(rand.nextInt());
entityHome = (EncryptEntityHome) PortableRemoteObject.narrow(obj,
EncryptEntityHome.class);
encryptEntity = entityHome.create(new Integer(100));
encryptEntity.setAlias("alias");
encryptEntity.setEncryptString(encryptedBytes.toSt ring());
System.out.println("Encrypt Entity is : " + encryptEntity);