Conversion from BigInteger to octet?
I am trying to manually create the signature tag for a web service call. I accessed the certificate from the keystore and accessed the public key for the certificate. I now have the problem in converting the RSAKeyValue to ds:CryptoBinary type. Code returns t开发者_如何转开发he Biginteger value for mudulus and exponent and I am looking for a method or algorithm to convert them to octets and then converting to Bas64. Here is my code
RSAPublicKey rsaKey = (RSAPublicKey)certificate.getPublicKey();
customSignature.Modulus = rsaKey.getModulus();
customSignature.Exponent = rsaKey.getPublicExponent();
Is there any solution available in Java for converting the integers to octet representation?
Try following code using apache commons codec framework:
BigInteger modulus = rsaKey.getModulus();
org.apache.commons.codec.binary.Base64.encodeBase64String(modulus.toByteArray());
Unfortunatly, modulus.toByteArray()
does not map directly to the XML Digital Signature's ds:CryptoBinary type, which also requires stripping leading zero octets. You need to do something like the following, before you do the base64 encoding
byte[] modulusBytes = modulus.toByteArray();
int numLeadingZeroBytes = 0;
while( modulusBytes[numLeadingZeroBytes] == 0 )
++numLeadingZeroBytes;
if ( numLeadingZeroBytes > 0 ) {
byte[] origModulusBytes = modulusBytes;
modulusBytes = new byte[origModulusBytes.length - numLeadingZeroBytes];
System.arraycopy(origModulusBytes,numLeadingZeroBytes,modulusBytes,0,modulusBytes.length);
}
精彩评论