c# - RSA Public key encryption using modulus and exponent -
i want generate public key using rsa given modulus , exponent values.
public static string rsapublic(string toencrypt) { var crypt = asymmetrickeyalgorithmprovider.openalgorithm(asymmetricalgorithmnames.rsapkcs1); var buffer = cryptographicbuffer.convertstringtobinary(toencrypt, binarystringencoding.utf8); string publikkey = modulus + exponent; publikkey.replace("\r\n", ""); var plaintextbytes = system.text.encoding.utf8.getbytes(publikkey); string pk = system.convert.tobase64string(plaintextbytes); ibuffer keybuffer = cryptographicbuffer.decodefrombase64string(pk); cryptographickey key = crypt.importpublickey(keybuffer, cryptographicpublickeyblobtype.x509subjectpublickeyinfo); // throws exception here, have tried using 4 available blobtypes // var key = crypt.createkeypair(512); var sigbuffer = cryptographicengine.encrypt(key, buffer, null); string signature = cryptographicbuffer.encodetobase64string(sigbuffer); return signature; }
following exception message : "asn1 bad tag value met. (exception hresult: 0x8009310b)"
stacktrace : " @ windows.security.cryptography.core.asymmetrickeyalgorithmprovider.importpublickey(ibuffer keyblob, cryptographicpublickeyblobtype blobtype) @ myproject.general.utility.rsapublic(string toencrypt)"
i not able figure out right way generate cryptographickey necessary create encrypted string. appreciated.
you should have of public key format, key format must obey ans.1 rule. @ question rsa public key format
if still not sure format, use asymmetrickeyalgorithmprovider create 2 or 3 or more public keys , convert them hex string.
you can find format.
i use rsapkcs1 create sample public key 30818902818100ae037f0bcb6b4a5b661d7fc43178133b190f12f6c4e0e3ca694d4ec47458862b89691cb06767aa5054a92fc61ec8dc5c53983341c78ba3b95faf887b108093b41632a2ae324b0aaccab4172d83d7691476a6a97683d595355bd0bfa1fa5ea4d9cf2d5836ddb471de1df34ec27b6f0c4f903a13b6700cfb08ada8e43cf3b0cf7b0203010001
if use rsapkcs1 create key, public key start 30818902818100 ae037f0bcb6b4a5b661d7fc43178133b190f12f6c4e0e3ca694d4ec47458862b89691cb06767aa5054a92fc61ec8dc5c53983341c78ba3b95faf887b108093b41632a2ae324b0aaccab4172d83d7691476a6a97683d595355bd0bfa1fa5ea4d9cf2d5836ddb471de1df34ec27b6f0c4f903a13b6700cfb08ada8e43cf3b0cf7b end 0203010001
just join header , tail fill format.
Comments
Post a Comment