public static string DecryptString(string stringToDecrypt, string key)
        {
            var decryptedBytes = SuiteB.Decrypt(Encoding.UTF8.GetBytes(key),
                                                Convert.FromBase64String(stringToDecrypt));

            return(decryptedBytes != null?Encoding.UTF8.GetString(decryptedBytes) : null);
        }
        public static string EncryptString(string stringToEncrypt, string key)
        {
            var encryptedBytes =
                SuiteB.Encrypt(Encoding.UTF8.GetBytes(key), Encoding.UTF8.GetBytes(stringToEncrypt));

            return(encryptedBytes != null?Convert.ToBase64String(encryptedBytes) : null);
        }
Example #3
0
        private byte[] Encrypt(string text, string password)
        {
            var keyBytes      = text.ToBytes();
            var passwordBytes = password.ToBytes();
            var encryptedKey  = SuiteB.Encrypt(passwordBytes, new ArraySegment <byte>(keyBytes));

            return(encryptedKey);
        }
        public byte[] Unprotect(string str)
        {
            var protectedData = Convert.FromBase64String(str);
            var entropy       = Encoding.UTF8.GetBytes(_options.StorageKey);
            var data          = SuiteB.Decrypt(entropy, protectedData.AsArraySegment());

            return(data);
        }
        public string Protect(byte[] data)
        {
            var entropy       = Encoding.UTF8.GetBytes(_options.StorageKey);
            var protectedData = SuiteB.Encrypt(entropy, data.AsArraySegment());
            var str           = Convert.ToBase64String(protectedData);

            return(str);
        }
        public List <byte> Encrypt(ICollection <byte> data)
        {
            var dataArray        = data.ToArray();
            var dataArraySegment = new ArraySegment <byte>(dataArray);

            var encryptedData = SuiteB.Encrypt(_keyBytes, dataArraySegment);

            return(new List <byte>(encryptedData));
        }
        public List <byte> Decrypt(ICollection <byte> data)
        {
            var dataArray        = data.ToArray();
            var dataArraySegment = new ArraySegment <byte>(dataArray);

            var decryptedData = SuiteB.Decrypt(_keyBytes, dataArraySegment);

            if (decryptedData == null)
            {
                throw new ArgumentException("No valid data");
            }

            return(new List <byte>(decryptedData));
        }
Example #8
0
        static void Main(string[] args)
        {
            // Set colors to green on black, because everyone knows that's what hackers use
            Console.ForegroundColor = ConsoleColor.Green;
            Console.BackgroundColor = ConsoleColor.Black;
            Console.Clear();

            // Show banner
            Console.WriteLine("Developer Secrets Sender - Generation 2 (symmetric encryption with single key)");
            Console.WriteLine("Copyright (c) Michal A. Valasek - Altairis, 2016");
            Console.WriteLine(new string('-', Console.WindowWidth - 1));
            Console.WriteLine();

            // Get secret key
            Console.Write("Enter your partner's symmetric key: ");
            var keyString = Console.ReadLine();
            var key       = keyString.FromBase16();

            Console.WriteLine();

            // Get queue
            var q = GetQueue(CONNECTION_STRING, QUEUE_NAME);

            // Main loop
            Console.WriteLine("Enter message to send or empty string to quit:");
            while (true)
            {
                // Get message to send
                Console.Write("> ");
                var line = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(line))
                {
                    break;
                }

                // Encrypt message
                var encryptedData   = SuiteB.Encrypt(key, line.ToBytes().AsArraySegment());
                var encryptedString = encryptedData.ToBase16();

                // Prepare and send message
                var msg = new CloudQueueMessage(encryptedString);
                q.AddMessage(msg);

                // Display results
                Console.WriteLine("< Message sent");
            }
            Console.WriteLine("Program terminated successfully");
        }
Example #9
0
        private string Decrypt(string password, string accoutName)
        {
            byte[] encryptedAccount;
            try
            {
                encryptedAccount = File.ReadAllBytes($@".\{accoutName}.txt");
            }
            catch (Exception e)
            {
                throw new Exception("Wallet not found.");
            }


            try
            {
                var decryptedBytes = SuiteB.Decrypt(password.ToBytes(), new ArraySegment <byte>(encryptedAccount));

                return(decryptedBytes.FromBytes());
            }
            catch (Exception e)
            {
                throw new Exception("Wrong Password");
            }
        }
