小编Mon*_*iro的帖子

RSACryptoServiceProvider 使用自己的公钥和私钥加密和解密

有人告诉我,对于非对称加密,你用你的公钥加密明文,然后用你的私钥解密。所以我尝试了以下方法:

    static void Main(string[] args)
    {
        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
        string pubkey = rsa.ToXmlString(false);
        string prikey = rsa.ToXmlString(true);

        byte[] someThing = RSAEncrypt(Encoding.Unicode.GetBytes("Hello World"), pubkey);
        byte[] anotherThing = RSADecrypt(someThing, prikey);

        Console.WriteLine(Convert.ToBase64String(anotherThing));
    }
Run Code Online (Sandbox Code Playgroud)

以及加密和解密功能

    public static byte[] RSAEncrypt(byte[] plaintext, string destKey)
    {
        byte[] encryptedData;
        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
        rsa.FromXmlString(destKey);
        encryptedData = rsa.Encrypt(plaintext, true);
        rsa.Dispose();
        return encryptedData;
    }

    public static byte[] RSADecrypt(byte[] ciphertext, string srcKey)
    {
        byte[] decryptedData;
        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
        rsa.FromXmlString(srcKey);
        decryptedData = rsa.Decrypt(ciphertext, true);
        rsa.Dispose();
        return decryptedData; …
Run Code Online (Sandbox Code Playgroud)

c# encryption cryptography rsa

6
推荐指数
1
解决办法
1万
查看次数

标签 统计

c# ×1

cryptography ×1

encryption ×1

rsa ×1