How to get alias name of stored certificate from iKey token 2032
843810Dec 23 2004 — edited Dec 26 2004Hi All,
Below is my code woks well to use the same keypair for both encrypt/decryprt-SunPKCS#11 in SDK1.5. In my code i hard coded alias name of certificate, kindly tell me how to read alias name of certificate from iKey token 2032??
import java.io.*;
import java.util.*;
import java.lang.*;
import java.sql.*;
import java.text.*;
import java.math.*;
import java.security.*;
import java.security.cert.*;
import java.security.interfaces.*;
import javax.crypto.interfaces.*;
import javax.net.ssl.*;
import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import java.security.KeyStore.*;
public class Encrypt
{
public Encrypt(){}
public void loginToken() {
Provider p = new sun.security.pkcs11.SunPKCS11(MQConfig.getvalue("SecurityPropertyPath"));
String myAlias = "349eefd1-845b-4ba4-9f88-06e9f5cb82f6";
/** to view alias name
keytool -list -v -keystore NONE -storetype PKCS11 -storepass PASSWORD
**/
Security.addProvider(p);
KeyStore ks = null;
PrivateKey privKey = null;
PublicKey pubKey = null;
try{
String password = General.ReadFiles(MQConfig.getvalue("logFilePath"),"Simple");
password = password.trim();
char pin[] = password.toCharArray();
ks = KeyStore.getInstance("pkcs11");
ks.load(null,pin);
java.security.cert.Certificate cert = ks.getCertificate(myAlias);
Key key = ks.getKey(myAlias, pin);
if(PrivateKey.class.isInstance(key)) {
privKey = (PrivateKey)key;
pubKey = cert.getPublicKey();
}
FileInputStream in = new FileInputStream("C:\\ReportDBBE.properties");
FileOutputStream out = new FileOutputStream("C:\\ReportDBAE.properties");
Cipher cp=Cipher.getInstance("RSA/ECB/PKCS1Padding", p);
cp.init(cp.ENCRYPT_MODE,pubKey);
CipherOutputStream cout=new CipherOutputStream(out,cp);
byte[] input=new byte[8];
int byteread=in.read(input);
while(byteread!=-1){
cout.write(input,0,byteread);
byteread=in.read(input);
}
cout.flush();
in.close();
cout.close();
}
catch(NoSuchAlgorithmException nsae)
{
System.out.println("No Such Algorithm Exception " + nsae.getMessage());
}
catch(NoSuchPaddingException nspe)
{
System.out.println("No Such Padding Exception " + nspe.getMessage());
}
catch(InvalidKeyException ike)
{
System.out.println("Invalid Key Exception " + ike.getMessage());
ike.printStackTrace();
}
catch(IllegalStateException ise)
{
System.out.println("Illegal State Exception " + ise.getMessage());
}
catch(KeyStoreException kse)
{
System.out.println("Key Store Exception " + kse.getMessage());
}
catch(CertificateException ce)
{
System.out.println("Certificate Exception " + ce.getMessage());
}
catch(IOException ioe)
{
System.out.println("IO Exception " + ioe.getMessage());
}
catch(UnrecoverableKeyException unrke)
{
System.out.println("Unrecoverable Key Exception " + unrke.getMessage());
}
}
public static void main (String args[]) throws Exception {
try{
Encrypt tl = new Encrypt();
tl.loginToken();
}catch(Exception e){
e.printStackTrace();
}
}
}
Your help is very much appreciated!!!!