Example #10
0
        static void Main(string[] args)
        {
            // Set colors to green on black, because everyone knows that's what hackers use
            Console.ForegroundColor = ConsoleColor.Green;
            Console.BackgroundColor = ConsoleColor.Black;
            Console.Clear();

            // Show banner
            Console.WriteLine("Developer Secrets Receiver - Generation 3 (symmetric encryption with key derivation)");
            Console.WriteLine("Copyright (c) Michal A. Valasek - Altairis, 2016");
            Console.WriteLine(new string('-', Console.WindowWidth - 1));
            Console.WriteLine();

            // Create master symmetric key
            var rnd             = new CryptoRandom();
            var masterKey       = rnd.NextBytes(32);
            var masterKeyString = masterKey.ToBase16();

            Console.WriteLine("This is your master symmetric key: " + masterKeyString);
            Console.WriteLine("Send it to your chat partner safely!");
            Console.WriteLine();

            // Get queue
            var q = GetQueue(CONNECTION_STRING, QUEUE_NAME);

            // Main loop
            Console.WriteLine("Waiting for messages. Press SPACEBAR for pause, ESC for exit.");
            Console.WriteLine();

            uint expectedMessageNumber = 1;

            while (true)
            {
                // Wait to receive message
                var msg = q.GetMessage();
                if (msg == null)
                {
                    // No more messages
                    if (Console.KeyAvailable)
                    {
                        var keyCode = Console.ReadKey(intercept: true);
                        if (keyCode.Key == ConsoleKey.Escape)
                        {
                            break;
                        }
                        if (keyCode.Key == ConsoleKey.Spacebar)
                        {
                            Console.WriteLine();
                            Console.WriteLine("Paused, press any key to continue...");
                            Console.ReadKey(intercept: true);
                            Console.WriteLine("Waiting for messages. Press SPACEBAR for pause, ESC for exit.");
                            Console.WriteLine();
                        }
                    }
                    Thread.Sleep(250);
                    continue;
                }

                // Delete message from queue
                q.DeleteMessage(msg);

                // Parse message
                var  messageParts  = msg.AsString.Split('|');
                uint messageNumber = uint.Parse(messageParts[0]);
                var  cipherData    = messageParts[1].FromBase16();

                // Validate message number
                if (messageNumber < expectedMessageNumber)
                {
                    // Received number is too low - message is repeated or out of order
                    Console.WriteLine($"WARNING! The following message has too low serial number (expected {expectedMessageNumber}, got {messageNumber}.");
                    Console.WriteLine($"         Message is repeated or was delivered out of order.");
                }
                else if (messageNumber > expectedMessageNumber)
                {
                    // Received number is too high - some messages are missing or out of order
                    Console.WriteLine($"WARNING! The following message has too high serial number (expected {expectedMessageNumber}, got {messageNumber}.");
                    Console.WriteLine($"         Message was delivered out of order or some messages are missing.");
                    expectedMessageNumber = messageNumber + 1;
                }
                else
                {
                    // Received number is correct
                    expectedMessageNumber++;
                }

                // Derive key using HKDF algorithm
                var context    = BitConverter.GetBytes(messageNumber);
                var derivedKey = new byte[32].AsArraySegment();
                SP800_108_Ctr.DeriveKey(HMACFactories.HMACSHA256, masterKey, null, context.AsArraySegment(), derivedKey, messageNumber);

                // Authenticate message
                var authenticated = SuiteB.Authenticate(derivedKey.Array, cipherData.AsArraySegment());
                if (!authenticated)
                {
                    Console.WriteLine($"< Message #{messageNumber} ({msg.Id}) from {msg.InsertionTime:yyyy-MM-dd HH:mm:ss} was tampered with!");
                    continue;
                }

                // Decrypt message
                var plainData   = SuiteB.Decrypt(derivedKey.Array, cipherData.AsArraySegment());
                var plainString = plainData.FromBytes();

                // Display message
                Console.WriteLine($"< Message #{messageNumber} ({msg.Id}) from {msg.InsertionTime:yyyy-MM-dd HH:mm:ss}:");
                Console.WriteLine(plainString);
            }

            Console.WriteLine("Program terminated successfully.");
        }
