コード例 #1
0
        public void Decrypt()
        {
            AesCryptoServiceProvider cryptoServiceProvider = new AesCryptoServiceProvider();

            cryptoServiceProvider.Mode      = CipherMode.CBC;
            cryptoServiceProvider.Padding   = PaddingMode.ANSIX923;
            cryptoServiceProvider.BlockSize = 128;
            cryptoServiceProvider.KeySize   = 256;

            RSACrypter.Instance().Decrypt(keyPath);
            var decryptedKeypath = Path.GetFileName(keyPath).Split(".")[0].Split("_")[0] + "_DECRYPTED.txt";

            string[] lines = File.ReadAllLines(decryptedKeypath);
            byte[]   iv    = Convert.FromBase64String(lines[0]);
            byte[]   key   = Convert.FromBase64String(lines[1]);
            cryptoServiceProvider.IV  = iv;
            cryptoServiceProvider.Key = key;

            ICryptoTransform transform = cryptoServiceProvider.CreateEncryptor();

            transform = cryptoServiceProvider.CreateDecryptor();

            byte[] encryptedBytes = Convert.FromBase64String(File.ReadAllText(filePath));
            byte[] decryptedBytes = transform.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);
            string decryptedText  = Encoding.UTF8.GetString(decryptedBytes);

            string pureName  = Path.GetFileName(filePath).Split("_")[0];
            string extension = Path.GetFileName(filePath).Split(".")[1];

            File.WriteAllText($"{pureName}_DECRYPTED.{extension}", decryptedText);

            Console.WriteLine("done");
        }
コード例 #2
0
        public void Encrypt()
        {
            AesCryptoServiceProvider cryptoServiceProvider = new AesCryptoServiceProvider();

            cryptoServiceProvider.Mode      = CipherMode.CBC;
            cryptoServiceProvider.Padding   = PaddingMode.ANSIX923;
            cryptoServiceProvider.BlockSize = 128;
            cryptoServiceProvider.KeySize   = 256;

            string[] lines = File.ReadAllLines(keyPath);
            byte[]   iv    = Convert.FromBase64String(lines[0]);
            byte[]   key   = Convert.FromBase64String(lines[1]);
            cryptoServiceProvider.IV  = iv;
            cryptoServiceProvider.Key = key;

            string textToEncrypt = File.ReadAllText(filePath);
            string pureName      = Path.GetFileName(filePath).Split(".")[0];
            string extension     = Path.GetFileName(filePath).Split(".")[1];

            Console.WriteLine(pureName);

            ICryptoTransform transform = cryptoServiceProvider.CreateEncryptor();

            byte[] encryptedBytes =
                transform.TransformFinalBlock(Encoding.UTF8.GetBytes(textToEncrypt), 0, textToEncrypt.Length);

            string encryptedText = Convert.ToBase64String(encryptedBytes);

            File.WriteAllText($"{pureName}_ENCRYPTED.{extension}", encryptedText);

            RSACrypter.Instance().Encrypt(keyPath);
        }
コード例 #3
0
 public static RSACrypter Instance()
 {
     if (_instance == null)
     {
         _instance = new RSACrypter();
     }
     return(_instance);
 }
コード例 #4
0
 private void AsymetricDropPanel_OnDrop(object sender, DragEventArgs e)
 {
     if (e.Data.GetDataPresent(DataFormats.FileDrop))
     {
         string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
         RSACrypter.Instance().SetFileBeingEcryptedPath(files[0]);
         AsymetricFileNameText.Text = Path.GetFileName(files[0]);
     }
 }
コード例 #5
0
 private void AsymetricDecrypt_OnClick(object sender, RoutedEventArgs e)
 {
     RSACrypter.Instance().Decrypt();
 }