Tag: rsa

C#BouncyCastle – 使用公钥/私钥进行RSA加密

我需要在C#中加密数据,以便将其传递给Java。 Java代码属于第三方但我得到了相关的源代码,因此我决定,当Java使用Bouncy Castle库时,我将使用C#端口。 解密工作正常。 但是,只有当我使用私钥加密而不使用公钥时,解密才有效。 使用公钥时,解密会因unknown block type失败。 显然, RsaEncryptWithPrivate的加密在加密时使用公钥,所以我不明白为什么这两种加密方法在function上不相同: using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Encodings; using Org.BouncyCastle.Crypto.Engines; using Org.BouncyCastle.OpenSsl; public class EncryptionClass { public string RsaEncryptWithPublic(string clearText , string publicKey) { var bytesToEncrypt = Encoding.UTF8.GetBytes(clearText); var encryptEngine = new Pkcs1Encoding(new RsaEngine()); using (var txtreader = new StringReader(publicKey)) { var keyParameter = (AsymmetricKeyParameter)new PemReader(txtreader).ReadObject(); encryptEngine.Init(true, keyParameter); } […]

c#中的数字签名,不使用BouncyCastle

如果不使用第三方BouncyCastle库,有没有办法读取自定义私钥并签署消息? (sha256 hash +使用私钥加密)

为什么RSAParameters模数不等于P和Q的乘积?

P和Q的值与.Net RSAParameters的模数值不匹配。 根据RSA算法和MSDN文档,它应该是:P * Q =模数 我生成了一个512位RSA密钥对,并通过调用将其导出为XML: RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(512); rsa.ToXmlString(true); 这给了我以下XML: rcLI1XTfmXtX05zq67d1wujnUvevBu8dZ5Q5uBUi2mKndH1FZLYCKrjFaDTB/mXW1l5C74YycVLS6msY2NNJYw== AQAB 1dwGkK5POlcGCjQ96Se5NSPu/hCm8F5EYwyqRpLVzgk= 0AAEMHBj7CP2XHfCG/RzGldw1GdsW13rTo3uEE9Dtws= PO4jMLV4/TYuElowCW235twGC3zTE0jIUzAYk2LiZ4E= ELJ/o5fSHanBZCjk9zOHbezpDNQEmc0PT64LF1oVmIM= NyCDwTra3LiUin05ZCGkdKLwReFC9L8Zf01ZfYabSfQ= EWwFTPmx7aajULFcEJRNd2R4xSXWY8CX1ynSe7WK0BCH42wf/REOS9l8Oiyjf587BhGa3y8jGKhUD7fXANDxcQ== 现在我成功地编写了一个litte测试程序来加密,解密,签名和validation数据。 最后我添加了一些测试代码: RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(512); rsa.FromXmlString(key); // key = string with XML above RSAParameters param = rsa.ExportParameters(true); BigInteger p = new BigInteger(param.P); BigInteger q = new BigInteger(param.Q); BigInteger n = new […]

“坏数据”CryptographicException

