Opc.Ua.SecurityPolicies.Encrypt C# (CSharp) Method

Encrypt() public static method

Encrypts the text using the SecurityPolicyUri and returns the result.
public static Encrypt ( X509Certificate2 certificate, string securityPolicyUri, byte plainText ) : EncryptedData
certificate X509Certificate2
securityPolicyUri string
plainText byte
return EncryptedData
        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;
        }

Usage Example

コード例 #1
0
        /// <summary>
        /// Encrypts the DecryptedPassword using the EncryptionAlgorithm and places the result in Password
        /// </summary>
        public override void Encrypt(X509Certificate2 certificate, byte[] senderNonce, string securityPolicyUri)
        {
            if (m_decryptedPassword == null)
            {
                m_password = null;
                return;
            }

            // handle no encryption.
            if (String.IsNullOrEmpty(securityPolicyUri) || securityPolicyUri == SecurityPolicies.None)
            {
                m_password            = new UTF8Encoding().GetBytes(m_decryptedPassword);
                m_encryptionAlgorithm = null;
                return;
            }

            // encrypt the password.
            byte[] dataToEncrypt = Utils.Append(new UTF8Encoding().GetBytes(m_decryptedPassword), senderNonce);

            EncryptedData encryptedData = SecurityPolicies.Encrypt(
                certificate,
                securityPolicyUri,
                dataToEncrypt);

            m_password            = encryptedData.Data;
            m_encryptionAlgorithm = encryptedData.Algorithm;
        }
All Usage Examples Of Opc.Ua.SecurityPolicies::Encrypt