Beispiel #1
0
        private static void Test1()
        {
            byte[] test = Utilities.ScoopBytes(123);
            //
            Type type = typeof(HashAlgorithmHelper);

            PropertyInfo[] properties = type.GetProperties(BindingFlags.Static | BindingFlags.Public);
            foreach (PropertyInfo property in properties)
            {
                if (property.GetValue(type, null) is IHashAlgorithm algorithm)
                {
                    _total++;
                    IDigest digest = algorithm.GenerateDigest();
                    XTest(algorithm.Mechanism, digest, test);
                    _execute++;
                }
            }
            //
            List <string> names = new List <string>();

            names.AddRange(new string[] { "BLAKE2b-88", "SHA-512/368", "SHA512/368", "Skein-256-48" });
            foreach (string name in names)
            {
                _total++;
                _execute++;
                HashAlgorithmHelper.TryGetAlgorithm(name, out IHashAlgorithm algorithm);
                IDigest digest = algorithm.GenerateDigest();
                XTest(algorithm.Mechanism, digest, test);
            }
            Console.WriteLine();
        }
Beispiel #2
0
        private static void Demo1()
        {
            byte[] test = Utilities.ScoopBytes(4);
            AsymmetricCipherKeyPair keyPair   = AsymmetricAlgorithmHelper.RSA.GenerateKeyPair();
            IAsymmetricBlockCipher  encryptor = AsymmetricAlgorithmHelper.RSA.GenerateCipher(AsymmetricPaddingMode.PKCS1, keyPair.Public);
            IAsymmetricBlockCipher  decryptor = AsymmetricAlgorithmHelper.RSA.GenerateCipher(AsymmetricPaddingMode.PKCS1, keyPair.Private);

            byte[] enc = encryptor.ProcessBlock(test, 0, test.Length);
            _ = decryptor.ProcessBlock(enc, 0, enc.Length);
        }
Beispiel #3
0
        private static void Demo1()
        {
            byte[]  test   = Utilities.ScoopBytes(123);
            IDigest digest = HashAlgorithmHelper.SHA3_256.GenerateDigest();

            byte[] hash = new byte[digest.GetDigestSize()];
            //byte[] hash = new byte[HashAlgorithmHelper.SHA3_256.HashSize / 8];
            digest.BlockUpdate(test, 0, test.Length);
            digest.DoFinal(hash, 0);
        }
Beispiel #4
0
        private static void Demo3()
        {
            byte[]            test       = Utilities.ScoopBytes(123);
            byte[]            key        = Utilities.ScoopBytes(128 / 8);
            byte[]            iv         = Utilities.ScoopBytes(128 / 8);
            ICipherParameters parameters = SymmetricAlgorithmHelper.HC128.GenerateParameters(key, iv);
            IBufferedCipher   encryptor  = SymmetricAlgorithmHelper.HC128.GenerateCipher(true, parameters);
            IBufferedCipher   decryptor  = SymmetricAlgorithmHelper.HC128.GenerateCipher(false, parameters);

            byte[] enc = encryptor.DoFinal(test, 0, test.Length);
            _ = decryptor.DoFinal(enc, 0, enc.Length);
        }
Beispiel #5
0
        private static void Demo3()
        {
            byte[]            test       = Utilities.ScoopBytes(123);
            byte[]            key        = Utilities.ScoopBytes(128 / 8);
            ICipherParameters parameters = CMACHelper.AES_CMAC.GenerateParameters(key);
            IMac digest = CMACHelper.AES_CMAC.GenerateDigest(parameters);

            byte[] hash = new byte[digest.GetMacSize()];
            //byte[] hash = new byte[CMACHelper.AES_CMAC.HashSize / 8];
            digest.BlockUpdate(test, 0, test.Length);
            digest.DoFinal(hash, 0);
        }