Example #11
0
        static void Main(string[] args)
        {
            // Set colors to green on black, because everyone knows that's what hackers use
            Console.ForegroundColor = ConsoleColor.Green;
            Console.BackgroundColor = ConsoleColor.Black;
            Console.Clear();

            // Show banner
            Console.WriteLine("Developer Secrets Sender - Generation 4 (symmetric encryption with asymmetric key exchange)");
            Console.WriteLine("Copyright (c) Michal A. Valasek - Altairis, 2016");
            Console.WriteLine(new string('-', Console.WindowWidth - 1));
            Console.WriteLine();

            // Get your partner's public key
            Console.WriteLine("Enter your partner's public key:");
            var partnerPublicKeyString = Console.ReadLine();
            var partnerPublicKey       = partnerPublicKeyString.FromBase16().ToPublicKeyFromBlob();

            Console.WriteLine();

            // Create ephemeral bundle
            var ephemeralBundle = partnerPublicKey.GetSharedEphemeralDhmSecret();

            // Send ephemeral public key to partner
            var ephemeralPublicKeyString = ephemeralBundle.EphemeralDhmPublicKeyBlob.ToBase16();

            Console.WriteLine("This is your ephemeral public key:");
            Console.WriteLine(ephemeralPublicKeyString);
            Console.WriteLine("Send it to your chat partner. The adversary can read the key, but should not be able modify it.");
            Console.WriteLine();

            // Get shared secret (master key)
            var masterKey = ephemeralBundle.SharedSecret;

            Console.WriteLine("This is your master key (displayed for demonstration only): ");
            Console.WriteLine(masterKey.ToBase16());
            Console.WriteLine();

            // Get queue
            var q = GetQueue(CONNECTION_STRING, QUEUE_NAME);

            // Main loop
            Console.WriteLine("Enter message to send or empty string to quit:");
            uint messageNumber = 0;

            while (true)
            {
                // Get message to send
                Console.Write("> ");
                var line = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(line))
                {
                    break;
                }

                // Increment counter
                messageNumber++;

                // Derive key using NIST SP 800-108 algorithm in counter mode
                var context    = BitConverter.GetBytes(messageNumber);
                var derivedKey = new byte[32].AsArraySegment();
                SP800_108_Ctr.DeriveKey(HMACFactories.HMACSHA256, masterKey, null, context.AsArraySegment(), derivedKey, messageNumber);

                // Encrypt message
                var encryptedData   = SuiteB.Encrypt(derivedKey.Array, line.ToBytes().AsArraySegment());
                var encryptedString = string.Join("|", messageNumber, encryptedData.ToBase16());

                // Prepare and send message
                var msg = new CloudQueueMessage(encryptedString);
                q.AddMessage(msg);

                // Display results
                Console.WriteLine($"< Message #{messageNumber} sent");
            }
            Console.WriteLine("Program terminated successfully");
        }
