公共和私钥作为变量C#

发布于 2025-02-07 14:37:07 字数 2886 浏览 2 评论 0原文

我正在编写一个功能,其中此功能将我的公钥作为变量接收到我的公钥,而该变量的值实际上是公共密钥。我需要Azure中的两个不同功能应用程序来加密和解密。键必须匹配,但是问题是,每次我称为API时,公共密钥都不同,我可以加密无问题。但是,当我必须解密时,它行不通。我无法将相同的密钥对用于这些功能。这就是为什么我试图将我以前生成的键作为变量的原因。

示例: 字符串publicKey =“ mmmfisidudhfhhsanbgkqhkig9w0baqefaocaq8amiibcgkcaqeai7zoktc55v9njuhqfr583bcfkcfkcfkcjflxnvmqfmqfmqc5/3b7t7v ... >

钥匙正在生成使用弹性城堡。

rsakeypairgenerator g = new rsakeypairgenerator(); G.Init(新的KeyGenerationParameters(new Securerandom(),2048)); AnmymetricCiphePhePhePair Keypair = G.GenerateKeypair();

它正常使用以下代码:

        string plainText = "test data here";
        byte[] plainTextToByte = Encoding.UTF8.GetBytes(plainText);

        //Generating Key Pair
        RsaKeyPairGenerator g = new RsaKeyPairGenerator();
        g.Init(new KeyGenerationParameters(new SecureRandom(), 2048));
        AsymmetricCipherKeyPair keyPair = g.GenerateKeyPair();

        //Extracting the private key from pair
        RsaKeyParameters privateKey = (RsaKeyParameters)keyPair.Private;
        RsaKeyParameters publicKey = (RsaKeyParameters)keyPair.Public;

        //Encryption proccess
        IAsymmetricBlockCipher cipher = new OaepEncoding(new RsaEngine());
        cipher.Init(true, publicKey);
        byte[] cipherText = cipher.ProcessBlock(plainTextToByte, 0, plainTextToByte.Length);
        string encryptedText = Encoding.UTF8.GetString(cipherText);
        Console.WriteLine(encryptedText);

        //Decryption Process
        cipher.Init(false, privateKey);
        byte[] decryptedText = cipher.ProcessBlock(cipherText, 0 , cipherText.Length);
        string decryptedTextToString = Encoding.UTF8.GetString(decryptedText);

        Console.WriteLine(decryptedTextToString);
        Console.ReadLine();`

我需要上面生成的键作为在控制台应用程序中的函数中使用的变量。

但是,当我尝试将密钥作为变量传递时,我会在下面获取错误:

https:// i。 sstatic.net/vlsol.png

我可以使用C#中的核心类执行相同的过程,它与下面的代码相似:

c#rsa加密/解密,带有传输

我遵循上面示例的逻辑相同,现在对我不起作用。我是所有这一切的初学者。 有没有办法做到这一点?

这是我使用的件代码来在屏幕截图上获取错误。这些密钥是由我在原始帖子上发布的代码生成的。

    string plainText = "test here";
    byte[] plainTextToByte = Encoding.UTF8.GetBytes(plainText);

    string publicKey = "MIIBIjANBgk...DAQAB";

    IAsymmetricBlockCipher cipher = new OaepEncoding(new RsaEngine());
    cipher.Init(true, publicKey);
    byte[] cipherText = cipher.ProcessBlock(plainTextToByte, 0, plainTextToByte.Length);
    string encryptedText = Encoding.UTF8.GetString(cipherText);
    Console.WriteLine(encryptedText);

    return new OkObjectResult(encryptedText);`

att。

I'm writing a function where this function is receiving my public key as variable, and the value for this variable is the actually public key. I need two different functions apps in Azure, to encrypt and decrypt. The keys must match, but the problem is, every time I call the API the public key is different, I can encrypt without problems. But when I have to decrypt it doesn't work. I am not able to use the same key pairs for these functions. Thats why Im trying to use the keys I generated before as variables.

Example:
string publicKey = "MMMFisIDUDHfhHSANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAi7ZOKtc55v9NJuhQFR583BcFkcjflXNVMqC5/3b7t7v..."

This is the method I'm using to encrypt:

cipher.Init(true, publicKey);

My keys are being generated using Bouncy Castle.

RsaKeyPairGenerator g = new RsaKeyPairGenerator();
g.Init(new KeyGenerationParameters(new SecureRandom(), 2048));
AsymmetricCipherKeyPair keyPair = g.GenerateKeyPair();

It worked normally with the code below:

        string plainText = "test data here";
        byte[] plainTextToByte = Encoding.UTF8.GetBytes(plainText);

        //Generating Key Pair
        RsaKeyPairGenerator g = new RsaKeyPairGenerator();
        g.Init(new KeyGenerationParameters(new SecureRandom(), 2048));
        AsymmetricCipherKeyPair keyPair = g.GenerateKeyPair();

        //Extracting the private key from pair
        RsaKeyParameters privateKey = (RsaKeyParameters)keyPair.Private;
        RsaKeyParameters publicKey = (RsaKeyParameters)keyPair.Public;

        //Encryption proccess
        IAsymmetricBlockCipher cipher = new OaepEncoding(new RsaEngine());
        cipher.Init(true, publicKey);
        byte[] cipherText = cipher.ProcessBlock(plainTextToByte, 0, plainTextToByte.Length);
        string encryptedText = Encoding.UTF8.GetString(cipherText);
        Console.WriteLine(encryptedText);

        //Decryption Process
        cipher.Init(false, privateKey);
        byte[] decryptedText = cipher.ProcessBlock(cipherText, 0 , cipherText.Length);
        string decryptedTextToString = Encoding.UTF8.GetString(decryptedText);

        Console.WriteLine(decryptedTextToString);
        Console.ReadLine();`

I need the keys generated above as a variable to use in a function inside a console app.

But when I try pass the key as variable, I'm getting the error below:

https://i.sstatic.net/vLSOL.png

I could do same procedure using core classes from C#, it was similar with the code below:

C# RSA encryption/decryption with transmission

The same logic I follow for the example above is not working for me now. I am beginner into all this.
Is there a way to do that?

This is the piece code I'm using to get the error on the screenshot. The keys were generated with the code I posted on the original post.

    string plainText = "test here";
    byte[] plainTextToByte = Encoding.UTF8.GetBytes(plainText);

    string publicKey = "MIIBIjANBgk...DAQAB";

    IAsymmetricBlockCipher cipher = new OaepEncoding(new RsaEngine());
    cipher.Init(true, publicKey);
    byte[] cipherText = cipher.ProcessBlock(plainTextToByte, 0, plainTextToByte.Length);
    string encryptedText = Encoding.UTF8.GetString(cipherText);
    Console.WriteLine(encryptedText);

    return new OkObjectResult(encryptedText);`

Att.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪失控 2025-02-14 14:37:07

我不太清楚问题是什么。但是,根据问题中发布的最后一段片段,您正在尝试导入公共密钥。根据您的倒数第二条评论,它是X.509/SPKI格式中用pemwriter导出的PEM编码的公钥:

-----BEGIN PUBLIC KEY-----
MIIB...
...AQAB
-----END PUBLIC KEY-----

可以在cipher#init()中导入并使用此键。如下(令publicKeypem是导出的PEM密钥):

using Org.BouncyCastle.OpenSsl;
...
PemReader pemReader = new PemReader(new StringReader(publicKeyPem));
RsaKeyParameters publicKeyReloaded = (RsaKeyParameters)pemReader.ReadObject();
...
cipher.Init(true, publicKeyReloaded); 

I'm not quite clear what the problem is. But based on the last snippet posted in the question, you are trying to import a public key. And according to your penultimate comment, it is a PEM encoded public key in X.509/SPKI format exported with a PemWriter:

-----BEGIN PUBLIC KEY-----
MIIB...
...AQAB
-----END PUBLIC KEY-----

Such a key can be imported and used in Cipher#Init() as follows (let publicKeyPem be the exported PEM key):

using Org.BouncyCastle.OpenSsl;
...
PemReader pemReader = new PemReader(new StringReader(publicKeyPem));
RsaKeyParameters publicKeyReloaded = (RsaKeyParameters)pemReader.ReadObject();
...
cipher.Init(true, publicKeyReloaded); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文