使用解密密钥解密数据

时间:2020-12-11 18:23:17

I am trying to decrypt the data using private key. I was able to decrypt the key using RSA and private key. Now I would like to decrypt the data using the decrypted key. The data was encrypted the values using AES and random session secret using PHP. Could you please let me know if there are any examples?

我试图使用私钥解密数据。我能够使用RSA和私钥解密密钥。现在我想使用解密密钥解密数据。使用AES使用AES和随机会话密钥对数据进行加密。如果有任何例子,请告诉我吗?

Here is the code I have so far.

这是我到目前为止的代码。

    static void Main(string[] args)
    {
           AsymmetricCipherKeyPair keyPair;

        string protectedSecret = "U6XksFkhWV4.......eo3fRg==";
        var decodedSecret = Convert.FromBase64String(protectedSecret);

        string iv = "KLnP....wA==";
        var decodedIV = Convert.FromBase64String(iv);

        using (var reader = File.OpenText(@"c:\\private.key")) 
            keyPair = (AsymmetricCipherKeyPair)new PemReader(reader).ReadObject();

        var decryptPKIEngine = new Pkcs1Encoding(new RsaEngine());
        decryptPKIEngine.Init(false, keyPair.Private);

        var secret = Encoding.UTF8.GetString(decryptPKIEngine.ProcessBlock(decodedSecret, 0, decodedSecret.Length));

        var  protectedData = Convert.FromBase64String("f8..Po=");
      }

2 个解决方案

#1


1  

Create a RijndaelManaged instance and set its Key and IV to your byte arrays.

创建一个RijndaelManaged实例并将其Key和IV设置为您的字节数组。

Then, create a CryptoStream from CreateDecryptor() wrapping a MemoryStream with your ciphertext byte array.
Finally, read the plaintext from the CryptoStream. (if it's actual text, you may want to use a StreamReader)

然后,从CreateDecryptor()创建一个CryptoStream,用您的密文字节数组包装一个MemoryStream。最后,从CryptoStream中读取明文。 (如果是实际文本,您可能想要使用StreamReader)

#2


0  

Try this replace put the necessary strings where needed

试试这个替换在必要时放置必要的字符串

static string PHPDecrypt() 
    {            
      byte[] keyBytes = Convert.FromBase64String("U6XksFkhWV4.......eo3fRg=="); //put in your real values here and below for iv and cipherTextBytes
      byte[] iv = Convert.FromBase64String("KLnP....wA=="");
      byte[] cipherTextBytes = Convert.FromBase64String("Put the EncryptedText here");

      var symmetricKey = new RijndaelManaged 
      { 
         Mode = CipherMode.CBC, 
         IV = iv, KeySize = 256, 
         Key = keyBytes, 
         Padding = PaddingMode.Zeros
      };

      using (var decryptor = symmetricKey.CreateDecryptor())
      using (var ms = new MemoryStream(cipherTextBytes))
      using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read)) {
        var plainTextBytes = new byte[cipherTextBytes.Length];
        int decryptedByteCount = cs.Read(plainTextBytes, 0, plainTextBytes.Length);
        return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
      }
    }

#1


1  

Create a RijndaelManaged instance and set its Key and IV to your byte arrays.

创建一个RijndaelManaged实例并将其Key和IV设置为您的字节数组。

Then, create a CryptoStream from CreateDecryptor() wrapping a MemoryStream with your ciphertext byte array.
Finally, read the plaintext from the CryptoStream. (if it's actual text, you may want to use a StreamReader)

然后,从CreateDecryptor()创建一个CryptoStream,用您的密文字节数组包装一个MemoryStream。最后,从CryptoStream中读取明文。 (如果是实际文本,您可能想要使用StreamReader)

#2


0  

Try this replace put the necessary strings where needed

试试这个替换在必要时放置必要的字符串

static string PHPDecrypt() 
    {            
      byte[] keyBytes = Convert.FromBase64String("U6XksFkhWV4.......eo3fRg=="); //put in your real values here and below for iv and cipherTextBytes
      byte[] iv = Convert.FromBase64String("KLnP....wA=="");
      byte[] cipherTextBytes = Convert.FromBase64String("Put the EncryptedText here");

      var symmetricKey = new RijndaelManaged 
      { 
         Mode = CipherMode.CBC, 
         IV = iv, KeySize = 256, 
         Key = keyBytes, 
         Padding = PaddingMode.Zeros
      };

      using (var decryptor = symmetricKey.CreateDecryptor())
      using (var ms = new MemoryStream(cipherTextBytes))
      using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read)) {
        var plainTextBytes = new byte[cipherTextBytes.Length];
        int decryptedByteCount = cs.Read(plainTextBytes, 0, plainTextBytes.Length);
        return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
      }
    }