为什么 RSA 密钥看起来像它们那样?

问题描述 投票:0回答:1

我试图更好地了解 RSA 加密,所以我让我的程序显示它生成的密钥,但根据我对 RSA 加密的理解,密钥应该只是数字,但我的程序有一长串数字、字符、和字母。

这是我的代码:

using System;
using System.Security.Cryptography;
using System.Text;

class Program
{
    static void Main()
    {
        // Get user input for plaintext
        Console.WriteLine("Enter the plaintext:");
        string plaintext = Console.ReadLine();

        // Generate RSA key pair (for simplicity, we use a fixed key size of 2048 bits)
        using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(2048))
        {
            // Display the public key
            Console.WriteLine("Public Key:");
            Console.WriteLine(rsa.ToXmlString(false));

            // Encrypt the plaintext
            byte[] plaintextBytes = Encoding.UTF8.GetBytes(plaintext);
            byte[] encryptedBytes = rsa.Encrypt(plaintextBytes, fOAEP: false);

            // Display the encrypted string
            string encryptedString = Convert.ToBase64String(encryptedBytes);
            Console.WriteLine("Encrypted Text:");
            Console.WriteLine(encryptedString);
        }

        Console.ReadKey();
    }
}

当我的程序显示公钥时,它看起来像这样

<RSAKeyValue>
<Modulus>m/DXR9Ld2vAAIS3sATZGx2z4lsZeeImu7qQzAW6j+EGIcU6ToGBOVK3kCYcbv+o884HT/hDj9M/FV9Tc/apaFREqucSw973pJyNXnp2bvO8DwO9XohBPX5lWognAWr4KDybMn9oBuqR4fAYck2ym1uYtXxGetSgf3+qgX9RcbZGi7ifafJyw1hGNLzpA6d5pZkyXUvrpDz4YLT5vbJ8xoFEFfxYV6zA5EZcEi/9w8IJaU/ypFpdmZhMtmDSXKSBJ9MVVridnAjwahwWuZNCp9rRsbsTiGEvPPvbnW1WXrbOxT41IrDuGr/gNX2GEkAv+SNVXUN8z2LcHtIHRTj7/GQ==</Modulus>
<Exponent>AQAB</Exponent>
</RSAKeyValue>

如果密钥纯粹是用数学和数字生成的,我不明白密钥看起来如何或为何如此?我唯一的想法就是这条线 Console.WriteLine(rsa.ToXmlString(false)); 但这行到底是什么意思?

c# encryption rsa public-key-encryption
1个回答
1
投票

如文档所述

RSA.ToXmlString
返回密钥对的 XML 表示形式(默认情况下仅包含公共部分,或者同时包含公共部分和私有部分)。

看似神秘的文本是Base64编码的二进制数据;

AQAB
是字节“01 00 01”的编码,反过来,当解释为大端整数时,对指数 65537 进行编码:

$ python3
>>> import base64
>>> int.from_bytes(base64.b64decode(b"AQAB"), "big")
65537

同样的想法代表

Modulus
,这是一个相当大的数字(十进制形式有616位长)。

至于这些数字的含义和使用方法,你必须参考维基百科

© www.soinside.com 2019 - 2024. All rights reserved.