我的任务如下:
output.dat.下面的程序抛出错误:"java.security.InvalidKeyException:没有安装的提供程序支持此密钥:sun.security.provider.DSAPublicKeyImpl".
import java.security.*;
import java.security.KeyStore.*;
import java.io.*;
import java.security.PublicKey;
import java.security.PrivateKey;
import javax.crypto.Cipher;
import java.nio.charset.*;
import sun.security.provider.*;
import javax.crypto.*;
public class Code {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
/* getting data for keystore */
File file = new File(System.getProperty("user.home") + File.separatorChar + ".keystore");
FileInputStream is = new FileInputStream(file);
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
/*Information for certificate to be generated */
String …Run Code Online (Sandbox Code Playgroud) 我正在使用python-crypto API在Python2中编写一个客户端来对XML文件进行数字签名,我有一个用Scala编写的服务,用于验证签名.我的Python代码看起来像这样:
from Crypto.PublicKey import RSA
from Crypto.Hash import SHA
from os import urandom
import logging
...
...
Run Code Online (Sandbox Code Playgroud)
要生成密钥(keysize为2048):
self.__key = RSA.generate(self.keySize,urandom)
self.__private_key = self.__key.exportKey()
self.__public_key = self.__key.publickey().exportKey()
with open(pubPath,'w') as fpub:
logging.info("Writing Public Key to %s" % pubPath)
fpub.write(self.__public_key)
with open(priPath,'w') as fpri:
logging.info("Writing Private Key to %s" % priPath)
fpri.write(self.__private_key)
Run Code Online (Sandbox Code Playgroud)
并且用于阅读键:
self.__private_key = fpri.read()
self.__public_key = fpub.read()
self.__key = RSA.importKey(self.__private_key)
Run Code Online (Sandbox Code Playgroud)
并进行数字签名
logging.debug('Data to sign: "%s"' % data)
digest = SHA.new(data.strip()).digest()
return str(self.__key.sign(digest, None)[0])
Run Code Online (Sandbox Code Playgroud)
然后在Scala/Java中,我使用以下内容:
package com.example.security
import com.example.action.ActionRequest …Run Code Online (Sandbox Code Playgroud) 我对将具有RSA签名的SHA-1哈希应用于某些数据感兴趣,但是我需要分两个步骤进行-先应用哈希,然后对数据签名。Signature.sign()函数似乎会创建一个更复杂的(ASN.1?)数据结构,该数据结构最终会被签名(请参阅此问题)。如何在不使用BouncyCastle之类的任何外部库的情况下使两者等效?
应用哈希并使用签名单步登录:
PrivateKey privatekey = (PrivateKey) keyStore.getKey(alias, null);
...
sig = Signature.getInstance("SHA1withRSA", "SunMSCAPI");
sig.initSign(privatekey);
sig.update(data_to_sign);
byte[] bSignedData_CAPISHA1_CAPIRSA = sig.sign();
Run Code Online (Sandbox Code Playgroud)
通过MessageDigest应用哈希,然后使用签名签名:
PrivateKey privatekey = (PrivateKey) keyStore.getKey(alias, null);
...
MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
byte[] data_to_sign = sha1.digest(bdataToSign);
Signature sig = Signature.getInstance("NONEwithRSA", "SunMSCAPI");
sig.initSign(privatekey);
sig.update(data_to_sign);
byte[] bSignedData_JAVASHA1_CAPIRSA = sig.sign();
...
Run Code Online (Sandbox Code Playgroud)
我正在寻找以下等效项:
bSignedData_JAVASHA1_CAPIRSA == bSignedData_CAPISHA1_CAPIRSA
Run Code Online (Sandbox Code Playgroud)
我的最终目标是创建哈希,然后使用PKCS11令牌签名,但出于验证目的,我要求签名的数据必须与旧数据的格式相同。