public string Encrypt(byte[] dataToEncrypt) { var rsaKeyParameters = GetRsaKeyParameters(); var rsaEngine = new Pkcs1Encoding(new RsaEngine()); rsaEngine.Init(true, rsaKeyParameters); var encodedDataToEncrypt = new UTF8Encoding().GetBytes(Convert.ToBase64String(dataToEncrypt)); return Convert.ToBase64String(rsaEngine.ProcessBlock(encodedDataToEncrypt, 0, encodedDataToEncrypt.Length)); }
public static byte[] DecryptRsa(string encrypted, string privateKey) { var rsaKeyParameters = GetRsaKeyParameters(privateKey); var rsaEngine = new Pkcs1Encoding(new RsaEngine()); rsaEngine.Init(false, rsaKeyParameters); var decoded = Convert.FromBase64String(encrypted); var decryptedEncodedBytes = rsaEngine.ProcessBlock(decoded, 0, decoded.Length); var decryptedEncodedString = new UTF8Encoding().GetString(decryptedEncodedBytes, 0, decryptedEncodedBytes.Length); return Convert.FromBase64String(decryptedEncodedString); }