Beispiel #6
0
        private static void Demo1()
        {
            byte[] test = Utilities.ScoopBytes(93);
            AsymmetricCipherKeyPair keyPair = SignatureAlgorithmHelper.SHA256withECDSA.GenerateKeyPair();
            //AsymmetricCipherKeyPair keyPair = AsymmetricAlgorithmHelper.ECDSA.GenerateKeyPair();
            ISigner signer   = SignatureAlgorithmHelper.SHA256withECDSA.GenerateSigner(keyPair.Private);
            ISigner verifier = SignatureAlgorithmHelper.SHA256withECDSA.GenerateSigner(keyPair.Public);

            signer.BlockUpdate(test, 0, test.Length);
            byte[] signature = signer.GenerateSignature();
            verifier.BlockUpdate(test, 0, test.Length);
            bool same = verifier.VerifySignature(signature);
        }
Beispiel #7
0
        private static void Demo2()
        {
            byte[]            test       = Utilities.ScoopBytes(123);
            byte[]            key        = Utilities.ScoopBytes(128 / 8);
            byte[]            nonce      = Utilities.ScoopBytes(104 / 8);
            int               macSize    = 96;
            ICipherParameters parameters = SymmetricAlgorithmHelper.AES.GenerateParameters(key, nonce, macSize, null);
            IBufferedCipher   encryptor  = SymmetricAlgorithmHelper.AES.GenerateCipher(true, SymmetricCipherMode.CCM, SymmetricPaddingMode.NoPadding, parameters);
            IBufferedCipher   decryptor  = SymmetricAlgorithmHelper.AES.GenerateCipher(false, SymmetricCipherMode.CCM, SymmetricPaddingMode.NoPadding, parameters);

            byte[] enc = encryptor.DoFinal(test, 0, test.Length);
            _ = decryptor.DoFinal(enc, 0, enc.Length);
        }
Beispiel #8
0
        private static void Demo4()
        {
            byte[]            test       = Utilities.ScoopBytes(123);
            byte[]            key        = Utilities.ScoopBytes(128 / 8);
            byte[]            iv         = Utilities.ScoopBytes(128 / 8);
            ICipherParameters parameters = MACHelper.AES_MAC.GenerateParameters(key, iv);
            IMac digest = MACHelper.AES_MAC.GenerateDigest(MACCipherMode.CBC, MACPaddingMode.NoPadding, parameters);

            byte[] hash = new byte[digest.GetMacSize()];
            //byte[] hash = new byte[MACHelper.AES_MAC.HashSize / 8];
            digest.BlockUpdate(test, 0, test.Length);
            digest.DoFinal(hash, 0);
        }
Beispiel #9
0
        internal static void Test()
        {
            byte[]        test  = Utilities.ScoopBytes(93);
            List <string> names = new List <string>();

            names.AddRange(new string[] { "RIPEMD320withRSA" });
            foreach (string name in names)
            {
                SignatureAlgorithmHelper.TryGetAlgorithm(name, out ISignatureAlgorithm algorithm);
                AsymmetricCipherKeyPair keyPair = algorithm.GenerateKeyPair();
                ISigner signer   = algorithm.GenerateSigner(keyPair.Private);
                ISigner verifier = algorithm.GenerateSigner(keyPair.Public);
                signer.BlockUpdate(test, 0, test.Length);
                byte[] signature = signer.GenerateSignature();
                verifier.BlockUpdate(test, 0, test.Length);
                bool diff = !verifier.VerifySignature(signature);
            }
        }
Beispiel #10
0
        private static void Test4()
        {
            Array modes    = Enum.GetValues(typeof(MACCipherMode));
            Array paddings = Enum.GetValues(typeof(MACPaddingMode));

            byte[] test = Utilities.ScoopBytes(123);
            //
            Type type = typeof(MACHelper);

            PropertyInfo[] properties = type.GetProperties(BindingFlags.Static | BindingFlags.Public);
            foreach (PropertyInfo property in properties)
            {
                if (property.GetValue(type, null) is IMAC algorithm)
                {
                    foreach (int modeValue in modes)
                    {
                        MACCipherMode mode    = (MACCipherMode)modeValue;
                        int           keySize = GetQualitySize(algorithm.KeySizes);
                        byte[]        key     = Utilities.ScoopBytes(keySize / 8);
                        algorithm.TryGetIVSizes(mode, out KeySizes[] ivSizes);
                        int               ivSize     = GetQualitySize(ivSizes);
                        byte[]            iv         = Utilities.ScoopBytes(ivSize / 8);
                        ICipherParameters parameters = algorithm.GenerateParameters(key, iv);
                        foreach (int paddingValue in paddings)
                        {
                            _total++;
                            MACPaddingMode padding   = (MACPaddingMode)paddingValue;
                            string         mechanism = string.Format(CultureInfo.InvariantCulture, "{0}/{1}/{2}", algorithm.Mechanism, mode.ToString(), padding.ToString());
                            IMac           digest    = algorithm.GenerateDigest(mode, padding, parameters);
                            try
                            {
                                XTest(mechanism, digest, test);
                                _execute++;
                            }
                            catch (Exception)
                            {
                                Console.WriteLine("{0}-------- Ignored --------", mechanism.PadRight(32));
                            }
                        }
                    }
                }
            }
        }
Beispiel #11
0
        private static void Test1()
        {
            Array paddings = Enum.GetValues(typeof(AsymmetricPaddingMode));
            //
            List <IAsymmetricEncryptionAlgorithm> algorithms = new List <IAsymmetricEncryptionAlgorithm>();

            AsymmetricAlgorithmHelper.TryGetAlgorithm("ElGamal", out IAsymmetricEncryptionAlgorithm encryption);
            algorithms.Add(encryption);
            AsymmetricAlgorithmHelper.TryGetAlgorithm("RSA", out encryption);
            algorithms.Add(encryption);
            //
            byte[] test = Utilities.ScoopBytes(4);
            foreach (IAsymmetricEncryptionAlgorithm algorithm in algorithms)
            {
                foreach (int paddingValue in paddings)
                {
                    _total++;
                    AsymmetricPaddingMode padding = (AsymmetricPaddingMode)paddingValue;
                    string mechanism = string.Format(CultureInfo.InvariantCulture, "{0}/{1}", algorithm.Mechanism, padding.ToString());
                    try
                    {
                        AsymmetricCipherKeyPair keyPair   = algorithm.GenerateKeyPair();
                        IAsymmetricBlockCipher  encryptor = algorithm.GenerateCipher(padding, keyPair.Public);
                        IAsymmetricBlockCipher  decryptor = algorithm.GenerateCipher(padding, keyPair.Private);
                        XTest(mechanism, encryptor, decryptor, test);
                        _execute++;
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("{0}-------------------------------- Ignored.", mechanism.PadRight(32));
                    }
                }
            }
            {
                AsymmetricCipherKeyPair keyPair   = ((RSA)AsymmetricAlgorithmHelper.RSA).GenerateKeyPair(true);
                IAsymmetricBlockCipher  encryptor = AsymmetricAlgorithmHelper.RSA.GenerateCipher(AsymmetricPaddingMode.NoPadding, keyPair.Public);
                IAsymmetricBlockCipher  decryptor = AsymmetricAlgorithmHelper.RSA.GenerateCipher(AsymmetricPaddingMode.NoPadding, keyPair.Private);
                XTest(".NET RSA KEY 2048", encryptor, decryptor, test);
            }
        }
Beispiel #12
0
        private static void Test3()
        {
            byte[] test = Utilities.ScoopBytes(123);
            //
            Type type = typeof(CMACHelper);

            PropertyInfo[] properties = type.GetProperties(BindingFlags.Static | BindingFlags.Public);
            foreach (PropertyInfo property in properties)
            {
                if (property.GetValue(type, null) is ICMAC algorithm)
                {
                    _total++;
                    int               keySize    = GetQualitySize(algorithm.KeySizes);
                    byte[]            key        = Utilities.ScoopBytes(keySize / 8);
                    ICipherParameters parameters = algorithm.GenerateParameters(key);
                    IMac              digest     = algorithm.GenerateDigest(parameters);
                    XTest(algorithm.Mechanism, digest, test);
                    _execute++;
                }
            }
            Console.WriteLine();
        }
