相关疑难解决方法(0)

将.Net RSA xml密钥移植到Java

我有来自.Net系统的私有和公共密钥,采用xml格式.我必须使用此密钥在Java中执行加密/解密.有什么办法吗?

公钥看起来像这样:

<RSAKeyValue>
    <Modulus>jHIxcGzzpByFv...pvhxFnP0ssmlBfMALis</Modulus>
    <Exponent>AQAB</Exponent>
</RSAKeyValue>
Run Code Online (Sandbox Code Playgroud)

私钥:

<RSAKeyValue>
    <Modulus>4hjg1ibWXHIlH...ssmlBfMAListzrgk=</Modulus>
    <Exponent>AQAB</Exponent>
    <P>8QZCtrmJcr9uW7VRex+diH...jLHV5StmuBs1+vZZAQ==</P>
    <Q>8CUvJTv...yeDszMWNCQ==</Q>
    <DP>elh2Nv...cygE3657AQ==</DP>
    <DQ>MBUh5XC...+PfiMfX0EQ==</DQ>
    <InverseQ>oxvsj4WCbQ....LyjggXg==</InverseQ>
    <D>KrhmqzAVasx...uxQ5VGZmZ6yOAE=</D>
</RSAKeyValue>
Run Code Online (Sandbox Code Playgroud)

我已经编写了一些代码来加密数据,但我不确定它是否正确.

        Element modulusElem = root.getChild("Modulus");
        Element exponentElem = root.getChild("Exponent");

        byte[] expBytes = decoder.decodeBuffer(exponentElem.getText().trim());
        byte[] modBytes = decoder.decodeBuffer(modulusElem.getText().trim());

        RSAPublicKeySpec keySpec = new RSAPublicKeySpec(new BigInteger(1, modBytes), new BigInteger(1, expBytes));
        KeyFactory fact = KeyFactory.getInstance("RSA");
        PublicKey pubKey = fact.generatePublic(keySpec);
Run Code Online (Sandbox Code Playgroud)

如何从xml中创建私钥来解密数据?

.net java encryption rsa

18
推荐指数
1
解决办法
9616
查看次数

Java等效于带有SHA-1的.NET RSACryptoServiceProvider

我在C#中有以下数据签名代码

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

string PrivateKeyText = "<RSAKeyValue><Modulus>....</D></RSAKeyValue>";

rsa.FromXmlString(PrivateKeyText);

string data = "my data";        

byte[] SignedByteData = rsa.SignData(Encoding.UTF8.GetBytes(data), new SHA1CryptoServiceProvider());
Run Code Online (Sandbox Code Playgroud)

我想在Java(Android)中重现相同的代码:

String modulusElem = "...";
String expElem = "...";

byte[] expBytes = Base64.decode(expElem, Base64.DEFAULT);
byte[] modulusBytes = Base64.decode(modulusElem, Base64.DEFAULT);

BigInteger modulus = new BigInteger(1, modulusBytes);
BigInteger exponent = new BigInteger(1, expBytes);

try {
    KeyFactory factory = KeyFactory.getInstance("RSA");

    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");

    String data = "my data";

    MessageDigest md = MessageDigest.getInstance("SHA-1");
    byte[] hashedData = md.digest(data.getBytes("UTF-8"));

    RSAPublicKeySpec pubSpec = new …
Run Code Online (Sandbox Code Playgroud)

.net c# java android cryptography

8
推荐指数
1
解决办法
2820
查看次数

标签 统计

.net ×2

java ×2

android ×1

c# ×1

cryptography ×1

encryption ×1

rsa ×1