private void bgwCrypto_DoWork(object sender, DoWorkEventArgs eventArgs)
        {
            bool encTask = (bool)eventArgs.Argument;

            if (encTask) // encryption
            {
                List <byte> bytes      = new List <byte>();
                int         block_size = (n.BitLength() - 1) / 8;
                int         nIter      = currentFileContents.Length / block_size;
                byte[]      subArr     = new byte[block_size];
                for (int i = 0; i < nIter; i++)
                {
                    Buffer.BlockCopy(currentFileContents, i * block_size, subArr, 0, block_size);
                    BigInteger m   = Util.ArrToUnsignedBigInt(ref subArr);
                    BigInteger c   = MyRSA.Encrypt(m, e, n);
                    byte[]     cip = Util.BigIntToFixedLenArr(ref c, block_size + 1);
                    bytes.AddRange(cip);
                    (sender as BackgroundWorker).ReportProgress(100 * i / nIter);
                }
                int rem = currentFileContents.Length % block_size;
                if (rem > 0)
                {
                    subArr = new byte[rem];
                    Buffer.BlockCopy(currentFileContents, nIter * block_size, subArr, 0, rem);
                    BigInteger m   = Util.ArrToUnsignedBigInt(ref subArr);
                    BigInteger c   = MyRSA.Encrypt(m, e, n);
                    byte[]     cip = Util.BigIntToFixedLenArr(ref c, block_size + 1);
                    bytes.AddRange(cip);
                }
                bytes.AddRange(Encoding.UTF8.GetBytes(new BigInteger(rem).ToString()));
                currentFileContents = bytes.ToArray();
                (sender as BackgroundWorker).ReportProgress(100);
            }
            else // decrypt
            {
                List <byte> bytes      = new List <byte>();
                int         block_size = (n.BitLength() - 1) / 8 + 1;
                int         nIter      = currentFileContents.Length / block_size;
                byte[]      subArr     = new byte[block_size];
                for (int i = 0; i < nIter - 1; i++)
                {
                    Buffer.BlockCopy(currentFileContents, i * block_size, subArr, 0, block_size);
                    BigInteger c   = Util.ArrToUnsignedBigInt(ref subArr);
                    BigInteger m   = MyRSA.Decrypt(ref c, ref d, ref n);
                    byte[]     msg = Util.BigIntToFixedLenArr(ref m, block_size - 1);
                    bytes.AddRange(msg);
                    (sender as BackgroundWorker).ReportProgress(100 * i / nIter);
                }
                {
                    int rem = currentFileContents.Length % block_size;
                    subArr = new byte[rem];
                    Buffer.BlockCopy(currentFileContents, nIter * block_size, subArr, 0, rem);
                    int lastIterByteCount = (int)BigInteger.Parse(Encoding.UTF8.GetString(subArr));
                    subArr = new byte[block_size];
                    Buffer.BlockCopy(currentFileContents, (nIter - 1) * block_size, subArr, 0, block_size);
                    BigInteger c   = Util.ArrToUnsignedBigInt(ref subArr);
                    BigInteger m   = MyRSA.Decrypt(ref c, ref d, ref n);
                    byte[]     msg = Util.BigIntToFixedLenArr(ref m, lastIterByteCount);
                    bytes.AddRange(msg);
                }

                currentFileContents = bytes.ToArray();
                (sender as BackgroundWorker).ReportProgress(100);
            }

            eventArgs.Result = encTask;
        }
Exemple #2
0
        private void btnRegister_Click(object sender, EventArgs e)
        {
            string n        = txtName.Text.Trim();
            string u        = txtUsername.Text.Trim();
            string p        = txtPassword.Text;
            string r        = txtRetype.Text;
            Regex  uPattern = new Regex("^[a-zA-Z_][a-zA-Z0-9_]*$");

            if (n.Length < 1 ||
                !uPattern.IsMatch(u) ||
                p.Length < 1 ||
                p != r)
            {
                MessageBox.Show("Please fill out form properly.", "Invalid Form Data", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            if (!Directory.Exists(Config.AppDataFolderPath))
            {
                Directory.CreateDirectory(Config.AppDataFolderPath);
            }
            if (!File.Exists(Config.AppDataFilePath))
            {
                var fs = File.Create(Config.AppDataFilePath);
                fs.Close();
            }
            if (!File.Exists(Path.Combine(Config.AppDataFolderPath, u)))
            {
                var fs = File.Create(Config.GetAppDataUserFile(u));
                fs.Close();
            }
            string[] lines = File.ReadAllLines(Config.AppDataFilePath);
            foreach (string line in lines)
            {
                if (u == line.Split(new char[] { ' ' })[0])
                {
                    MessageBox.Show("This username is already taken. Please use a different username.", "Username already taken", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
            }

            string hp = Util.Hash(p);

            BigInteger ke, kd, kn;

            MyRSA.KeyGen(Config.CryptoKeySize, out ke, out kd, out kn);
            string eStr = Convert.ToBase64String(Util.XOREncDec(ke.ToByteArray()));
            string dStr = Convert.ToBase64String(Util.XOREncDec(kd.ToByteArray()));
            string nStr = Convert.ToBase64String(Util.XOREncDec(kn.ToByteArray()));

            StringBuilder sb = new StringBuilder();

            sb.Append(u);
            sb.Append(" ");
            sb.Append(hp);
            sb.Append(" ");
            sb.Append(eStr);
            sb.Append(" ");
            sb.Append(dStr);
            sb.Append(" ");
            sb.Append(nStr);

            File.AppendAllLines(Config.AppDataFilePath, new string[] { sb.ToString() });

            if (!Directory.Exists(Path.Combine(Config.AppDataFolderPath, u)))
            {
                Directory.CreateDirectory(Path.Combine(Config.AppDataFolderPath, u));
            }

            this.Username     = u;
            this.E            = ke;
            this.D            = kd;
            this.N            = kn;
            this.DialogResult = DialogResult.OK;

            MessageBox.Show("Account created successfully. You are now logged in.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.Close();
        }