Beispiel #13
0
        private static void Test2()
        {
            byte[]            test       = Utilities.ScoopBytes(123);
            byte[]            key        = Utilities.ScoopBytes(31);
            ICipherParameters parameters = Org.BouncyCastle.Security.ParameterUtilities.CreateKeyParameter("AES", key);
            //
            Type type = typeof(HMACHelper);

            PropertyInfo[] properties = type.GetProperties(BindingFlags.Static | BindingFlags.Public);
            foreach (PropertyInfo property in properties)
            {
                if (property.GetValue(type, null) is IHMAC algorithm)
                {
                    _total++;
                    IMac digest = algorithm.GenerateDigest(parameters);
                    XTest(algorithm.Mechanism, digest, test);
                    _execute++;
                }
            }
            //
            Console.WriteLine();
        }
Beispiel #14
0
        private static void Test1()
        {
            byte[] test = Utilities.ScoopBytes(93);
            //
            Type type = typeof(SignatureAlgorithmHelper);

            PropertyInfo[] properties = type.GetProperties(BindingFlags.Static | BindingFlags.Public);
            foreach (PropertyInfo property in properties)
            {
                if (property.GetValue(type, null) is ISignatureAlgorithm algorithm)
                {
                    _total++;
                    AsymmetricCipherKeyPair keyPair = algorithm.GenerateKeyPair();
                    ISigner signer   = algorithm.GenerateSigner(keyPair.Private);
                    ISigner verifier = algorithm.GenerateSigner(keyPair.Public);
                    XTest(algorithm, signer, verifier, test);
                    _execute++;
                }
            }
            //
            List <string> names = new List <string>();

            names.AddRange(new string[] { "Ed25519ctx", "Ed448ph", "SHA3-256withRSA/ISO9796-2", "SHA1withRSA/X9.31" });
            names.AddRange(new string[] { "RIPEMD128WITHSM2", "RIPEMD160WITHSM2", "RIPEMD256WITHSM2", "RIPEMD256WITHSM2" });
            names.AddRange(new string[] { "SHA1WITHSM2", "SHA224WITHSM2", "SHA256WITHSM2", "SHA384WITHSM2", "SHA512WITHSM2" });
            foreach (string name in names)
            {
                _total++;
                _execute++;
                SignatureAlgorithmHelper.TryGetAlgorithm(name, out ISignatureAlgorithm algorithm);
                AsymmetricCipherKeyPair keyPair = algorithm.GenerateKeyPair();
                ISigner signer   = algorithm.GenerateSigner(keyPair.Private);
                ISigner verifier = algorithm.GenerateSigner(keyPair.Public);
                XTest(algorithm, signer, verifier, test);
            }
        }
Beispiel #15
0
        private static void Test1()
        {
            Array modes    = Enum.GetValues(typeof(SymmetricCipherMode));
            Array paddings = Enum.GetValues(typeof(SymmetricPaddingMode));

            byte[] test = Utilities.ScoopBytes(37);
            //
            Type type = typeof(SymmetricAlgorithmHelper);

            PropertyInfo[] properties = type.GetProperties(BindingFlags.Static | BindingFlags.Public);
            foreach (PropertyInfo property in properties)
            {
                if (property.GetValue(type, null) is IBlockAlgorithm algorithm)
                {
                    foreach (int modeValue in modes)
                    {
                        SymmetricCipherMode mode = (SymmetricCipherMode)modeValue;
                        foreach (int paddingValue in paddings)
                        {
                            _total++;
                            SymmetricPaddingMode padding = (SymmetricPaddingMode)paddingValue;
                            string mechanism             = string.Format(CultureInfo.InvariantCulture, "{0}/{1}/{2}", algorithm.Mechanism, mode.ToString(), padding.ToString());

                            if (algorithm.TryGetIVSizes(mode, padding, out KeySizes[] ivSizes))