Ejemplo n.º 1
0
        public static void Main(string[] args)
        {
            CommandLineParser cmd = CommandLineParser.Parse(args);

            Log.CreateInstance(true);

            string keyText = null;

            if (cmd["key"] != null)
            {
                keyText = cmd["key"].Value;
            }
            else
            {
                keyText = PasswordPrompt.Get("Please enter the encryption key");
                string keyText2 = PasswordPrompt.Get("Please confirm the encryption key");

                if (keyText != keyText2)
                {
                    Log.Instance.Write(Log_Severity.Fatal, "Keys did not match");
                }
            }

            if (cmd["to"] == null || cmd["from"] == null)
            {
                Log.Instance.Write(Log_Severity.Fatal, "Need arguments 'to' and 'from'");
            }

            ulong sender    = ulong.Parse(cmd["from"].Value);
            ulong recipient = ulong.Parse(cmd["to"].Value);

            var mt  = new MersenneTwister((uint)Guid.NewGuid().GetHashCode());
            var key = Encoding.UTF8.GetBytes(keyText);

            if (sender == 0)
            {
                sender = mt.NextULong();
            }

            if (recipient == 0)
            {
                recipient = mt.NextULong();
            }

            var iv   = mt.NextBytes(BLOCK_SIZE);
            var data = BitShifter.ToByte(sender).Concat(BitShifter.ToByte(recipient)).ToArray();

            BufferedBlockCipher cipher   = new CtsBlockCipher(new CbcBlockCipher(new AesEngine()));
            ICipherParameters   keyParam = new ParametersWithIV(new KeyParameter(key), iv);

            cipher.Init(true, keyParam);
            Log.Instance.Write(iv.Concat(cipher.DoFinal(data, 0, data.Length)).ToArray().ToHex());
        }
Ejemplo n.º 2
0
        public static void Main(string[] args)
        {
            CommandLineParser cmd = CommandLineParser.Parse(args);

            Log.CreateInstance(true);

            if (cmd["random"] != null)
            {
                int count = 0;
                if (!int.TryParse(cmd["random"].Value, out count))
                {
                    count = 10;
                }

                for (int i = 0; i < count; i++)
                {
                    string text   = StringHelper.GenerateRandomString(32);
                    string key    = StringHelper.GenerateRandomString(32);
                    byte[] b64    = Convert.FromBase64String(text.Encrypt(key));
                    byte[] result = Reduce(Reduce(SHA256.Create().ComputeHash(b64)));
                    Log.Instance.Write(Log_Severity.None, $"Random {i}: {result.ToHex()}");
                }

                return;
            }

            string pw = null;

            if (cmd["password"] != null)
            {
                pw = cmd["password"].Value;
            }
            else
            {
                pw = PasswordPrompt.Get();
                string pw2 = PasswordPrompt.Get("Please confirm your password");

                if (pw != pw2)
                {
                    Log.Instance.Write(Log_Severity.Fatal, "Passwords did not match");
                }
            }

            if (cmd["encrypt"] != null)
            {
                string text = cmd["encrypt"].Value;
                if (File.Exists(text))
                {
                    text = File.ReadAllText(text);
                }

                try {
                    Log.Instance.Write(Log_Severity.None, text.Encrypt(pw));
                } catch {
                    Log.Instance.Write(Log_Severity.Fatal, "Encryption failed");
                }
            }

            if (cmd["decrypt"] != null)
            {
                string text = cmd["decrypt"].Value;
                if (File.Exists(text))
                {
                    text = File.ReadAllText(text);
                }

                try {
                    Log.Instance.Write(Log_Severity.None, text.Decrypt(pw));
                } catch {
                    Log.Instance.Write(Log_Severity.Fatal, "Decryption failed");
                }
            }
        }