public String RsaDecrypt(String input, RSAEntity entity) { if (String.IsNullOrEmpty(input)) { return(null); } try { byte[] keybuf = Convert.FromBase64String(input); //Create a new instance of RSACryptoServiceProvider. var RSA = new RSACryptoServiceProvider(); //Import the RSA Key information. This only needs //toinclude the public key information. var objRsaPars = new RSAParameters(); objRsaPars.Modulus = entity.Modulus; objRsaPars.Exponent = entity.Exponent; objRsaPars.P = entity.P; objRsaPars.Q = entity.Q; objRsaPars.DP = entity.DP; objRsaPars.DQ = entity.DQ; objRsaPars.InverseQ = entity.InverseQ; objRsaPars.D = entity.D; RSA.ImportParameters(objRsaPars); //Decrypt the passed byte array and specify OAEP padding. //OAEP padding is only available on Microsoft Windows XP or //later. return(Encoding.UTF8.GetString(RSA.Decrypt(keybuf, false))); } //Catch and display a CryptographicException catch (CryptographicException e) { //return null; throw e; } }
public String RsaEncrypt(String input, RSAEntity entity) { input = Byte2Hex(Hex2Byte(input)); if (String.IsNullOrEmpty(input)) { return(null); } try { byte[] inbuf = Encoding.UTF8.GetBytes(input); //Create a new instance of RSACryptoServiceProvider. var rsa = new RSACryptoServiceProvider(); //Import the RSA Key information. This only needs //toinclude the public key information. var objRsaPars = new RSAParameters { Modulus = entity.Modulus, Exponent = entity.Exponent }; rsa.ImportParameters(objRsaPars); //Encrypt the passed byte array and specify OAEP padding. //OAEP padding is only available on Microsoft Windows XP or //later. return(Convert.ToBase64String(rsa.Encrypt(inbuf, false))); } //Catch and display a CryptographicException catch (CryptographicException e) { //return null; throw e; } }