Beispiel #1
0
 public IEnumerable <string> Generate(int count)
 {
     for (int i = 0; i < count; i++)
     {
         yield return(random.Generate());
     }
 }
Beispiel #2
0
        public void LengthConstructor1000()
        {
            CryptoRandom generator = new CryptoRandom(1000);
            int          length    = generator.Generate().Length;

            AssertValidLength(1000, length);
        }
Beispiel #3
0
        public void EmptyConstructorDefaultLength()
        {
            CryptoRandom generator = new CryptoRandom();
            int          length    = generator.Generate().Length;

            AssertValidLength(CryptoRandom.DefaultLength, length);
        }
Beispiel #4
0
        public void FormatDate()
        {
            CryptoRandom generator = new CryptoRandom("{D:MMdd}");

            for (int i = 0; i < 100; i++)
            {
                string text = generator.Generate();
                AssertValidLength(4, text.Length);
                Assert.AreEqual(DateTime.Now.ToString("MMdd"), text);
            }
        }
Beispiel #5
0
        public void FormatSpecial6()
        {
            CryptoRandom generator = new CryptoRandom("{S:12}");

            for (int i = 0; i < 100; i++)
            {
                string result = generator.Generate();
                int    length = result.Length;
                AssertValidLength(12, length);
                Console.Out.WriteLine("result = {0}", result);
            }
        }
Beispiel #6
0
        public void FormatGeneral6Lower()
        {
            CryptoRandom generator = new CryptoRandom("{g:6}");

            for (int i = 0; i < 100; i++)
            {
                string generated = generator.Generate();
                Assert.AreEqual(generated.ToLower(), generated);
                AssertValidLength(6, generated.Length);
                Console.Out.WriteLine("result = {0}", generated);
            }
        }
Beispiel #7
0
        public void FormatGeneral6()
        {
            CryptoRandom generator = new CryptoRandom("{Gg:6}");

            for (int i = 0; i < 100; i++)
            {
                string generated = generator.Generate();
                int    length    = generated.Length;
                AssertValidLength(6, length);
                Console.Out.WriteLine("result = {0}", generated);
            }
        }
Beispiel #8
0
        public void FormatCardNumber()
        {
            CryptoRandom generator = new CryptoRandom("{G:2}{N:2}-{G:4}-{G:3}-{G:4}");

            for (int i = 0; i < 100; i++)
            {
                string generated = generator.Generate();
                Assert.AreEqual(generated.ToUpper(), generated);
                Console.Out.WriteLine("result = {0}", generated);
                AssertValidLength(18, generated.Length);
            }
        }
Beispiel #9
0
        public void FormatNumeric6()
        {
            CryptoRandom generator = new CryptoRandom("{N:6}");

            for (int i = 0; i < 100; i++)
            {
                string text = generator.Generate();
                AssertValidLength(6, text.Length);
                Assert.IsTrue(ValidationProvider.IsInteger(text));
                Console.Out.WriteLine("result = {0}", text);
            }
        }
Beispiel #10
0
        public void FormatAlpha2General6()
        {
            CryptoRandom generator = new CryptoRandom("{A:2}-{G:6}");

            for (int i = 0; i < 100; i++)
            {
                string text = generator.Generate();
                AssertValidLength(9, text.Length);
                Assert.AreEqual(2, text.IndexOf('-'));
                Console.Out.WriteLine("result = {0}", text);
            }
        }
        public void SpeedTest()
        {
            List <TestClass> bigList = new List <TestClass>();
            CryptoRandom     random  = new CryptoRandom("{A:10}");

            for (int i = 0; i < 100000; i++)
            {
                bigList.Add(new TestClass(i, random.Generate(), DateTime.Now));
            }

            CodeTimer timer = CodeTimer.Start();

            MultiLevelComparer <TestClass> comparer = new MultiLevelComparer <TestClass>();

            comparer.AddColumn("Id", SortDirection.Ascending);
            comparer.AddColumn("Name", SortDirection.Descending);
            bigList.Sort(comparer);

            Console.WriteLine("Sorted in {0} ms", CodeTimer.Stop(timer).TotalMilliseconds);
        }
Beispiel #12
0
        public void NoDuplicatesIn10000()
        {
            StringDictionary dict = new StringDictionary();

            int dupes = 0;

            Parallel.For(0, 10000, i =>
            {
                CryptoRandom generator = new CryptoRandom("{D:MM}{A:9}-{G:5}");
                string number          = generator.Generate();
                if (!dict.ContainsKey(number))
                {
                    dict.Add(number, null);
                }
                else
                {
                    dupes++;
                }
            });

            Assert.AreEqual(0, dupes, "{0} duplicates found in generated text. Should be 0", dupes);
        }
Beispiel #13
0
        public void NoFormatInFormatString()
        {
            CryptoRandom generator = new CryptoRandom("NoFormat");

            Assert.AreEqual("NoFormat", generator.Generate(), "The generated text should be the same as the string passed to the format constructor.");
        }
        static void Main(string[] args)
        {
            var str = CryptoSecureString.ToSecureString(new[] { 'h', 'i', 'r', 'o' });

            char [] charArray = CryptoSecureString.CharacterData(str);

            string unsecure = CryptoSecureString.ConvertToUnsecureString(str);

            var rsaParameters = new CryptoRSAParameters();

            rsaParameters.GenerateKeys();

            Console.WriteLine($"Random {Convert.ToBase64String(CryptoRandom.Generate(32))}");

            Console.WriteLine("-------------------------------------------");

            var message      = "Hiro universe";
            var messageBytes = Encoding.UTF8.GetBytes(message);

            Console.WriteLine($"MD5 message: {message} hash: {Convert.ToBase64String(CryptoHash.Md5(messageBytes))}");
            Console.WriteLine($"SHA1 message: {message} hash: {Convert.ToBase64String(CryptoHash.Sha1(messageBytes))}");
            Console.WriteLine($"SHA256 message: {message} hash: {Convert.ToBase64String(CryptoHash.Sha256(messageBytes))}");
            Console.WriteLine($"SHA512 message: {message} hash: {Convert.ToBase64String(CryptoHash.Sha512(messageBytes))}");

            var key = CryptoRandom.Generate(32);

            Console.WriteLine($"HMAC MD5 message: {message} hash: {Convert.ToBase64String(CryptoHmac.Md5(messageBytes, key))}");
            Console.WriteLine($"HMAC SHA1 message: {message} hash: {Convert.ToBase64String(CryptoHmac.Sha1(messageBytes, key))}");
            Console.WriteLine($"HMAC SHA256 message: {message} hash: {Convert.ToBase64String(CryptoHmac.Sha256(messageBytes, key))}");
            Console.WriteLine($"HMAC SHA512 message: {message} hash: {Convert.ToBase64String(CryptoHmac.Sha512(messageBytes, key))}");

            Console.WriteLine("-------------------------------------------");
            Console.WriteLine("Passsword hash with salt");
            Console.WriteLine($"Password: {message}");
            Console.WriteLine($"Password hashed: {Convert.ToBase64String(CryptoHash.Password(messageBytes))}");

            Console.WriteLine("-------------------------------------------");
            var salt = CryptoRandom.Generate(32);

            Console.WriteLine("Passsword hash with salt - Based Key Derivation Function - PBKDF2");
            Console.WriteLine($"Password: {message}");
            Console.WriteLine($"Password hashed 100 rounds: {Convert.ToBase64String(CryptoHash.Password(messageBytes, salt, 100))}");
            Console.WriteLine($"Password hashed 1000 rounds: {Convert.ToBase64String(CryptoHash.Password(messageBytes, salt, 1000))}");
            Console.WriteLine($"Password hashed 10000 rounds: {Convert.ToBase64String(CryptoHash.Password(messageBytes, salt, 10000))}");

            Console.WriteLine("-------------------------------------------");
            var desKey = CryptoRandom.Generate(8);
            var desIv  = CryptoRandom.Generate(8);
            var desEncryptedMessage = CryptoDes.Encrypt(message, desKey, desIv);
            var desDecryptedMessage = CryptoDes.Decrypt(desEncryptedMessage, desKey, desIv);

            Console.WriteLine("DES Encryption");
            Console.WriteLine($"Text: {message}");
            Console.WriteLine($"Key: {Convert.ToBase64String(desKey)}");
            Console.WriteLine($"IV: {Convert.ToBase64String(desIv)}");
            Console.WriteLine($"Encrypted: {Convert.ToBase64String(desEncryptedMessage)}");
            Console.WriteLine($"Decrypted: {desDecryptedMessage}");

            Console.WriteLine("-------------------------------------------");
            var tripleDesKey = CryptoRandom.Generate(16);
            var tripleDesIv  = CryptoRandom.Generate(8);
            var tripleDesEncryptedMessage = CryptoTripleDes.Encrypt(message, tripleDesKey, tripleDesIv);
            var tripleDesDecryptedMessage = CryptoTripleDes.Decrypt(tripleDesEncryptedMessage, tripleDesKey, tripleDesIv);

            Console.WriteLine("Triple DES Encryption");
            Console.WriteLine($"Text: {message}");
            Console.WriteLine($"Key: {Convert.ToBase64String(tripleDesKey)}");
            Console.WriteLine($"IV: {Convert.ToBase64String(tripleDesIv)}");
            Console.WriteLine($"Encrypted: {Convert.ToBase64String(tripleDesEncryptedMessage)}");
            Console.WriteLine($"Decrypted: {tripleDesDecryptedMessage}");

            Console.WriteLine("-------------------------------------------");
            var aesKey = CryptoRandom.Generate(32);
            var aesIv  = CryptoRandom.Generate(16);
            var aesEncryptedMessage = CryptoAes.Encrypt(message, aesKey, aesIv);
            var aesDecryptedMessage = CryptoAes.Decrypt(aesEncryptedMessage, aesKey, aesIv);

            Console.WriteLine("AES Encryption");
            Console.WriteLine($"Text: {message}");
            Console.WriteLine($"Key: {Convert.ToBase64String(aesKey)}");
            Console.WriteLine($"IV: {Convert.ToBase64String(aesIv)}");
            Console.WriteLine($"Encrypted: {Convert.ToBase64String(aesEncryptedMessage)}");
            Console.WriteLine($"Decrypted: {aesDecryptedMessage}");


            Console.WriteLine("-------------------------------------------");
            var rsaEncryptedMessage = CryptoRsa.Encrypt(messageBytes, rsaParameters.publicKey);
            var rsaDecryptedMessage = CryptoRsa.Decrypt(rsaEncryptedMessage, rsaParameters.privateKey);

            Console.WriteLine("RSA Encryption");
            Console.WriteLine($"Text: {message}");
            Console.WriteLine($"Encrypted: {Convert.ToBase64String(rsaEncryptedMessage)}");
            Console.WriteLine($"Decrypted: {Encoding.Default.GetString(rsaDecryptedMessage)}");

            Console.WriteLine("-------------------------------------------");
            var hybridEncryptedPacket  = CryptoHybrid.Encrypt(message, rsaParameters.publicKey);
            var hybridDecryptedMessage = CryptoHybrid.Decrypt(hybridEncryptedPacket, rsaParameters.privateKey);

            Console.WriteLine("Hybrid Encryption using AES and RSA");
            Console.WriteLine($"Text: {message}");
            Console.WriteLine($"Encrypted: {Convert.ToBase64String(hybridEncryptedPacket.EncryptedData)}");
            Console.WriteLine($"Decrypted: {hybridDecryptedMessage}");

            Console.WriteLine("-------------------------------------------");
            var hashedMessage = CryptoHash.Sha256(messageBytes);
            var signature     = CryptoDigitalSignature.Sign(hashedMessage, rsaParameters.privateKey);
            var verify        = CryptoDigitalSignature.Verify(hashedMessage, signature, rsaParameters.publicKey);

            Console.WriteLine("Digital Signature");
            Console.WriteLine($"Text: {message}");
            Console.WriteLine($"Signature: {Convert.ToBase64String(signature)}");
            Console.WriteLine("Is Verified: " + (verify ? "true" : "false"));

            Console.WriteLine("-------------------------------------------");
            try {
                var hybridSignatureEncryptedPacket  = CryptoHybridIntegrity.Encrypt(message, rsaParameters);
                var hybridSignatureDecryptedMessage = CryptoHybridIntegrity.Decrypt(hybridSignatureEncryptedPacket, rsaParameters);
                Console.WriteLine("Hybrid encryption with digital signature");
                Console.WriteLine($"Text: {message}");
                Console.WriteLine($"Signature: {Convert.ToBase64String(hybridSignatureEncryptedPacket.Signature)}");
                Console.WriteLine($"Encrypted: {Convert.ToBase64String(hybridSignatureEncryptedPacket.EncryptedData)}");
                Console.WriteLine($"Decrypted: {hybridSignatureDecryptedMessage}");
            }
            catch (CryptographicException ex)
            {
                Console.WriteLine("Error : " + ex.Message);
            }
        }