c# - Portable encryption algorithm -
i need implement new, or existing, encryption algorithm encrypt , decrypt string using string key. problem this algorithm have work independently computer on used.
so methods signature are:
public static string encrypt(this string source, string key); public static string decrypt(this string source, string key);
i tried these algorithms, don't work way want:
public static string encrypt(this string source, string key) { if (string.isnullorempty(source) || string.isnullorempty(key)) throw new argumentexception(); cspparameters cspp = new cspparameters { keycontainername = key }; using (var rsa = new rsacryptoserviceprovider(cspp) { persistkeyincsp = true }) return bitconverter.tostring(rsa.encrypt(utf8encoding.utf8.getbytes(source), true)); } public static string decrypt(this string source, string key) { if (string.isnullorempty(source) || string.isnullorempty(key)) throw new argumentexception(); try { cspparameters cspp = new cspparameters { keycontainername = key }; using (var rsa = new rsacryptoserviceprovider(cspp) { persistkeyincsp = true }) { string[] decryptarray = source.split(new char[] { '-' }, stringsplitoptions.none); byte[] bytes = array.convertall<string, byte>(decryptarray, (s => convert.tobyte(byte.parse(s, numberstyles.hexnumber)))); return utf8encoding.utf8.getstring(rsa.decrypt(bytes, true)); } } catch { return null; } }
how can do?
the keycontainername not key. in example above, passing key store name, you'll create new rsa keypair on each machine store name of key passed in (rather storename of "myrsakeypair" or whatever). mean both public , private keys different , routines won't seem work.
also: you're using asymmetric encryption, has maximum block size limit of key length. means you'll either need create chunking mechanism (slow asymmetric encryption expensive) or use symmetric aes aes key being sent using asymmetric encryption (such rsa) on per conversation basis.
you need export rsa public key , import remote machine's keystore. easier still generating x509 certificate (you can self sign if you're going between couple of machines, exporting public part of .cer file, can use x509 certificate store api rsa provider, meaning have nice transportable key.
public static rsacryptoserviceprovider getrsaproviderfromcertificate() { x509store store = new x509store(storelocation.localmachine); store.open(openflags.readonly | openflags.openexistingonly); x509certificate2collection certcollection = (x509certificate2collection)store.certificates; foreach(x509certificate2 cert in certcollection) { if (cert.subjectname.name.indexof("thecertiwanttouse") > 0) { return cert.privatekey rsacryptoserviceprovider; } }
i hope that's explicit enough...
if want without certs
// export public key (on encrypting end) publickey = rsaprovider.toxmlstring(false); // write public key file publickeyfile = file.createtext(publickeyfilename); publickeyfile.write(publickey);
then on other machine
// select target csp cspparams = new cspparameters(); cspparams.providertype = 1; // prov_rsa_full rsaprovider = new rsacryptoserviceprovider(cspparams); // read public key file publickeyfile = file.opentext(publickeyfilename); publickeytext = publickeyfile.readtoend(); // import public key rsaprovider.fromxmlstring(publickeytext);
Comments
Post a Comment