首先,我只是为了学术目的而编写了以下代码。 我说这个的原因是因为我没有将它放在生产环境中,因此我“绕过”了我需要做的一些开销,我只需要能够使用加密/解密字符串下面的代码。 我能够做到这一点,但由于某种原因,我开始收到“CryptographicException Bad Data”,并且不确定是什么原因造成了这个问题。 private string RSAEncrypt(string value) { byte[] encryptedData = Encoding.Unicode.GetBytes(value); CspParameters cspParams = new CspParameters(); cspParams.KeyContainerName = _rsaContainerName; using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(2048,cspParams)) { encryptedData = RSA.Encrypt(encryptedData, false); return Convert.ToBase64String(encryptedData); } } private string RSADecrypt(string value) { byte[] encryptedData = Encoding.Unicode.GetBytes(value); CspParameters cspParams = new CspParameters(); cspParams.KeyContainerName = _rsaContainerName; using […]

使用AES和rsaEncryption(PKCS#1 v1.5填充而不是v2(OAEP)填充)的EnvelopedCMS可能吗?

我一直在使用.NET进行加密。 到目前为止,我将3DES(Oid 1.2.840.113549.3.7)与rsaEncryption(Oid 1.2.840.113549.1.1.1,RSAES-PKCS1-v1_5)结合使用。 虽然第一个现在已被AES取代(Oid 2.16.840.1.101.3.4.1.42),但我仍然必须使用rsaEncryption / RSAES-PKCS1-v1_5 ,而不是RSAES-OAEP 。 如果我只是将另一个参数传递给我正在调用的EnvelopedCMS构造函数,我可以从3DES切换到AES: ContentInfo plainContent = new ContentInfo(new Oid(“1.2.840.113549.1.7.1”), data); EnvelopedCms encryptedMessage = new EnvelopedCms(plainContent); // using 3DES // EnvelopedCms encryptedMessage = new EnvelopedCms(plainContent, new AlgorithmIdentifier(new Oid(“2.16.840.1.101.3.4.1.42”))); // for AES (id-aes256-CBC) CmsRecipient recipient = new CmsRecipient(cert); encryptedMessage.Encrypt(recipient); byte[] encryptedBytes = encryptedMessage.Encode(); 到目前为止这很好。 不幸的是,有些收件人无法解密我的邮件,尽管他们能够解密AES。 查看ASN.1结构告诉我,不仅3DES改为AES,而且rsaEncryption(1.2.840.113549.1.1.1)被RSAES-OAEP (1.2.840.113549.1.1.7)取代。 我可以以某种方式强迫仍然使用EnvelopedCMS的RSAES-PKCS1-v1_5吗? 或者您是否在切换3DES-> […]

使用RsaProtectedConfigurationProvider加密/解密app.config部分

在我们的程序安装过程中,我们运行此方法来加密app.config的各个部分: // Get the application configuration file. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); // Define the Rsa provider name. const string provider = “RsaProtectedConfigurationProvider”; // Get the section to protect. ConfigurationSection connStrings = config.ConnectionStrings; if (connStrings != null) { if (!connStrings.SectionInformation.IsProtected) { if (!connStrings.ElementInformation.IsLocked) { // Protect the section. connStrings.SectionInformation.ProtectSection(provider); connStrings.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Full); } } } […]

使用RSA密钥保护许可证密钥

已经晚了,我累了,而且可能很密集…… 我编写了一个我需要保护的应用程序,因此它只能在我生成密钥的机器上运行。 我现在正在做的是获取BIOS序列号并从中生成哈希值,然后我使用XML RSA私钥对其进行加密。 然后我签署XML以确保它不被篡改。 我试图打包公钥来解密和validation签名,但每次我尝试执行代码作为不同的用户而不是生成签名的用户我在签名上失败。 我的大多数代码都是根据我发现的示例代码进行修改的,因为我对RSA加密并不像我想要的那样熟悉。 下面是我正在使用的代码以及我认为需要使用的代码才能使其正常工作… 任何反馈都会非常感激,因为我现在很遗憾我正在处理的原始代码就是这样,只要用户启动程序与最初签署文档的程序相同,此代码就可以正常工作… CspParameters cspParams = new CspParameters(); cspParams.KeyContainerName = “XML_DSIG_RSA_KEY”; cspParams.Flags = CspProviderFlags.UseMachineKeyStore; // Create a new RSA signing key and save it in the container. RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(cspParams) { PersistKeyInCsp = true, }; 这段代码是我认为我应该做的,但无论我做什么都无法validation签名,无论它是同一个用户还是另一个用户…… RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(); //Load the private key from xml […]

使用带有RSA C#的非对称算法私钥和公钥

我有两个包含RSA Private和RSA公钥的AsymmetricAlgorithm对象。 从Windows-MY密钥库中检索私钥,从用户证书中检索公钥。 如何使用这些密钥和RSACryptoServiceProvider在C#中使用RSA算法加密数据? 换句话说,我如何指定我想使用已有的密钥?

RSA:在javascript中加密密码但在C#中无法解密

我想在我的项目中应用RSA加密,但遇到了一些麻烦: 首先,我从http://www.ohdave.com/rsa/下载JavaScripts库,并添加对我的项目的引用; 其次,我已经定义了RSA对象和代码来初始化: internal RSACryptoServiceProvider Rsa { get { if (HttpContext.Cache[“Rsa”] != null) { RSACryptoServiceProvider encryptKeys = (RSACryptoServiceProvider)HttpContext.Cache[“Rsa”]; return encryptKeys; } else { return new RSACryptoServiceProvider(1024); } } set { HttpContext.Cache.Remove(“Rsa”); HttpContext.Cache.Insert(“Rsa”, value); } } public ActionResult SignUp() { this.Rsa = Security.GetRsa(); RSAParameters param= this.Rsa.ExportParameters(true); //this will bind to view TempData[“exponent”] = Util.BytesToHexString(param.Exponent); TempData[“key”] = […]

C#中的大数据RSA加密

这是我的第一篇文章,所以希望我没有错过任何重要的内容。 我在C#中做一个项目,我需要使用公钥/私钥加密来加密消息,然后通过SSL连接发送它。 根据文档,我选择使用RSACryptoService ,这是用于加密数据的唯一非对称加密方案。 问题是我遇到了很多问题。 (我想做对称加密,但这不是我老师要我做的事情,根据他的说法,确定块大小应该很容易,然后它应该为你完成所有的工作。)好吧,到目前为止没有运气,我尝试了一些不同的方法,但现在我又回到了基础并再次尝试,这是我目前的代码: public string[] GenerateKeysToStrings(string uniqueIdentifier) { string[] keys; using (var rsa = new RSACryptoServiceProvider(4096)) { try { string privateKey = rsa.ToXmlString(true); string publicKey = rsa.ToXmlString(false); this.pki.StoreKey(publicKey, uniqueIdentifier); keys = new string[2]; keys[0] = privateKey; keys[1] = publicKey; } finally { //// Clear the RSA key container, deleting generated keys. rsa.PersistKeyInCsp […]