Example #12
0
        static void Main(string[] args)
        {
            // Set colors to green on black, because everyone knows that's what hackers use
            Console.ForegroundColor = ConsoleColor.Green;
            Console.BackgroundColor = ConsoleColor.Black;
            Console.Clear();

            // Show banner
            Console.WriteLine("Developer Secrets Receiver - Generation 2 (symmetric encryption with single key)");
            Console.WriteLine("Copyright (c) Michal A. Valasek - Altairis, 2016");
            Console.WriteLine(new string('-', Console.WindowWidth - 1));
            Console.WriteLine();

            // Create symmetric key
            var rnd       = new CryptoRandom();
            var key       = rnd.NextBytes(32);
            var keyString = key.ToBase16();

            Console.WriteLine("This is your symmetric key: " + keyString);
            Console.WriteLine("Send it to your chat partner safely!");
            Console.WriteLine();

            // Get queue
            var q = GetQueue(CONNECTION_STRING, QUEUE_NAME);

            // Main loop
            Console.WriteLine("Waiting for messages. Press SPACEBAR for pause, ESC for exit.");
            Console.WriteLine();

            while (true)
            {
                // Wait to receive message
                var msg = q.GetMessage();
                if (msg == null)
                {
                    // No more messages
                    if (Console.KeyAvailable)
                    {
                        var keyCode = Console.ReadKey(intercept: true);
                        if (keyCode.Key == ConsoleKey.Escape)
                        {
                            break;
                        }
                        if (keyCode.Key == ConsoleKey.Spacebar)
                        {
                            Console.WriteLine();
                            Console.WriteLine("Paused, press any key to continue...");
                            Console.ReadKey(intercept: true);
                            Console.WriteLine("Waiting for messages. Press SPACEBAR for pause, ESC for exit.");
                            Console.WriteLine();
                        }
                    }
                    Thread.Sleep(250);
                    continue;
                }

                // Delete message from queue
                q.DeleteMessage(msg);

                // Authenticate message
                var cipherData    = msg.AsString.FromBase16();
                var authenticated = SuiteB.Authenticate(key, cipherData.AsArraySegment());
                if (!authenticated)
                {
                    Console.WriteLine($"< WARNING!");
                    Console.WriteLine($"< Message {msg.Id} from {msg.InsertionTime:yyyy-MM-dd HH:mm:ss} was tampered with!");
                    continue;
                }

                // Decrypt message
                var plainData   = SuiteB.Decrypt(key, cipherData.AsArraySegment());
                var plainString = plainData.FromBytes();

                // Display message
                Console.WriteLine($"< Message {msg.Id} from {msg.InsertionTime:yyyy-MM-dd HH:mm:ss}:");
                Console.WriteLine(plainString);
            }

            Console.WriteLine("Program terminated successfully.");
        }
 public static bool Authenticate(byte[] ciphertext, ArraySegment <byte>?salt = null)
 {
     return(SuiteB.Authenticate(MasterKey, new ArraySegment <byte>(ciphertext), salt));
 }
 public static byte[] Decrypt(byte[] ciphertext, ArraySegment <byte>?salt = null)
 {
     return(SuiteB.Decrypt(MasterKey, new ArraySegment <byte>(ciphertext), salt));
 }
 public static byte[] Encrypt(byte[] plaintext, ArraySegment <byte>?salt = null)
 {
     return(SuiteB.Encrypt(MasterKey, new ArraySegment <byte>(plaintext), salt));
 }
Example #16
0
        static void Main(string[] args)
        {
            // Set colors to green on black, because everyone knows that's what hackers use
            Console.ForegroundColor = ConsoleColor.Green;
            Console.BackgroundColor = ConsoleColor.Black;
            Console.Clear();

            // Show banner
            Console.WriteLine("Developer Secrets Sender - Generation 3 (symmetric encryption with key derivation)");
            Console.WriteLine("Copyright (c) Michal A. Valasek - Altairis, 2016");
            Console.WriteLine(new string('-', Console.WindowWidth - 1));
            Console.WriteLine();

            // Get secret key
            Console.Write("Enter your partner's master symmetric key: ");
            var masterKeyString = Console.ReadLine();
            var masterKey       = masterKeyString.FromBase16();

            Console.WriteLine();

            // Get queue
            var q = GetQueue(CONNECTION_STRING, QUEUE_NAME);

            // Main loop
            Console.WriteLine("Enter message to send or empty string to quit:");
            uint messageNumber = 0;

            while (true)
            {
                // Get message to send
                Console.Write("> ");
                var line = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(line))
                {
                    break;
                }

                // Increment counter
                messageNumber++;

                // Derive key using NIST SP 800-108 algorithm in counter mode
                var context    = BitConverter.GetBytes(messageNumber);
                var derivedKey = new byte[32].AsArraySegment();
                SP800_108_Ctr.DeriveKey(
                    HMACFactories.HMACSHA256,
                    masterKey,
                    null,
                    context.AsArraySegment(),
                    derivedKey,
                    messageNumber);

                // Encrypt message
                var encryptedData   = SuiteB.Encrypt(derivedKey.Array, line.ToBytes().AsArraySegment());
                var encryptedString = string.Join("|", messageNumber, encryptedData.ToBase16());

                // Prepare and send message
                var msg = new CloudQueueMessage(encryptedString);
                q.AddMessage(msg);

                // Display results
                Console.WriteLine($"< Message #{messageNumber} sent");
            }
            Console.WriteLine("Program terminated successfully");
        }