From a public key string to RSACryptoServiceProvider object

Rusk
Rusk used Ask the Experts™
on
Hello,

I have some encrypted text, which I know to have been encrpyted using RSA.
The sender of this text has provided me with a public key in a simple .txt file. It looks like this:

-----BEGIN CERTIFICATE-----
MIIDijCCAnKgAwIBAgIFAMGDJ+gwDQYJKoZIhvcNAQEEBQAwUzELMAkGA1UEBhMC
S1oxDDAKBgNVBAoTA0tLQjELMAkGA1UECxMCQ0ExDzANBgNVBAMTBktLQiBDQTEY
MBYGCSqGSIb3DQEJARYJY2FAa2tiLmt6MB4XDTA0MTAxODEwMzYwNloXDTA5MTAx
NzEwMzYwNlowgYMxCzAJBgNVBAYTAktaMQ8wDQYDVQQHEwZBbG1hdHkxHDAaBgNV
BAoTE0pTQyBLYXprb21tZXJ0c2JhbmsxEzARBgNVBAsTCkhlYWRPZmZpY2UxFDAS
BgNVBAMTC0VQQVkgU1lTVEVNMRowGAYJKoZIhvcNAQkBFgtlcGF5QGtrYi5rejCB
nzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAwNtnbUr82ALmO1E0tQ8Ejp1D+9GH
EKPeqVTokLb95VhvXX3GoYCKPseFvXMD8x+P0I9x03nJnSRyP6hJ85W/jsElkuYj
LzYWFfYhCeCQgit2lbSx9FbGVJ1B7M4ZMYuub4DlgtjQ8ebbqRACZ3Yw7LhciUcS
235c4K+zmL7p5i0CAwEAAaOBtzCBtDAdBgNVHQ4EFgQU+7GVWbXvHRDbdxFSXqc6
vd6VPQcwHwYDVR0jBBgwFoAU712nYyivxvN+d0LbneCElQZ9clMwDAYDVR0TBAUw
AwEBADAOBgNVHQ8BAf8EBAMCBPAwNQYDVR0fBC4wLDAqoCigJoYkaHR0cDovL3d3
dy5ra2Iua3ovY2VydHJvb3Qva2tiY2EuY3JsMB0GA1UdJQQWMBQGCCsGAQUFBwMC
BggrBgEFBQcDBDANBgkqhkiG9w0BAQQFAAOCAQEAB0jZpXUO9O0uWJZAJP28ATnC
PCMYa9jheM8MwKhSqe0m1IQ8mkPBzFnKqgmBdyPSp94Fy6xY2ciEnJ5oCEJGM9Nm
L1kUeg4/HqiTcEUaZWtec4SvwEhKjfoXVwx/teV9KNQoQ8YNyQflEm6DMjiZ6GDM
qLNV2ZLD5RytWKJYzqg/WScKpuGHYBlUmAi75Ew4nNx1PXi0ATZ9wc0aiXYlwAaP
pDhNvvLcVLiBjjs/o/QhBgtKewAbltVnU97gf/+yQErbfrL2z+Hg4hF0R63vkjj5
HsxOZ+pVNi0p+TzKyI1QcS4f53rxo0pMYxHn0LJQquCzbyDKcipkg9qrAFkxoA==
-----END CERTIFICATE-----

My problem is to import the contents of this text file into a fully populated RSACryptoProvider object in order to call it's .decrypt method.
It seems like a simple matter, but my searches have been unfruitful.
Any help will be much appreciated.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Cryptographic Engineer
Commented:
1) Rename the attachment from .txt to .cer. This is a PEM encoded certificate file.  They used the .txt extension since .cer gets blocked by many email firewalls.  That should be all you need to do to get their certificate, then you can worry about how to import it.

2) This is not encrypted - it is Base64 encoded.  Big difference.   To read the contents you can even keep it as a .txt file or rename to .cer or whatever you like - you don't need anything special to do this.  You can use the certutil -dump to display the contents.  Here is a portion of that output:

C:\CertTemp>certutil -dump 1.txt   | more
402.203.0: 0x80070057 (WIN32: 87): ..CertCli Version
X509 Certificate:
Version: 3
Serial Number: c18327e8
Signature Algorithm:
    Algorithm ObjectId: 1.2.840.113549.1.1.4 md5RSA
    Algorithm Parameters:
    05 00
Issuer:
    E=ca@kkb.kz
    CN=KKB CA
    OU=CA
    O=KKB
    C=KZ

NotBefore: 10/18/2004 4:36 AM
NotAfter: 10/17/2009 4:36 AM

Subject:
    E=epay@kkb.kz
    CN=EPAY SYSTEM
    OU=HeadOffice
    O=JSC Kazkommertsbank
    L=Almaty
    C=KZ

3) To actually decode the text, you can use this:
certutil -decode filenameIN.txt filenameOUT.txt
This won't probably give you what you want, however, as the output will just be a DER certificate file that if you view it will be a bunch of square boxes and an occasional random character, but if for some reason you need a DER encoded cert file instead of PEM then there it is (most apps accept either both, if they pick one its usually PEM).
ParanormasticCryptographic Engineer

Commented:
Never mind the last post - after posting it I realized I just read it wrong... I thought the cert text was what you were trying to decode...

RSACryptoProvider Class Usage, Examples & Important Notes:
http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsacryptoserviceprovider.aspx

Take a look at this previous post and the link posted within that and see if that helps you out.
http://www.experts-exchange.com/Programming/Languages/.NET/Web_Services/Q_24025947.html

Author

Commented:
Very enlightening post about the contents of the file I've posted. Not only points the way to solving the problem, but also provides a better understanding of the underlying factors.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial