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

Decrypt() public static method

Decrypts the CipherText using the SecurityPolicyUri and returns the PlainTetx.
public static Decrypt ( X509Certificate2 certificate, string securityPolicyUri, EncryptedData dataToDecrypt ) : byte[]
certificate X509Certificate2
securityPolicyUri string
dataToDecrypt EncryptedData
return byte[]
        public static byte[] Decrypt(X509Certificate2 certificate, string securityPolicyUri, EncryptedData dataToDecrypt)
        {
            // check if nothing to do.
            if (dataToDecrypt == null)
            {
                return null;
            }

            // nothing more to do if no encryption.
            if (String.IsNullOrEmpty(securityPolicyUri))
            {
                return dataToDecrypt.Data;
            }

            // decrypt data.
            switch (securityPolicyUri)
            {
                case SecurityPolicies.Basic256:
                case SecurityPolicies.Basic256Sha256:
                    {
                        if (dataToDecrypt.Algorithm == SecurityAlgorithms.RsaOaep)
                        {
                            return RsaUtils.Decrypt(new ArraySegment<byte>(dataToDecrypt.Data), certificate, true);
                        }

                        break;
                    }

                case SecurityPolicies.Basic128Rsa15:
                    {
                    if (dataToDecrypt.Algorithm == SecurityAlgorithms.Rsa15)
                        {
                            return RsaUtils.Decrypt(new ArraySegment<byte>(dataToDecrypt.Data), certificate, false);
                        }

                        break;
                    }

                case SecurityPolicies.None:
                    {
                        if (String.IsNullOrEmpty(dataToDecrypt.Algorithm))
                        {
                            return dataToDecrypt.Data;
                        }

                        break;
                    }

                default:
                    {
                        throw ServiceResultException.Create(
                            StatusCodes.BadSecurityPolicyRejected,
                            "Unsupported security policy: {0}",
                            securityPolicyUri);
                    }
            }

            throw ServiceResultException.Create(
                StatusCodes.BadIdentityTokenInvalid, 
                "Unexpected encryption algorithm : {0}",
                dataToDecrypt.Algorithm);
        }

Usage Example

コード例 #1
0
        /// <summary>
        /// Decrypts the Password using the EncryptionAlgorithm and places the result in DecryptedPassword
        /// </summary>
        public override void Decrypt(X509Certificate2 certificate, byte[] senderNonce, string securityPolicyUri)
        {
            EncryptedData encryptedData = new EncryptedData();

            encryptedData.Data      = m_tokenData;
            encryptedData.Algorithm = m_encryptionAlgorithm;

            byte[] decryptedTokenData = SecurityPolicies.Decrypt(
                certificate,
                securityPolicyUri,
                encryptedData);

            // verify the sender's nonce.
            int startOfNonce = decryptedTokenData.Length;

            if (senderNonce != null)
            {
                startOfNonce -= senderNonce.Length;

                for (int ii = 0; ii < senderNonce.Length; ii++)
                {
                    if (senderNonce[ii] != decryptedTokenData[ii + startOfNonce])
                    {
                        throw new ServiceResultException(StatusCodes.BadSecurityChecksFailed);
                    }
                }
            }

            // copy results.
            m_decryptedTokenData = new byte[startOfNonce];
            Array.Copy(decryptedTokenData, m_decryptedTokenData, startOfNonce);
        }
All Usage Examples Of Opc.Ua.SecurityPolicies::Decrypt