Example #1
0
        public byte[] Decrypt(byte[] bytes, int index, int count, EncryptionPaddingMode padding)
        {
            ThrowIfObjectDisposed();

            if (this.AlgorithmName != AsymmetricAlgName.RSA)
            {
                throw new InvalidOperationException($"Only supported by RSA.");
            }

            if (bytes == null)
            {
                throw new ArgumentNullException(nameof(bytes));
            }

            if (index < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(index), $"Argument '{nameof(index)}' value must be >= 0.");
            }

            if (index >= bytes.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(index), $"Argument '{nameof(index)}' value exceeds the maximum length of argument '{nameof(bytes)}'.");
            }

            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(count), $"Argument '{nameof(count)}' value must be >= 0.");
            }

            if (index + count > bytes.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(count), $"Argument '{nameof(index)} + {nameof(count)}' value exceeds the maximum length of argument '{nameof(bytes)}'.");
            }


            if (count == bytes.Length)
            {
                return((this.provider as System.Security.Cryptography.RSA).Decrypt(bytes, ToSysEncryptionPaddingMode(padding)));
            }
            else
            {
                var parts = new byte[count];
                Array.Copy(bytes, index, parts, 0, count);

                return((this.provider as System.Security.Cryptography.RSA).Decrypt(parts, ToSysEncryptionPaddingMode(padding)));
            }
        }
Example #2
0
        public byte[] Decrypt(byte[] bytes, EncryptionPaddingMode padding)
        {
            AssertUtil.ArgumentNotNull(bytes, nameof(bytes));

            return(Decrypt(bytes, 0, bytes.Length, padding));
        }
Example #3
0
        static System.Security.Cryptography.RSAEncryptionPadding ToSysEncryptionPaddingMode(EncryptionPaddingMode padding)
        {
            switch (padding)
            {
            case EncryptionPaddingMode.Pkcs1:
                return(System.Security.Cryptography.RSAEncryptionPadding.Pkcs1);

            case EncryptionPaddingMode.OaepSHA1:
                return(System.Security.Cryptography.RSAEncryptionPadding.OaepSHA1);

            case EncryptionPaddingMode.OaepSHA256:
                return(System.Security.Cryptography.RSAEncryptionPadding.OaepSHA256);

            case EncryptionPaddingMode.OaepSHA384:
                return(System.Security.Cryptography.RSAEncryptionPadding.OaepSHA384);

            case EncryptionPaddingMode.OaepSHA512:
                return(System.Security.Cryptography.RSAEncryptionPadding.OaepSHA512);

            default:
                throw new Exception();
            }
        }