c# - Decrypt message with private key RSA -
i have problem how can find private key windows 2008 server.
first encrypt data public key extracted url https this:
public static string encrypt(string data) { try { var crypto = new rsacryptoserviceprovider(2048); var rsakeyinfo = crypto.exportparameters(false); rsakeyinfo.modulus = publickeybyte(); crypto.importparameters(rsakeyinfo); var bytesdata = encoding.unicode.getbytes(data); var bytescyphertext = crypto.encrypt(bytesdata, false); var cyphertext = convert.tobase64string(bytescyphertext); return cyphertext; } catch (exception ex) { return null; } } private static byte[] publickeybyte() { uri u = new uri("https:\\domain.com"); servicepoint sp = servicepointmanager.findservicepoint(u); string groupname = guid.newguid().tostring(); httpwebrequest req = httpwebrequest.create(u) httpwebrequest; req.connectiongroupname = groupname; using (webresponse resp = req.getresponse()) { } sp.closeconnectiongroup(groupname); return sp.certificate.getpublickey(); ; }
now dont know how extract private key in c# decrypting message? , want know more informations this
thanks,
i resolved extracting certificate file .pfx , im using system.security.cryptography.x509certificates encrypting , decrypting:
public static string encrypt(string data) { try { var path = @"certificate.pfx"; var password = "test"; var collection = new x509certificate2collection(); collection.import(path, password, x509keystorageflags.persistkeyset); var certificate = collection[0]; var publickey = certificate.publickey.key rsacryptoserviceprovider; var bytesdata = convert.frombase64string(data); var encrypteddata = publickey.encrypt(bytesdata, false); var cyphertext = convert.tobase64string(encrypteddata); return cyphertext; } catch (exception ex) { return null; } } public static string decrypt(string data) { try { var path = @"certificate.pfx"; var password = "test"; var collection = new x509certificate2collection(); collection.import(path, password, x509keystorageflags.persistkeyset); var certificate = collection[0]; var privatekey = certificate.privatekey rsacryptoserviceprovider; var bytesdata = convert.frombase64string(data); var databyte = privatekey.decrypt(bytesdata, false); return convert.tobase64string(databyte); } catch (exception ex) { return ""; } }
Comments
Post a Comment