java rsa .net,Java和.NET互操作(RSA)签名

博客作者在尝试使用C#的智能卡进行RSA签名,并在Java环境中验证该签名,但遇到了验证失败的问题。经过检查,输入输出数据传输、密钥设置和数据一致,但发现Java和C#生成的签名不同。作者怀疑可能是实现不兼容导致的。文中提到了Java中生成RSA密钥对的方法,并展示了如何创建和设置私钥。
摘要由CSDN通过智能技术生成

I'm signing some data on a .net-based smartcard and trying to verify that signature in a java environment - but without success.

Smartcard (c#):

RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(1024);

// In a different method, rsaParams.Exponent and rsaParams.Modulus are set

rsaProvider.ImportParameters(rsaParams); // Here I'm importing the key

SHA1 sha1 = SHA1.Create();

byte[] signature = rsaProvider.SignData(data, sha1);

Client (Java):

Signature sig = Signature.getInstance("SHA1withRSA");

sig.initVerify(rsaPublicKey); // initiate the signature with public key

sig.update(data); // update signature with the data that was signed by the card

sig.verify(signedData); // Test card signature - this always returns false

I then tried to create the signature on the Java client (for testing) - and it turns out that the signature created on the Java client is different from the one created on the smartcard. I created it like this:

Signature sig = Signature.getInstance("SHA1withRSA");

sig.initSign(rsaPrivateKey);

sig.update(data);

locallySigned = sig.sign();

Now I understand that the signature is something like the hash of (passed data + the used algorithm). Is it possible that the implementations are not compatible here? Am I missing something else? Thanks!

PS: Yes, I verified that both input and output are transferred correctly from/to the card, that the key parameters are set and that input/output are exactly the same.

Edit: Key Generation in Java:

// Create a key-pair and install the private key on the card

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");

SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");

keyGen.initialize(1024, random);

KeyPair keyPair = keyGen.genKeyPair();

privateKey = (RSAPrivateKey)keyPair.getPrivate();

publicKey = (RSAPublicKey)keyPair.getPublic();

and then I'm setting exp and mod of the private key on the card.

解决方案

// In a different method, rsaParams.Exponent and rsaParams.Modulus are set

To set the private exponent in an RSA key, you should use RSAParameters.D. RSAParameters.Exponent is for the public exponent.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值