예제 #1
0
        public override void ImportParameters(DSAParameters parameters)
        {
            if (parameters.P == null || parameters.Q == null || parameters.G == null || parameters.Y == null)
            {
                throw new ArgumentException(SR.Cryptography_InvalidDsaParameters_MissingFields);
            }

            // J is not required and is not even used on CNG blobs. It should however be less than P (J == (P-1) / Q). This validation check
            // is just to maintain parity with DSACryptoServiceProvider, which also performs this check.
            if (parameters.J != null && parameters.J.Length >= parameters.P.Length)
            {
                throw new ArgumentException(SR.Cryptography_InvalidDsaParameters_MismatchedPJ);
            }

            bool hasPrivateKey = parameters.X != null;

            int keySizeInBytes = parameters.P.Length;
            int keySizeInBits  = keySizeInBytes * 8;

            if (parameters.G.Length != keySizeInBytes || parameters.Y.Length != keySizeInBytes)
            {
                throw new ArgumentException(SR.Cryptography_InvalidDsaParameters_MismatchedPGY);
            }
            if (hasPrivateKey && parameters.X.Length != parameters.Q.Length)
            {
                throw new ArgumentException(SR.Cryptography_InvalidDsaParameters_MismatchedQX);
            }

            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter bw = new BinaryWriter(ms))
                {
                    if (keySizeInBits <= MaxV1KeySize)
                    {
                        GenerateV1DsaBlob(bw, parameters, keySizeInBytes, hasPrivateKey);
                    }
                    else
                    {
                        GenerateV2DsaBlob(bw, parameters, keySizeInBytes, hasPrivateKey);
                    }
                }

                ms.Flush();
                byte[]            blob         = ms.ToArray();
                CngKey            cngKey       = CngKey.Import(blob, hasPrivateKey ? CngKeyBlobFormat.GenericPrivateBlob : CngKeyBlobFormat.GenericPublicBlob);
                CngExportPolicies exportPolicy = cngKey.ExportPolicy | CngExportPolicies.AllowPlaintextExport;
                cngKey.SetProperty(new CngProperty(NCryptNative.KeyPropertyName.ExportPolicy, BitConverter.GetBytes((int)exportPolicy), CngPropertyOptions.None));
                Key = cngKey;
            }
        }
        public BasicSymmetricCipherLiteNCrypt(
            Func <CngKey> cngKeyFactory,
            CipherMode cipherMode,
            int blockSizeInBytes,
            ReadOnlySpan <byte> iv,
            bool encrypting,
            int paddingSizeInBytes)
        {
            BlockSizeInBytes   = blockSizeInBytes;
            PaddingSizeInBytes = paddingSizeInBytes;
            _encrypting        = encrypting;
            _key = cngKeyFactory();
            CngProperty chainingModeProperty = cipherMode switch
            {
                CipherMode.ECB => s_ECBMode,
                CipherMode.CBC => s_CBCMode,
                CipherMode.CFB => s_CFBMode,
                _ => throw new CryptographicException(SR.Cryptography_InvalidCipherMode),
            };

            _key.SetProperty(chainingModeProperty);

            Reset(iv);
        }