无法使用.pem

时间:2016-03-18 04:12:39

标签: c# encryption pem

我想将解密后的字符串转换为数据字符串(我可以读取属性)

我尝试了here

中的代码
string token ="eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJpc3N1ZXIiLCJzdWIiOiJzdWJqZWN0IiwiYXVkIjoiYXVkaWVuY2UiLCJleHAiOjE0NjAwMDIxNzEsIm5iZiI6MTQ1ODI3Mzg3MSwiaWRnSWQiOiIwMDAxNzI4MTcwIiwic2Vjb25kRmEiOiJmYWxzZSIsInJvbGVzIjoiW09OTElORV9UUkFESU5HLCBST0xFX09OTElORV9UUkFESU5HLCBPTkxJTkVfVklFV19BQ0NPVU5UX0lORk8sIFJPTEVfT05MSU5FX1ZJRVdfQUNDT1VOVF9JTkZPXSIsImN1c3RvbWVySWQiOiIwMDAxNzI4MTcwIiwidnRvc0F1dGhlbnRpY2F0ZWQiOiJmYWxzZSIsInVzZXJJZCI6IjUyNzY5IiwiY3VzdG9tZXJOYW1lIjoiTmd1eWVuIFZhbiBNIiwiZW1haWwiOiJtaW5odHE5MEBnbWFpbC5jb20iLCJ1c2VybmFtZSI6InRlc3QyMiIsInN0YXR1cyI6Ik9OTElORV9BQ1RJVkUifQ.CfcVsKidaHi6QEneYDfox9509SnIQUQZS6OUWf3jBN4OnTEQ-xFeHeIuQP6qgEK3b_0fLjWZ4OT7qnItLMKYg0vd68OxjDbHw79C4jQNnxq1EUgsEs72-gNNBV42n6I0R5VZy4t6AuKgHTFp6gN4fTisfA7vnX2Vq5Sg2faAoMl2nevN7F1P9YEWFPea9qoDqmPwGY72DvEauZNZmIb7OvtRvoBFAK_cIJvuRShIzUfQ5q4W4sMiRH92Ou_6S7k4NfqO8kOdoJkSqZCDlhJIdAmyW4IBsYc5TvgeoGIrzgKCVr2_1wM0RHXE3vq6oobe2rB68Rc8eQ0VYzH6fH4KQA";
var bytesToDecrypt = Convert.FromBase64String(token); // string to decrypt, base64 encoded

AsymmetricCipherKeyPair keyPair; 

using (var reader = File.OpenText(@"c:\myprivatekey.pem")) // file containing RSA PKCS1 private key
    keyPair = (AsymmetricCipherKeyPair) new PemReader(reader).ReadObject(); 

var decryptEngine = new Pkcs1Encoding(new RsaEngine());
decryptEngine.Init(false, keyPair.Private); 

var decrypted = Encoding.UTF8.GetString(decryptEngine.ProcessBlock(bytesToDecrypt, 0, bytesToDecrypt.Length));

然而,它在第二行抛出异常:

var bytesToDecrypt = Convert.FromBase64String(token); //exception

例外是:

The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.

如何解决?

1 个答案:

答案 0 :(得分:1)

我尝试了一些代码,这对我有用:

        string token = "eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJpc3N1ZXIiLCJzdWIiOiJzdWJqZWN0IiwiYXVkIjoiYXVkaWVuY2UiLCJleHAiOjE0NjAwMDIxNzEsIm5iZiI6MTQ1ODI3Mzg3MSwiaWRnSWQiOiIwMDAxNzI4MTcwIiwic2Vjb25kRmEiOiJmYWxzZSIsInJvbGVzIjoiW09OTElORV9UUkFESU5HLCBST0xFX09OTElORV9UUkFESU5HLCBPTkxJTkVfVklFV19BQ0NPVU5UX0lORk8sIFJPTEVfT05MSU5FX1ZJRVdfQUNDT1VOVF9JTkZPXSIsImN1c3RvbWVySWQiOiIwMDAxNzI4MTcwIiwidnRvc0F1dGhlbnRpY2F0ZWQiOiJmYWxzZSIsInVzZXJJZCI6IjUyNzY5IiwiY3VzdG9tZXJOYW1lIjoiTmd1eWVuIFZhbiBNIiwiZW1haWwiOiJtaW5odHE5MEBnbWFpbC5jb20iLCJ1c2VybmFtZSI6InRlc3QyMiIsInN0YXR1cyI6Ik9OTElORV9BQ1RJVkUifQ.CfcVsKidaHi6QEneYDfox9509SnIQUQZS6OUWf3jBN4OnTEQ-xFeHeIuQP6qgEK3b_0fLjWZ4OT7qnItLMKYg0vd68OxjDbHw79C4jQNnxq1EUgsEs72-gNNBV42n6I0R5VZy4t6AuKgHTFp6gN4fTisfA7vnX2Vq5Sg2faAoMl2nevN7F1P9YEWFPea9qoDqmPwGY72DvEauZNZmIb7OvtRvoBFAK_cIJvuRShIzUfQ5q4W4sMiRH92Ou_6S7k4NfqO8kOdoJkSqZCDlhJIdAmyW4IBsYc5TvgeoGIrzgKCVr2_1wM0RHXE3vq6oobe2rB68Rc8eQ0VYzH6fH4KQA";
        string converted = token.Replace("-", "");
        converted = converted.Replace("_", "");
        converted = converted.Replace(".", "");
        int mod4 = converted.Length % 4;
        if (mod4 > 0)
        {
            converted += new string('=', 4 - mod4);
        }
        var bytesToDecrypt = Convert.FromBase64String(converted);

你得到的例外可能是因为有些像(-_.)这些字符在基本64字符串中无效而另一个字符串是你的字符串长度也应该是正确的长度。有关更多信息,您可以看到 - WikipediaMSDN