public static EncryptedData Encrypt(X509Certificate2 certificate, string securityPolicyUri, byte[] plainText)
{
EncryptedData encryptedData = new EncryptedData();
encryptedData.Algorithm = null;
encryptedData.Data = plainText;
// check if nothing to do.
if (plainText == null)
{
return encryptedData;
}
// nothing more to do if no encryption.
if (String.IsNullOrEmpty(securityPolicyUri))
{
return encryptedData;
}
// encrypt data.
switch (securityPolicyUri)
{
case SecurityPolicies.Basic256:
case SecurityPolicies.Basic256Sha256:
{
encryptedData.Algorithm = SecurityAlgorithms.RsaOaep;
encryptedData.Data = RsaUtils.Encrypt(plainText, certificate, true);
break;
}
case SecurityPolicies.Basic128Rsa15:
{
encryptedData.Algorithm = SecurityAlgorithms.Rsa15;
encryptedData.Data = RsaUtils.Encrypt(plainText, certificate, false);
break;
}
case SecurityPolicies.None:
{
break;
}
default:
{
throw ServiceResultException.Create(
StatusCodes.BadSecurityPolicyRejected,
"Unsupported security policy: {0}",
securityPolicyUri);
}
}
return encryptedData;
}