'ASN1 corrupted data.' 替换 public 密钥而非私钥时出错
'ASN1 corrupted data.' error when replacing public key but not private key
了解使用 RSA 签名的机制,我有一段代码可以在下面运行。
var privateRSAKey = File.ReadAllText("RSAPrivateKey.txt").Trim();
Regex privateRSAKeyRegex = new Regex(@"-----(BEGIN|END) RSA PRIVATE KEY-----[\W]*");
privateRSAKey = privateRSAKeyRegex.Replace(privateRSAKey, "");
//byte[602]
byte[] rsaPrivateKeyBytes = Convert.FromBase64String(privateRSAKey);
RSA rsa = RSA.Create();
rsa.ImportRSAPrivateKey(new ReadOnlySpan<byte>(rsaPrivateKeyBytes), out _);
但是类似的块不能用于替换另一个 rsa 对象上的 public 密钥。
publicRSAKey = File.ReadAllText("RSAPublicKey.txt").Trim();
Regex publicRSAKeyRegex = new Regex(@"-----(BEGIN|END) PUBLIC KEY-----[\W]*");
publicRSAKey = publicRSAKeyRegex.Replace(publicRSAKey, "");
//byte[162]
byte[] rsaPublicKeyBytes = Convert.FromBase64String(publicRSAKey);
RSA recipientRSA = RSA.Create();
recipientRSA.ImportRSAPublicKey(new ReadOnlySpan<byte>(rsaPublicKeyBytes), out _);
我只想用字符串文件替换 public rsa 密钥,但出现错误
An unhandled exception of type 'System.Security.Cryptography.CryptographicException' occurred in System.Security.Cryptography.Algorithms.dll
ASN1 corrupted data.
我在发帖后从字面上找到了这个
https://vcsjones.dev/key-formats-dotnet-3/
To summarize each PEM label and API pairing:
“BEGIN RSA PRIVATE KEY” => RSA.ImportRSAPrivateKey
“BEGIN PRIVATE KEY” => RSA.ImportPkcs8PrivateKey
“BEGIN ENCRYPTED PRIVATE KEY” => RSA.ImportEncryptedPkcs8PrivateKey
“BEGIN RSA PUBLIC KEY” => RSA.ImportRSAPublicKey
“BEGIN PUBLIC KEY” => RSA.ImportSubjectPublicKeyInfo
我的问题是我的密钥格式为 -----BEGIN PUBLIC KEY-----
我正在使用 ImportRSAPublicKey
.
我切换到.ImportSubjectPublicKeyInfo
,一切正常
了解使用 RSA 签名的机制,我有一段代码可以在下面运行。
var privateRSAKey = File.ReadAllText("RSAPrivateKey.txt").Trim();
Regex privateRSAKeyRegex = new Regex(@"-----(BEGIN|END) RSA PRIVATE KEY-----[\W]*");
privateRSAKey = privateRSAKeyRegex.Replace(privateRSAKey, "");
//byte[602]
byte[] rsaPrivateKeyBytes = Convert.FromBase64String(privateRSAKey);
RSA rsa = RSA.Create();
rsa.ImportRSAPrivateKey(new ReadOnlySpan<byte>(rsaPrivateKeyBytes), out _);
但是类似的块不能用于替换另一个 rsa 对象上的 public 密钥。
publicRSAKey = File.ReadAllText("RSAPublicKey.txt").Trim();
Regex publicRSAKeyRegex = new Regex(@"-----(BEGIN|END) PUBLIC KEY-----[\W]*");
publicRSAKey = publicRSAKeyRegex.Replace(publicRSAKey, "");
//byte[162]
byte[] rsaPublicKeyBytes = Convert.FromBase64String(publicRSAKey);
RSA recipientRSA = RSA.Create();
recipientRSA.ImportRSAPublicKey(new ReadOnlySpan<byte>(rsaPublicKeyBytes), out _);
我只想用字符串文件替换 public rsa 密钥,但出现错误
An unhandled exception of type 'System.Security.Cryptography.CryptographicException' occurred in System.Security.Cryptography.Algorithms.dll
ASN1 corrupted data.
我在发帖后从字面上找到了这个 https://vcsjones.dev/key-formats-dotnet-3/
To summarize each PEM label and API pairing:
“BEGIN RSA PRIVATE KEY” => RSA.ImportRSAPrivateKey
“BEGIN PRIVATE KEY” => RSA.ImportPkcs8PrivateKey
“BEGIN ENCRYPTED PRIVATE KEY” => RSA.ImportEncryptedPkcs8PrivateKey
“BEGIN RSA PUBLIC KEY” => RSA.ImportRSAPublicKey
“BEGIN PUBLIC KEY” => RSA.ImportSubjectPublicKeyInfo
我的问题是我的密钥格式为 -----BEGIN PUBLIC KEY-----
我正在使用 ImportRSAPublicKey
.
我切换到.ImportSubjectPublicKeyInfo
,一切正常