What are the correct steps to signing a token using SHA1 in PKI

I am implementing a PKI solution. My encryption of a clear text token is done as follows:

publicstatic String encrypt(String token)throws Exception{

Cipher cipher = Cipher.getInstance("RSA/NONE/PKCS1Padding", provider);

cipher.init(Cipher.ENCRYPT_MODE, publicKey);

byte[] stringBytes = token.getBytes("UTF8");

byte[] raw = cipher.doFinal(stringBytes);

// BASE64 encode the result

String s = encoder.encode(raw);

// URL Encode

String senc = URLEncoder.encode(s,"UTF-8");

return senc;

}

Now this is what I am doing in my code to sign the message..

publicstatic String sign(String token)throws Exception{

Signature signer = Signature.getInstance("SHA1/RSA", provider.getName());

signer.initSign(privateKey);

byte[] stringBytes = token.getBytes("UTF8");

signer.update(stringBytes);

byte[] signature = signer.sign();

// BASE64 encode the string result

String s = encoder.encode(signature);

String senc = URLEncoder.encode(s,"UTF-8");

return senc;

}

Please look at the method sign() and inform me if I am doing the following in the sign() method..

I am being asked to hash the clear token with SHA1 and then use the private key to encrypt it with RSA.

In my method I am signing the token using the private key but it looks like I am not hasking the token first and then signing it with the private key.

Can someone please provide me with a code snippet where in I am first hashing the token using SHA1 and then signing the key with my private key..

Thanks in advance..

[2388 byte] By [kiran_pothakamurya] at [2007-11-27 0:53:21]
# 1

The signature class does the hashing for you. You should not attempt to do it manually. Your method does what you want it to do. The only potential issue is the name you have specified for the signature instance, "SHA1/RSA". It is acceptable as an alias for the BC provider, but not for the SunRSA provider. On the other hand, "SHA1WithRSA" works for both.

ghstarka at 2007-7-11 23:25:12 > top of Java-index,Security,Cryptography...
# 2

Thanks a ton. I am using BC provider but I will switch to SHA1withRSA.

I read that Signature.update() does that but had to confirm with the experts on this excellent forum.

I do a bit of PKI implementation with various vendors every once in a while at work. I love this area of specilization and would love to gain proficiency on Security and Cryptography. Are there any good resources for people like me. I usually benefit from sample scenarios and code.

Thanks in advance..

kiran_pothakamurya at 2007-7-11 23:25:12 > top of Java-index,Security,Cryptography...