KeyStore Persistence

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • girishprabhakar
    New Member
    • May 2007
    • 1

    KeyStore Persistence

    Hi,

    i have developed a code for encryption and decryption of credit card number.
    For this i am using java.security.K eyStore.

    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.securit y.x509.CertAndK eyGen keypair;

    keypair = new com.ibm.securit y.x509.CertAndK eyGen("RSA",
    "MD5WithRSA ");
    com.ibm.securit y.x509.X500Name x500Name;

    x500Name = new com.ibm.securit y.x509.X500Name ("IGEL RemoteManager",
    "IGEL Technology GmbH", "IGEL Technology GmbH", "DE");
    keypair.generat e(1024);
    PrivateKey thisPrivateKey = keypair.getPriv ateKey();
    PublicKey thisPublicKey = keypair.getPubl icKey();
    System.out.prin tln("Private Key : " + thisPrivateKey) ;
    System.out.prin tln("Private Key : " + thisPublicKey);
    java.security.c ert.X509Certifi cate[] chain = new java.security.c ert.X509Certifi cate[1];

    chain[0] = keypair
    .getSelfCertifi cate(x500Name, 7000 * 24 * 60 * 60);

    System.out.prin tln("chain " + chain[0]);
    Key key = (Key) (thisPrivateKey );
    //Certificate cert = (Certificate)(c hain[0]);
    String str = "ABC";
    char ch[] = str.toCharArray ();

    KeyStore ks;

    ks = KeyStore.getIns tance(KeyStore. getDefaultType( ));
    // String keyStoreType = KeyStore.getDef aultType();
    // System.out.prin tln("keyStoreTy pe is ---"+keyStoreType) ;
    // Provider provider = ks.getProvider( );
    // System.out.prin tln("Provider** *********"+prov ider);

    ks.load(null, ch);



    ks.setKeyEntry( "alias", key, ch, chain);
    System.out.prin tln(ks.getCerti ficate("alias") );

    FileOutputStrea m fos = new FileOutputStrea m("d:/Girish2.keystor e");
    ks.store(fos, ch);
    fos.close();
    System.out.prin tln("KeyStore is **************" +ks.toString()) ;

    X509Certificate car = (com.ibm.securi ty.x509.X509Cer tImpl) ks
    .getCertificate ("alias");
    //X509CertImpl car = ( X509CertImpl) ks.getCertifica te("alias");
    System.out.prin tln("Certificat e is +++++++++++++++ " + car);

    System.out.prin tln("********** ********" + ks.getKey("alia s", ch));

    SecureRandom srandom = new SecureRandom();

    Cipher cp;

    cp = Cipher.getInsta nce("RSA/ECB/PKCS1Padding");

    cp.init(Cipher. ENCRYPT_MODE, car, srandom);
    // PrivateKey pkEntry = (PrivateKey)ks. getKey("testali as",ch1);

    String input = "12345678901234 56";

    byte[] inputBytes = input.getBytes( );

    byte[] encryptedBytes;

    encryptedBytes = cp.doFinal(inpu tBytes);
    System.out.prin tln("certificat e--" + car);
    System.out.prin tln("Encrypted Code-----" + encryptedBytes) ;

    Random rand = new Random(1000);
    Integer ixd = new Integer(rand.ne xtInt());
    entityHome = (EncryptEntityH ome) PortableRemoteO bject.narrow(ob j,
    EncryptEntityHo me.class);
    encryptEntity = entityHome.crea te(new Integer(100));
    encryptEntity.s etAlias("alias" );
    encryptEntity.s etEncryptString (encryptedBytes .toString());
    System.out.prin tln("Encrypt Entity is : " + encryptEntity);
Working...