相关疑难解决方法(0)

如何使用RSA和SHA256与.NET签署文件?

我的应用程序将采用一组文件并对其进行签名.(我不是要尝试签署程序集.)有一个.p12文件,我从中获取私钥.

这是我试图使用的代码,但我得到了一个System.Security.Cryptography.CryptographicException "Invalid algorithm specified.".

X509Certificate pXCert = new X509Certificate2(@"keyStore.p12", "password");
RSACryptoServiceProvider csp = (RSACryptoServiceProvider)pXCert.PrivateKey;
string id = CryptoConfig.MapNameToOID("SHA256");
return csp.SignData(File.ReadAllBytes(filePath), id);
Run Code Online (Sandbox Code Playgroud)

根据这个答案,它无法完成(RSACryptoServiceProvider不支持SHA-256),但我希望可以使用不同的库,如Bouncy Castle.

我是新手,我发现Bouncy Castle非常混乱.我正在将一个Java应用程序移植到C#,我必须使用相同类型的加密来签名文件,所以我坚持使用RSA + SHA256.

我怎么能用Bouncy Castle,OpenSSL.NET,Security.Cryptography或其他我没有听说过的第三方库来做到这一点?我假设,如果它可以在Java中完成,那么它可以在C#中完成.

更新:

这是我从poupou的anwser链接中得到的

        X509Certificate2 cert = new X509Certificate2(KeyStoreFile, password");
        RSACryptoServiceProvider rsacsp = (RSACryptoServiceProvider)cert.PrivateKey;
        CspParameters cspParam = new CspParameters();
        cspParam.KeyContainerName = rsacsp.CspKeyContainerInfo.KeyContainerName;
        cspParam.KeyNumber = rsacsp.CspKeyContainerInfo.KeyNumber == KeyNumber.Exchange ? 1 : 2;
        RSACryptoServiceProvider aescsp = new RSACryptoServiceProvider(cspParam);
        aescsp.PersistKeyInCsp = false;
        byte[] signed = aescsp.SignData(File.ReadAllBytes(file), "SHA256");
        bool isValid …
Run Code Online (Sandbox Code Playgroud)

.net c# openssl cryptography bouncycastle

43
推荐指数
6
解决办法
8万
查看次数

C# 使用 RSA 签名和验证签名。编码问题

我的问题与 2011 年的一种形式非常相似,Signing and verifying signatures with RSA C#。尽管如此,当我比较签名数据和原始消息时,我也会出错。请指出我的错误。

代码:

 public static void Main(string[] args)
    {            

        //Generate a public/private key pair.  
        RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

        //Save the public key information to an RSAParameters structure.
        RSAParameters RSAPublicKeyInfo = RSA.ExportParameters(false);
        RSAParameters RSAPrivateKeyInfo = RSA.ExportParameters(true);    

        string message = "2017-04-10T09:37:35.351Z";

        string signedMessage = SignData(message, RSAPrivateKeyInfo);

        bool success = VerifyData(message, signedMessage, RSAPublicKeyInfo);

        Console.WriteLine($"success {success}");

        Console.ReadLine();
    }   
Run Code Online (Sandbox Code Playgroud)

签约方式:

 public static string SignData(string message, RSAParameters privateKey)
    {
        ASCIIEncoding byteConverter = new ASCIIEncoding();

        byte[] signedBytes; …
Run Code Online (Sandbox Code Playgroud)

c# encoding signing rsa

4
推荐指数
1
解决办法
3485
查看次数

System.Security.Cryptography.CryptographicException:RSACryptoserviceProvider 中的长度错误

我想RSACryptoServiceProvider在 wp8 项目中使用c#加密和解密数据。我正在创建非对称密钥:

CspParameters parameters = new CspParameters();
parameters.KeyContainerName = "MyContainer";

RSACryptoServiceProvider provider = new RSACryptoServiceProvider(parameters);  
Run Code Online (Sandbox Code Playgroud)

现在我想加密数据。我在做:

CspParameters parameters = new CspParameters();

parameters.KeyContainerName = "MyContainer";
RSACryptoServiceProvider obj = new RSACryptoServiceProvider(parameters);
byte[] a = Generic.RSAEncrypt(ByteConverter.GetBytes(s[0]),
                              obj.ExportParameters(false), false); 

public static byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo,
                                bool DoOAEPPadding)
{
    try {
        byte[] encryptedData;
        //Create a new instance of RSACryptoServiceProvider. 
        CspParameters parameters = new CspParameters();
        parameters.KeyContainerName = "TCSContainer";
        using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(parameters))
        {
            //Import the RSA Key information. This only …
Run Code Online (Sandbox Code Playgroud)

c# encryption rsa windows-phone-8

3
推荐指数
1
解决办法
1万
查看次数

如何在 .net 中为 google 云存储签名 url

我想知道如何在 .net 中使用谷歌云存储类生成 signurl

我已经按照要求创建了字符串

GET


1388534400
/bucket/objectname
Run Code Online (Sandbox Code Playgroud)

但我现在想用 p12 密钥对这个 url 进行签名,然后想让它对 url 友好

这个库没有显示它的特定功能 - > https://developers.google.com/resources/api-libraries/documentation/storage/v1/csharp/latest/annotated.html

所以,基本上我需要 .net 替代 Google_Signer_P12 类的 php

$signer = new Google_Signer_P12(file_get_contents(__DIR__.'/'."final.p12"), "notasecret");
$signature = $signer->sign($to_sign);
Run Code Online (Sandbox Code Playgroud)

.net php c# asp.net google-cloud-storage

2
推荐指数
2
解决办法
2992
查看次数

在 C# 中使用私钥对数据进行签名

我需要使用算法 SHA1RSA ,Rsa 密钥长度 2048 和 64 基本编码,用一个私钥对一些数据进行签名。我的代码是这样的

string sPayload = "";
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("URI");
httpWebRequest.ContentType = "application/json; charset=utf-8";
httpWebRequest.Method = WebRequestMethods.Http.Post;

using (StreamWriter streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
    sPayload = "{\"id\":\"14123213213\"," +
                "\"uid\":\"teller\"," +
                "\"pwd\":\"abc123\"," +
                "\"apiKey\":\"2343243\"," +
                "\"agentRefNo\":\"234324324\"}";

    httpWebRequest.Headers.Add("SIGNATURE", Convert.ToBase64String(new System.Security.Cryptography.SHA1CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(sPayload))));
    
    streamWriter.Write(sPayload);
    streamWriter.Flush();
    streamWriter.Close();
}

System.Net.ServicePointManager.Expect100Continue = false;

HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
    string result = streamReader.ReadToEnd();
}
Run Code Online (Sandbox Code Playgroud)

在标头名称签名中,我需要使用私钥传递签名数据(sPayload)。但是使用上面的代码会出现来自第三方的“无效签名”错误,我不确定加密部分是否正确。

httpWebRequest.Headers.Add("SIGNATURE", Convert.ToBase64String(new System.Security.Cryptography.SHA1CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(sPayload))));
Run Code Online (Sandbox Code Playgroud)

第三方提供了一个证书(cert、sha1)和密钥。我应该将其参考代码吗?

c# cryptography rsa httprequest digital-signature

2
推荐指数
1
解决办法
3万
查看次数