2011-01-14 4 views
1

Webサービス呼び出しの署名タグを手動で作成しようとしています。私はキーストアから証明書にアクセスし、証明書の公開鍵にアクセスしました。私は今RSAKeyValueをds:CryptoBinary型に変換する際に問題があります。コードはmudulusと指数のBiginteger値を返します。私はそれらを八重奏に変換してからBas64に変換する方法やアルゴリズムを探しています。ここに私のコードですBigIntegerからオクテットへの変換?

RSAPublicKey rsaKey = (RSAPublicKey)certificate.getPublicKey(); 
customSignature.Modulus = rsaKey.getModulus(); 
customSignature.Exponent = rsaKey.getPublicExponent(); 

整数をオクテット表現に変換するソリューションはありますか?

答えて

2

は、Apache Commonsのコーデックのフレームワークを使用して、次のコードを試してみてください。

BigInteger modulus = rsaKey.getModulus(); 
org.apache.commons.codec.binary.Base64.encodeBase64String(modulus.toByteArray()); 
+0

ありがとう、とてもシンプルです –

0

残念ながら、modulus.toByteArray()も先行ゼロオクテットストリッピング必要とXMLデジタル署名のds:CryptoBinaryタイプに直接マップされません。 base64エンコーディングを行う前に、次のようなことをする必要があります。

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); 
} 
関連する問題