输入不是有效的Base-64字符串,因为它在使用X509Certificate证书时包含非基本64字符

时间:2018-04-08 18:20:24

标签: c# wcf encryption soap base64

我从X509Certificate获取私钥但是当我调用Convert.FromBase64String方法时,我收到一条错误消息:

  

输入不是有效的Base-64字符串,因为它包含非基数64   字符,两个以上的填充字符或非法字符   在填充字符中。

这是加密密钥

  

j0ZKFBmTz3Kj0cQ82rq63MYAR + 0 = C + WfKnOxqmDVC6uhsFPQjAVcjsAcD5 + QkpoGiNN3UcGmPIf6V / JiahTY + 6ZXir + Mnw0d2Fe805vJ3i8p2itsbaPWiWKU56WeI6b5odIm / HfKTWibM1JYQ9MMAKv2nym0stjyeSieQwwR2vBXvwY2e0hNb88qy22 / cOKpjATKgM9tg755hdl5Po24ARFwjnTj12jFANepzmpDcEvlF7LPmN5s2ZzDGC9h3KVJQ8WhB53yIDeQLXdmB7DtnIwF0ocw5idSkOGSMZTdAumIUzerFvQ1Qw02KcPb5nyqxZkvc1hXiqVFqe6SBRRuokFlm7VwfaT2hbPl + GX02LSZXKn3qg ==

这是我的代码

var encryptedKey = doc.GetElementsByTagName("e:EncryptedKey")[0].InnerText;
var cipherText = doc.GetElementsByTagName("e:CipherValue")[1].InnerText;
var cipher = Convert.FromBase64String(cipherText);

 var clientCert = new X509Certificate2(@"C:\certificates\xxxxx.pfx", "xxxxxx");
 var rsa = (RSACryptoServiceProvider)clientCert.PrivateKey;
 byte[] key = rsa.Decrypt(Convert.FromBase64String(encryptedKey), false);

的xml:

<s:Envelope 
    xmlns:s="http://www.w3.org/2003/05/soap-envelope" 
    xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
    <s:Header>
        <VsDebuggerCausalityData 
            xmlns="http://schemas.microsoft.com/vstudio/diagnostics/servicemodelsink">uIDPo4tYpt6X40FEk+VSAe5mc8MAAAAAP497cBuXfk+uFIOY80O0iuLtIW56q7hLktgVYPhbnHMACQAA
        </VsDebuggerCausalityData>
        <o:Security s:mustUnderstand="1" 
            xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
            <o:BinarySecurityToken u:Id="uuid-10490fb0-8ee0-4a4c-a8db-77242c9a3b7f-2" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">MIIF+TCCBOGgAwIBAgIQIWv3OdE866kXP/MQVRf4fjANBgkqhkiG9w0BAQsFADCBljELMAkGA1UEBhMCR0IxGzAZBgNVBAgTEkdyZWF0ZXIgTWFuY2hlc3RlcjEQMA4GA1UEBxMHU2FsZm9yZDEaMBgGA1UEChMRQ09NT0RPIENBIExpbWl0ZWQxPDA6BgNVBAMTM0NPTU9ETyBSU0EgT3JnYW5pemF0aW9uIFZhbGlkYXRpb24gU2VjdXJlIFNlcnZlciBDQTAeFw0xODAxMDMwMDAwMDBaFw0yMTAxMDIyMzU5NTlaMIHVMQswCQYDVQQGEwJVUzEOMAwGA1UEERMFOTQ2MTIxCzAJBgNVBAgTAkNBMRAwDgYDVQQHEwdPYWtsYW5kMRcwFQYDVQQJEw4xIEthaXNlciBQbGF6YTEsMCoGA1UEChMjS2Fpc2VyIEZvdW5kYXRpb24gSGVhbHRoIFBsYW4sIEluYy4xDTALBgNVBAsTBEtQSVQxGTAXBgNVBAsTEE11bHRpLURvbWFpbiBTU0wxJjAkBgNVBAMTHWNucGRjbnYwMjQzMi5kY2xkLnBsZGMua3Aub3JnMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqQ3uaJLeAueQJz8GvaCmQiql22cIa4d1nUbz71VXc2wyB64AcfRu5PhupHo8/cGlc7rrpZ959Ckgxp30JKGfUVZf3Ec3Uie8ycp5Sx5ISimBEp94v5AnF1dhiFE8i0UQJaDo6HxUkCM98ekrmGM4i1IVPhGbzNLupNAbSlY496xaUcNtVmKIHZ5Sjp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03+qmxfYi0BIBcST08JB6dnfnjAZrSRXXSmuIBTei76XRjWTvKsE3yIGJShvRYSQWHthumQJ3+lV30k9rJ6bF2nByqhdO1mr1eod9u4ndl8vnxuGfv0j9Nqc+H5yPb4VNHt</o:BinarySecurityToken>
            <e:EncryptedKey Id="_0" 
                xmlns:e="http://www.w3.org/2001/04/xmlenc#">
                <e:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p">
                    <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" 
                        xmlns="http://www.w3.org/2000/09/xmldsig#" />
                </e:EncryptionMethod>
                <KeyInfo 
                    xmlns="http://www.w3.org/2000/09/xmldsig#">
                    <o:SecurityTokenReference>
                        <o:KeyIdentifier ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509SubjectKeyIdentifier" EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">j0ZKFBmTz3Kj0cQ82rq63MYAR+0=</o:KeyIdentifier>
                    </o:SecurityTokenReference>
                </KeyInfo>
                <e:CipherData>
                    <e:CipherValue>ANCElFZ5v/nkh7olNmxSN3tugOi0WakX2loTeKAc6xg97KgUGsCpOcMQIEBeij9YUUtW7iciBqfNX9dd4FYWVap4MI3MVz0KAdfT8m5aLIlcrfvdSZ2/OtoeiriNn6s51yhbcVGBRw9kL9suAYk5sREdTEOd9kI2BUj1d5jmLMy+iAvoSioGrVXPCScJPOPiWKf5mDyx59bAf6W7sVkdqXUpiw1ROxGeqYA4yXL2KBcP5gJ59AZ7LedQcg8mRD6nl+lg/yp0gVdkkEtzN92paw/mLOBeXk5NXiltMvudNJKYkSp6ZfhXy2wIUR7LYR57nUB4mNRaIOC28QAASCF7nQ==</e:CipherValue>
                </e:CipherData>
                <e:ReferenceList>
                    <e:DataReference URI="#_2" />
                </e:ReferenceList>
            </e:EncryptedKey>
            <Signature 
                xmlns="http://www.w3.org/2000/09/xmldsig#">
                <SignedInfo>
                    <CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
                    <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
                    <Reference URI="#_1">
                        <Transforms>
                            <Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
                        </Transforms>
                        <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
                        <DigestValue>akiomlPdi6j1h6r9NDqmh9G1GD0=</DigestValue>
                    </Reference>
                </SignedInfo>
                <SignatureValue>LIjqWD/BXsoA0XNR7hvmcTkDIdUvgy2BGDT2Xwz4OSerUMAxMO2wpfScIA9ISzxt0mw6lw8KKmRzecifCC3CMYqCig3Yu2u/VZmeyA55oeKHq8GTE4Tn9IFq0/053GCODfJOlrKYM/b1Xj//x2YvE/opKw5l3V3lsVvpXfmkci3jNUM4E0oixlQVIKjcKB68L5b6+9PS97t9Yj+dGc4Z2/aCS+I2dVMCcH62FeB/P3XwelZoy5LN2GM9sVWJQ+bnUmbpRlaY5INPnvFbZ0yfD9umxB4jS48smsPvMDxZzx7dr1SdcPA3Rbsrx495Y9r1KEFoXP6vuKf18EEhgGWpXQ==</SignatureValue>
                <KeyInfo>
                    <o:SecurityTokenReference>
                        <o:Reference ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" URI="#uuid-10490fb0-8ee0-4a4c-a8db-77242c9a3b7f-2" />
                    </o:SecurityTokenReference>
                </KeyInfo>
            </Signature>
        </o:Security>
    </s:Header>
    <s:Body u:Id="_1">
        <e:EncryptedData Id="_2" Type="http://www.w3.org/2001/04/xmlenc#Content" 
            xmlns:e="http://www.w3.org/2001/04/xmlenc#">
            <e:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc" />
            <e:CipherData>
                <e:CipherValue>3kESnJnhc8KndR5BIEgqcV1z1RuAdu7bjPo4nG3b+4gXUcG65uvMk2OqYXLebgo1elshyvT/iQh8PGPo4+AlwD90hRbRurb6R2dMPUbzwdzPweCr5xbTCsGfmADRI7uoqQk+dprrnS8wuYIkj87crsD0hXIwNbLnvg4pAR3djwrG3EZCE0bEKL2JMOBbEzYu3HwqYoee4jgQf+bFsIfAN1snYcnfCYIZ9iwQ7Dc21djs749j//IoClBRx20dX7D9iAHwEeC686Ukmc2cvl4vMXGv3s91KhkQxOAeG7pO32YYhS86kHSrOMtcqVCBwSHURLpnZ2MJgTQsOkZT/CWAE+ws3KQ9uZeX1ZgHpAMrOhwvnhEByJYllb1UHIzpjslzfTWrr4ZO719BBPYOWxzSBzUHOr4sQvho4ANX7AabfnYUkPcwkLwMhICHEcolVUKR15bSFXD8Aot7xOcuaeGBiQIFAYRQyJqn0vkGN+4QmEnX7nNevb8M908YvpeiRC3KU8mj5j6iOdp+kdIO6GYS7F9Kgo7qlnaQIx51WmD4EOHXXsliG24pKW/osfCOTs7TeUpgOTCkPLsRS2dutTVllDzNlG0J/4BKTlo/HSCqbfwcxDv05Kxzdudl9IyJUKKKkdEvoyHKJgmSe3SgHv6v0GfU5blZvs1gy5yFPf4RBuyr+on0KNT1xe/wQ9QxWl0HH+Bxij2GD59OQ/GDEGfA2mb1NO/rlepRu+ovb+jjS881l0DAJTNxUK5lDBN3FrkOsx54oYq8uuhu9Bn4dfsEFcnRsHQY6bp1eHwH1xZQY/Kz2OiO91du9L1GtB+M+6e7shf7hJ7BV3qLuMXImndIeu180YjAY+CQJtNq+ffY3Hd0TLAbnx5CqgMqbvM8EYknAHkeEQjrtwJdojjibPoAOYt+wK5XJ6FbIsuDIrCnHeVrEVc8QwYiw4XGJFY2ZhuW</e:CipherValue>
            </e:CipherData>
        </e:EncryptedData>
    </s:Body>
</s:Envelope>

0 个答案:

没有答案