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

Encrypt() public static method

Encrypts the data using RSA PKCS#1 v1.5 encryption.
public static Encrypt ( byte dataToEncrypt, X509Certificate2 encryptingCertificate, bool useOaep ) : byte[]
dataToEncrypt byte
encryptingCertificate System.Security.Cryptography.X509Certificates.X509Certificate2
useOaep bool
return byte[]
        public static byte[] Encrypt(
            byte[] dataToEncrypt,
            X509Certificate2 encryptingCertificate,
            bool useOaep)
        {
            int plaintextBlockSize = GetPlainTextBlockSize(encryptingCertificate, useOaep);
            int blockCount = ((dataToEncrypt.Length + 4) / plaintextBlockSize) + 1;
            int plainTextSize = blockCount * plaintextBlockSize;
            int cipherTextSize = blockCount * GetCipherTextBlockSize(encryptingCertificate, useOaep);

            byte[] plainText = new byte[plainTextSize];

            // encode length.
            plainText[0] = (byte)((0x000000FF & dataToEncrypt.Length));
            plainText[1] = (byte)((0x0000FF00 & dataToEncrypt.Length) >> 8);
            plainText[2] = (byte)((0x00FF0000 & dataToEncrypt.Length) >> 16);
            plainText[3] = (byte)((0xFF000000 & dataToEncrypt.Length) >> 24);

            // copy data.
            Array.Copy(dataToEncrypt, 0, plainText, 4, dataToEncrypt.Length);

            byte[] buffer = new byte[cipherTextSize];
            ArraySegment<byte> cipherText = Encrypt(new ArraySegment<byte>(plainText), encryptingCertificate, useOaep, new ArraySegment<byte>(buffer));
            System.Diagnostics.Debug.Assert(cipherText.Count == buffer.Length);

            return buffer;
        }

Same methods

RsaUtils::Encrypt ( ArraySegment dataToEncrypt, X509Certificate2 encryptingCertificate, bool useOaep, ArraySegment outputBuffer ) : ArraySegment

Usage Example

コード例 #1
0
        /// <summary>
        /// Encrypts the text using the SecurityPolicyUri and returns the result.
        /// </summary>
        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);
        }