簡體   English   中英

嘗試使用 .net 中的 bouncycastle 使用公鑰(非私鑰)解密字符串

[英]Trying to decrypt a string with public key(not private) using bouncycastle in .net

作為示例應用程序,我使用 .NET 中的 BouncyCastle 庫使用 rsa 公鑰(由第 3 方 api 提供)加密了一個字符串。 當我將此加密字符串發送到上述第 3 方 api 端點時,它能夠對其進行解密(使用其私鑰)。 我沒有他們的私鑰,但是,是否可以僅使用我擁有的公鑰來解密我的字符串?

根據我對 RSA 公鑰/私鑰對的理解,在解密時,您使用與您一起存儲的私鑰來解密字符串,並使用公鑰來確認您正在從所述來源接收數據。

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);
            }

            var encrypted = Convert.ToBase64String(encryptEngine.ProcessBlock(bytesToEncrypt, 0, bytesToEncrypt.Length));
            return encrypted;

        }

public string RsaDecrypt(string base64Input
            , string privateKey)
        {
            var bytesToDecrypt = Convert.FromBase64String(base64Input);

            //get a stream from the string
            AsymmetricCipherKeyPair keyPair;
            var decryptEngine = new Pkcs1Encoding(new RsaEngine());

            using (var txtreader = new StringReader(privateKey))
            {
                keyPair = (AsymmetricCipherKeyPair)new PemReader(txtreader).ReadObject();

                //decryptEngine.Init(false, keyPair.Private);
                decryptEngine.Init(false, keyPair.Public);
            }

            var decrypted = Encoding.UTF8.GetString(decryptEngine.ProcessBlock(bytesToDecrypt, 0, bytesToDecrypt.Length));
            return decrypted;
        }


static void Main(string[] args)
        {
string _creditCardNumber = "5454545454545454";
string publicKey = System.IO.File.ReadAllText(@"C:\ThirdPartyKeys\RSAPublicKey_01.txt");

            var enc = new EncryptionClass();
            var encryptedWithPublic = enc.RsaEncryptWithPublic(_creditCardNumber, publicKey);
            Console.WriteLine("String: " + _creditCardNumber);
            Console.WriteLine("Encrypted String: " + encryptedWithPublic);

// Decrypt
            var outputWithPublic = enc.RsaDecrypt(encryptedWithPublic, publicKey);            

            //var outputWithPrivate = enc.RsaDecrypt(encryptedWithPrivate, _privateKey);

            Console.WriteLine("Decrypted String: " + outputWithPublic);
}

加密有效,但是當我嘗試使用相同的公鑰解密時,它會抱怨

Invalid Cast Exception:
Unable to cast object of type 'Org.BouncyCastle.Crypto.Parameters.RsaKeyParameters' to type 'Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair'.

at line in RsaDecrypt function:
keyPair = (AsymmetricCipherKeyPair)new PemReader(txtreader).ReadObject();

不。它是非對稱加密,這意味着您無法使用公鑰對其進行解密。 如果可以,它會破壞目的,任何擁有公鑰的人都可以解密您的秘密消息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM