Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从BigInteger到octet的转换?_Java_Rsa_Public Key Encryption - Fatal编程技术网

Java 从BigInteger到octet的转换?

Java 从BigInteger到octet的转换?,java,rsa,public-key-encryption,Java,Rsa,Public Key Encryption,我正在尝试为web服务调用手动创建签名标记。我从密钥库访问了证书,并访问了证书的公钥。现在,我在将RSAKeyValue转换为ds:CryptoBinary类型时遇到了问题。代码返回mudulus和exponent的Biginteger值,我正在寻找一种方法或算法将它们转换为八位字节,然后转换为Bas64。这是我的密码 RSAPublicKey rsaKey = (RSAPublicKey)certificate.getPublicKey(); customSignature.Modulus

我正在尝试为web服务调用手动创建签名标记。我从密钥库访问了证书,并访问了证书的公钥。现在,我在将RSAKeyValue转换为ds:CryptoBinary类型时遇到了问题。代码返回mudulus和exponent的Biginteger值,我正在寻找一种方法或算法将它们转换为八位字节,然后转换为Bas64。这是我的密码

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

Java中是否有将整数转换为八位字节表示的解决方案?

使用apache commons编解码器框架尝试以下代码:

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

不幸的是,
module.toByteArray()
没有直接映射到XML数字签名的类型,这也需要去掉前导的零八位字节。在进行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);
}