问题描述
我签署了一些数据上的基于.NET的智能卡,并试图验证在Java环境中的签名 - 但没有成功。
智能卡(C#):
的RSACryptoServiceProvider rsaProvider =新的RSACryptoServiceProvider(1024);
//在一种不同的方法,rsaParams.Exponent和rsaParams.Modulus被设置
rsaProvider.ImportParameters(rsaParams); //这里,我进口的关键
SHA1 SHA1 = SHA1.Create();
byte []的签名= rsaProvider.SignData(数据,SHA1);
客户端(JAVA):
签名SIG = Signature.getInstance(SHA1withRSA);
sig.initVerify(rsaPublicKey); //启动与公共密钥签名
sig.update(数据); //更新签名与该数据,将其通过卡签名
sig.verify(签名数据); //测试卡署名 - 这始终返回false
我又试图创建Java客户端(测试)上的签名 - 而且事实证明,Java客户端上创建的签名是在智能卡上创建的不同。我创造这样的:
签名SIG = Signature.getInstance(SHA1withRSA);
sig.initSign(rsaPrivateKey);
sig.update(数据);
locallySigned = sig.sign();
现在我明白签名是类似的(通过数据+所使用的算法)哈希值。是否有可能的实现是不兼容吗?我失去了什么东西?谢谢!
PS:是的,我证实,输入和输出都正确地从/向卡传送,即关键参数设置和输入/输出是完全一样的。
。编辑: Java中的密钥生成:
//创建一个密钥对,并安装在卡上的私钥
KeyPairGenerator的凯基= KeyPairGenerator.getInstance(RSA);
SecureRandom的随机= SecureRandom.getInstance(SHA1PRNG,SUN);
keyGen.initialize(1024,随机的);
密钥对密钥对= keyGen.genKeyPair();
privateKey =(RSAPrivateKey)keyPair.getPrivate();
公钥=(RSAPublicKey)keyPair.getPublic();
然后我设置EXP卡上的私钥和国防部。
要设置的私人的指数的RSA密钥,你应该使用的。 RSAParameters.Exponent
是公开的指数。
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.
To set the private exponent in an RSA key, you should use RSAParameters.D
. RSAParameters.Exponent
is for the public exponent.
这篇关于对(RSA)签名的Java和.NET互操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!