English 中文(简体)
利用加密的关键对数据进行加密
原标题:Decrypt the data using the decrypted key

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?

这里是我迄今为止制定的准则。

    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=");
      }
问题回答

创建<代码>RijndaelManaged. 例,并将其KeyIV贴在您的外围阵列上。

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)

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签