public IHttpActionResult Encrypt([FromBody] Cryptographymodel item) { try { byte[] keyBytes = Convert.FromBase64String(item.PublicKey); RsaKeyParameters publicKeyInfo = (RsaKeyParameters)PublicKeyFactory.CreateKey(keyBytes); RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); RSAParameters rsaParameters = new RSAParameters(); rsaParameters.Modulus = publicKeyInfo.Modulus.ToByteArrayUnsigned(); rsaParameters.Exponent = publicKeyInfo.Exponent.ToByteArrayUnsigned(); rsa.ImportParameters(rsaParameters); byte[] bytes = Encoding.UTF8.GetBytes(item.Plaintext); byte[] enc = rsa.Encrypt(bytes, false); string base64Enc = Convert.ToBase64String(enc); return(Ok(base64Enc)); } catch (Exception ex) { return(InternalServerError(ex)); } }
public IHttpActionResult Decrypt([FromBody] Cryptographymodel item) { try { RsaPrivateCrtKeyParameters privateKey = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(item.PrivateKey)); RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); RSAParameters rsaParameters2 = DotNetUtilities.ToRSAParameters((RsaPrivateCrtKeyParameters)privateKey); rsa.ImportParameters(rsaParameters2); byte[] dec = rsa.Decrypt(Convert.FromBase64String(item.Chipertext), false); string decStr = Encoding.UTF8.GetString(dec); return(Ok(decStr)); } catch (Exception ex) { return(InternalServerError(ex)); } }