为什么65537不使用CryptoPP将base64URL编码为"AQAB"?

Pat*_*ell 4 c++ base64 encoding rsa crypto++

我正在使用CryptoPP生成RSA密钥对,以允许对游戏服务器进行身份验证.我需要base64URL编码我的公共指数和模数以包含在JWK中但是我遇到了一些问题.代码显示了我如何生成RSA密钥,提取指数并对其进行编码:

typedef InvertibleRSAFunction RSAPrivateKey;
typedef RSAFunction RSAPublicKey;

RSAPrivateKey privateKey;
privateKey.Initialize( rng, 1024);

RSAPublicKey publicKey( privateKey );

const Integer& e = privateKey.GetPublicExponent();

Base64Encoder exponentSink(new StringSink(exponentString));
e.DEREncode(exponentSink);
exponentSink.MessageEnd();
base64URL(exponentString);

cout << "exponentString: " << exponentString << endl;
Run Code Online (Sandbox Code Playgroud)

base64URL函数只过滤=,+,\n和/字符的字符串,使其成为base64URL.

我知道CryptoPP使用的指数为17,上面的代码将其编码为"AgER".我从众多来源中读到65537编码为"AQAB",我通过手动设置e来尝试将其作为测试.当我这样做时,输出是"AgMBAAE",而不是"AQAB".

当我使用https://www.base64encode.org/等在线转换器时,输出是"NjU1Mzc".

有人可以解释所有这些差异来自何处以及17的正确编码是什么?谢谢!

Maa*_*wes 6

CryptoPP的输出似乎包括ASN.1 DER编码表示.在十六进制中,字符串AgMBAAE转换为0203010001.

现在在ASN.1/DER中,它读作:

    02 a signed INTEGER
    03 the length of the value
010001 the value, a big endian signed integer (i.e. 65537)
Run Code Online (Sandbox Code Playgroud)

base64encode.org的值似乎输出ASCII字符串的base 64编码"65537":3635353337十六进制.


值17没有单一的正确编码,它取决于您使用它的内容.

  • 作为单字节值,它将是 EQ==
  • 作为ASN.1/DER编码的整数,它将是 AgER
  • 作为字符串它将是 MTc=

你当然可以使用相同的字符串而不=填充字符(符合base64url编码而不是更常见的base 64编码).