我正在尝试使用CNG API和Microsoft证书库中的证书使用ECDSA签署文件.我已阅读了大量文档,并且已接近完成但我仍然挂断了从证书导入私钥.我用RSA完成了同样的事情,但似乎完全不同.这是我到目前为止的代码:
static void signFile()
{
X509Certificate2 myCert =
selectCert(StoreName.My,
StoreLocation.CurrentUser,
"Select a Certificate",
"Please select a certificate from the list below:");
Console.Write("Path for file to sign: ");
string path = Console.ReadLine();
TextReader file = null;
try
{
file = new StreamReader(path);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.Write("\nPress any key to return to the main menu: ");
Console.ReadKey();
}
UnicodeEncoding encoding = new UnicodeEncoding();
byte[] data = encoding.GetBytes(file.ReadToEnd());
ECDsaCng dsa = new ECDsaCng(
CngKey.Import(StringToByteArray(myCert.PrivateKey.ToString()),
CngKeyBlobFormat.EccPrivateBlob,
CngProvider.MicrosoftSoftwareKeyStorageProvider));
dsa.HashAlgorithm = CngAlgorithm.Sha384; …Run Code Online (Sandbox Code Playgroud) 我正在编写一个程序(在Java中使用Bouncy Castle API),它使用AES-256/GCM加密文件,并使用由一个EC密钥对生成的密钥.我有对称加密部分工作完美但现在密钥生成证明是困难的.尝试在javax.crypto.KeyAgreement.init()方法中使用ECPublicKey对象时,它返回此错误:
Exception in thread "main" java.security.InvalidKeyException: ECDH key agreement requires ECPrivateKey for initialisation
at org.bouncycastle.jcajce.provider.asymmetric.ec.KeyAgreementSpi.initFromKey(Unknown Source)
at org.bouncycastle.jcajce.provider.asymmetric.ec.KeyAgreementSpi.engineInit(Unknown Source)
at javax.crypto.KeyAgreement.init(KeyAgreement.java:461)
at javax.crypto.KeyAgreement.init(KeyAgreement.java:435)
at Encrpytion.generateKey(Encrpytion.java:188) ---aKA.init(key);---
at Encrpytion.main(Encrpytion.java:40) ---byte[] key = generateKey();---
Run Code Online (Sandbox Code Playgroud)
该方法的源代码如下:
public static byte[] generateKey() {
KeyStore ks = KeyStore.getInstance("PKCS12");
FileInputStream fis = new FileInputStream("file.pfx");
Scanner input = new Scanner(System.in);
char[] chars = {};
System.out.println("Enter the password for your .pfx: ");
chars = input.nextLine().toCharArray();
input.close();
Enumeration aliasEnum = null;
ks.load(fis, chars);
aliasEnum = ks.aliases();
Key key …Run Code Online (Sandbox Code Playgroud) 我一直试图找到如何通过ANY MEANS从文件导入ECC密钥多年.我尝试从Windows证书存储区,.p12文件和PKCS#8 OpenSSL密钥文件访问ECC证书,但没有成功.
我尝试过的很多事情中只有一个是:
StreamReader fs = new StreamReader("key.pem");
String key = fs.ReadToEnd();
byte[] tempkey = System.Text.Encoding.ASCII.GetBytes(key);
CngKey cngKey = CngKey.Import(tempkey, CngKeyBlobFormat.Pkcs8PrivateBlob);
Run Code Online (Sandbox Code Playgroud)
但是没有给出描述性错误消息,只有最后一行有invalid parameters或者在上面的代码的情况下An error occurred during encode or decode operation.
我正在使用MSDN中的示例程序进行ECDH并将其更改为使用AES的GCM模式.所有这一切都正常,直到我尝试使用预生成的ECC密钥而不是默认情况下在运行时为Microsoft示例创建的ECC密钥.我已经随机尝试了所有的CngKeyBlobFormats但是老实说,我无法很好地调试它,因为我不知道这些特定格式在原始数据中的样子.
我的密钥是以下格式,但我愿意使用任何可用的格式(.p12,Microsoft Store,PKCS#8等)
key.pem
-----BEGIN EC PARAMETERS-----
##############################
-----END EC PARAMETERS-----
-----BEGIN EC PRIVATE KEY-----
##############################
-----END EC PRIVATE KEY-----
Run Code Online (Sandbox Code Playgroud)
资源
关于如何生成公钥和私钥ECC密钥对,我一直在谷歌和微软的Crypto API上花了好几个小时.该ECDiffieHellmanCng班(http://msdn.microsoft.com/en-us/library/system.security.cryptography.ecdiffiehellmancng.aspx#Y3081)列出了一个例子,但我不知道如何直接访问私钥.
有关该程序的一些背景知识,它是一个C#控制台应用程序,用于管理TrueCrypt会话,AES预共享密钥加密和ECDH/AES加密.我需要一个函数来创建一个公钥/私钥对以保存到文件,然后记录如何在包装的AES加密文件中使用这些生成的密钥(而不是像运行示例所示在运行时生成它).此外,我知道将文件保存到硬盘驱动器时出现的所有漏洞,但我并不担心这个程序,我正在考虑2台客户端计算机是安全的.
另请注意,我不想使用BouncyCastle API.