Tag: cryptography

使用RSACryptoServiceProvider进行公钥加密

我已经在CodeProject上发表了一篇文章,解释了如何使用RSA提供程序进行加密和解密: RSA私钥加密 虽然2009年的旧版本是错误的,但新的2012版本(具有System.Numerics.BigInteger支持)似乎更可靠。 这个版本缺少的是一种使用公钥加密并使用私钥 解密的方法。 所以,我自己尝试了,但是当我解密时会得到垃圾。 我不熟悉RSA提供商,所以我在这里很黑暗。 很难找到关于它应该如何工作的更多信息。 有谁看到这有什么问题? 以下是带有PUBLIC密钥的ENcryption: // Add 4 byte padding to the data, and convert to BigInteger struct BigInteger numData = GetBig( AddPadding( data ) ); RSAParameters rsaParams = rsa.ExportParameters( false ); //BigInteger D = GetBig( rsaParams.D ); //only for private key BigInteger Exponent = GetBig( rsaParams.Exponent ); BigInteger Modulus […]

RSACryptoServiceProvider(.NET的RSA)可以使用SHA256进行加密(不签名)而不是SHA1吗?

加密时,RSACryptoServiceProvider(或.NET提供的任何其他RSA加密器)可以使用SHA256而不是SHA1吗? SHA1似乎是硬编码的,无法更改它。 例如,RSACryptoServiceProvider.SignatureAlgorithm被硬编码为返回“http://www.w3.org/2000/09/xmldsig#rsa-sha1”。 如果无法使RSACryptoServiceProvider使用SHA256,有哪些替代方案? 更新 以下代码完美地运行,但我想将OAEPWithSHA1AndMGF1Padding更改为OAEPWithSHA256AndMGF1Padding。 C#端需要什么才能使用SHA256而不是SHA1进行加密? 加密是在C#中完成的,使用: var parameters = new RSAParameters(); parameters.Exponent = new byte[] {0x01, 0x00, 0x01}; parameters.Modulus = new byte[] {0x9d, 0xc1, 0xcc, …}; rsa.ImportParameters(parameters); var cipherText = rsa.Encrypt(new byte[] { 0, 1, 2, 3 }, true); 解密是在Java中使用: Cipher cipher = Cipher.getInstance(“RSA/NONE/OAEPWithSHA1AndMGF1Padding”, “BC”); cipher.init(Cipher.DECRYPT_MODE, keyPair.getPrivate()); byte[] cipherText = …; byte[] plainText […]

C#如何validationRoot-CA-Cert证书(x509)链?

假设我有三个证书(Base64格式) Root | — CA | — Cert (client/signing/whatever) 如何在C#中validation证书和证书路径/链? (所有这三个证书可能都不在我的计算机证书库中) 编辑 :BouncyCastle具有validationfunction。 但我试图不使用任何第三方库。 byte[] b1 = Convert.FromBase64String(x509Str1); byte[] b2 = Convert.FromBase64String(x509Str2); X509Certificate cer1 = new X509CertificateParser().ReadCertificate(b1); X509Certificate cer2 = new X509CertificateParser().ReadCertificate(b2); cer1.Verify(cer2.GetPublicKey()); 如果cer1未由cert2(CA或root)签名,则会出现exception。 这正是我想要的。

使用纯.net Framework生成和签署证书请求

我正在尝试使用纯.net代码创建证书请求,并根据我可用的现有CA证书(在Windows证书存储区中或作为单独的文件)从证书请求创建证书。 我知道我有类X509Certificate和X509Certificate2可用于加载证书并获取对其信息的访问权限,但我没有在System.Security.Cryptography命名空间中看到可用于创建证书请求的任何类或function或签署此类证书请求以创建新的签名证书。 虽然System.Security.Cryptography.Pkcs命名空间的文档说: System.Security.Cryptography.Pkcs命名空间提供公钥加密标准(PKCS)的编程元素,包括签名数据,交换密钥, 请求证书 ,公钥加密和解密以及其他安全function的方法。 那么,如何使用System.Security.Cryptography纯.net类创建证书请求并签署该请求以创建新的X509证书? 注意: 我不想使用像openssl或MakeCert这样的外部可执行文件 我不想使用BouncyCastle 我不想使用Windows 证书注册API 我不想使用本机Win32 API函数

对于支持FIPS的系统,是否有针对MD5的备用哈希算法?

每当我尝试在启用了FIPS的Windows XP计算机上使用MD5时,我都会收到System.InvalidOperationException 。 在FIPS上是否应该使用替代算法而不是MD5?

在读卡器上找到智能卡上的证书

我使用Visual Studio 2013(C#)使用来自智能卡的证书对文档进行数字签名。 我无法识别当前插入读卡器的证书:( Windows复制插入读卡器中的所有卡的证书,并将其保存在商店中。 我想在读卡器中使用卡片。 我使用的代码是 public static byte[] Sign(Stream inData, string certSubject) { // Access Personal (MY) certificate store of current user X509Store my = new X509Store(StoreName.My, StoreLocation.CurrentUser); my.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly); // Find the certificate we’ll use to sign RSACryptoServiceProvider csp = null; foreach (X509Certificate2 cert in my.Certificates) { if (cert.Subject.Contains(certSubject)) { // […]

使用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())) { […]

有人有任何代码从C#调用SignerSignEx吗?

非常感谢在这里做了与SignerSignEx示例等效的.Net的东西: http://blogs.msdn.com/b/alejacma/archive/2008/12/11/how-to-sign-exe-files-with-an-authenticode-certificate-part-2.aspx?CommentPosted=true 谢谢!!!!!!!

使用SignedXml类添加基于Id属性的引用时出现“格式错误的引用元素”

当有名称空间前缀时,无法通过Id属性签署元素: void Main() { var doc = new XmlDocument(); doc.LoadXml(“Zebra”); SignedXml signedXml = new SignedXml(doc); signedXml.SigningKey = new RSACryptoServiceProvider(); Reference reference = new Reference(“#_0”); signedXml.AddReference(reference); signedXml.ComputeSignature(); } ComputeSignature()将在这里失败’错误的参考元素’应如何做到这一点?

.NET中的ECDiffieHellmanCng是否具有实现NIST SP 800-56A的密钥派生函数,第5.8.1节

我有一项任务需要使用NIST SP 800-56A第5.8.1节中描述的密钥导出函数来获取密钥材料。 我不是密码学方面的专家,所以如果问题是天真的,请原谅。 这是我到目前为止所做的: 我有另一方的公钥和我的私钥 现在我尝试使用C#(。NET 4)ECDiffieHellmanCng类使用ECDH 1.3.132.1.12生成共享密钥,如下所示: // The GetCngKey method reads the private key from a certificate in my Personal certificate store CngKey cngPrivateKey = GetCngKey(); ECDiffieHellmanCng ecDiffieHellmanCng = new ECDiffieHellmanCng(cngPrivateKey); ecDiffieHellmanCng.HashAlgorithm = CngAlgorithm.ECDiffieHellmanP256; ecDiffieHellmanCng.KeyDerivationFunction = ?? // What do I set here 最后这样做: ecDiffieHellmanCng.DeriveKeyMaterial(otherPartyPublicKey:); 在哪里/如何设置其他参数算法ID,Party U Info,Party V Info? 编辑我愿意使用像Bouncy Castle这样的其他库(前提是它们可以从.NET调用)