Esempio n. 1
0
        private void UIActionSymEncrypt_Click(object sender, EventArgs e)
        {
            string fileContent = FileManager.ReadTextFile(pathToFile);
            string fullPath    = FileManager.RootPath + "tajni_kljuc.txt";

            string[] aes = FileManager.ReadTextFile(fullPath).Split(' ');
            byte[]   key = Convert.FromBase64String(aes[0]);
            byte[]   iv  = Convert.FromBase64String(aes[1]);
            string   encryptedContent = "";

            Aes aesKey    = AESKeyProvider.ProvideKey();
            var encryptor = aesKey.CreateEncryptor(key, iv);

            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {
                        swEncrypt.Write(fileContent);
                    }
                }
                encryptedContent = Convert.ToBase64String(msEncrypt.ToArray());
            }

            FileManager.WriteTextToFile("simetricna-enkripcija.txt", encryptedContent);

            MessageBox.Show(encryptedContent);
        }
Esempio n. 2
0
        private void UIActionSymDecrypt_Click(object sender, EventArgs e)
        {
            string fullPath = FileManager.RootPath + "tajni_kljuc.txt";

            string[] aes = FileManager.ReadTextFile(fullPath).Split(' ');
            byte[]   key = Convert.FromBase64String(aes[0]);
            byte[]   iv  = Convert.FromBase64String(aes[1]);
            string   decryptedContent = "";
            string   encryptedContent = FileManager.ReadTextFile(FileManager.RootPath + "simetricna-enkripcija.txt");
            Aes      aesKey           = AESKeyProvider.ProvideKey();
            var      decryptor        = aesKey.CreateDecryptor(key, iv);

            using (MemoryStream msDecrypt = new MemoryStream(Convert.FromBase64String(encryptedContent)))
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader swDecrypt = new StreamReader(csDecrypt))
                    {
                        decryptedContent = swDecrypt.ReadToEnd();
                    }
                }
            }
            FileManager.WriteTextToFile("simetricna-dekripcija.txt", decryptedContent);
            MessageBox.Show(decryptedContent);
        }
Esempio n. 3
0
        private void UIActionCreateKeyFiles_Click(object sender, EventArgs e)
        {
            RSAKeyGenerator rsaGenerator = new RSAKeyGenerator(RSAKeyProvider.ProvideKey());
            AESKeyGenerator aesGenerator = new AESKeyGenerator(AESKeyProvider.ProvideKey());

            rsaGenerator.GenerateKeys();
            aesGenerator.GenerateKeys();

            FileManager.WriteTextToFile("javni_kljuc.txt", rsaGenerator.PublicKey);
            FileManager.WriteTextToFile("privatni_kljuc.txt", rsaGenerator.PrivateKey);
            FileManager.WriteTextToFile("tajni_kljuc.txt", aesGenerator.SecretKey);

            UIActionOpenEncryptionDialog.Enabled = true;
        }