Пример #1
0
        public Hashtable createKey(FileInfo file)
        {
            Random           r                  = new Random();
            int              seed               = r.Next(350000) + 258;
            Queue <byte>     buffer             = new Queue <byte>();
            RSAByteEncrypter encypter           = new RSAByteEncrypter();
            string           decryptKeyFilePath = $"{file.FullName}.public.key";
            string           encryptKeyFilePath = $"{file.FullName}.private.key";

            var keys       = encypter.GenerateKeyPairs(seed);
            var encryptKey = keys[0];
            var decryptKey = keys[1];

            System.Diagnostics.Debug.WriteLine("======encryptKey=====" + keys[0].value);
            System.Diagnostics.Debug.WriteLine("======decryptKey=====" + keys[1].value);

            using (var fs = File.OpenWrite(decryptKeyFilePath))
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(fs, decryptKey);
            }

            using (var fs = File.OpenWrite(encryptKeyFilePath))
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(fs, encryptKey);
            }
            Hashtable ht = new Hashtable();

            ht.Add("public", decryptKeyFilePath);
            ht.Add("private", encryptKeyFilePath);
            return(ht);
        }
Пример #2
0
        public string Encrypt(FileInfo encryptFile, FileInfo keyFile)
        {
            string encryptedFilePath = $"{encryptFile.FullName}.encrypted";

            SelectedFileLength = encryptFile.Length;
            RSAByteEncrypter encypter   = new RSAByteEncrypter();
            RSAKeyPair?      encryptKey = null;

            using (var fs = keyFile.OpenRead())
            {
                BinaryFormatter bf = new BinaryFormatter();
                encryptKey = bf.Deserialize(fs) as RSAKeyPair?;
            }

            if (encryptKey != null)
            {
                Queue <byte> buffer  = new Queue <byte>();
                RSAKeyPair   keyPair = encryptKey.Value;
                var          map     = encypter.CreateByteMap(keyPair);

                using (var inStream = encryptFile.OpenRead())
                {
                    using (var reader = new BinaryReader(inStream))
                    {
                        using (var outStream = File.OpenWrite(encryptedFilePath))
                        {
                            using (var writer = new BinaryWriter(outStream))
                            {
                                for (long i = 0; i < SelectedFileLength; i++)
                                {
                                    byte b = reader.ReadByte();
                                    buffer.Enqueue(map[b]);
                                    while (buffer.Count > 0)
                                    {
                                        writer.Write(buffer.Dequeue());
                                    }
                                    buffer.Clear();
                                }
                                while (buffer.Count > 0)
                                {
                                    writer.Write(buffer.Dequeue());
                                }
                            }
                        }
                    }
                    return(encryptedFilePath);
                }
            }
            return(null);
        }
Пример #3
0
        public string Decrypt(string KeyFilePath, string EncryptedFilePath)
        {
            FileInfo file = new FileInfo(EncryptedFilePath);
            FileInfo key  = new FileInfo(KeyFilePath);

            saveFilePath = file.FullName.Replace("encrypted", "decrypted");
            SetRegularExpressions();
            EncryptedFileLength = file.Length;

            RSAKeyPair?decryptKey = null;

            using (var fs = key.OpenRead())
            {
                BinaryFormatter bf = new BinaryFormatter();
                decryptKey = bf.Deserialize(fs) as RSAKeyPair?;
            }

            string originalFileName;

            if (!ExtractFileName(file.Name, out originalFileName))
            {
                return("");
            }
            string originalExtension = ExtractExtension(originalFileName);

            if (decryptKey != null)
            {
                Queue <byte>     buffer   = new Queue <byte>();
                RSAKeyPair       keyPair  = decryptKey.Value;
                RSAByteEncrypter encypter = new RSAByteEncrypter();
                var map = encypter.CreateByteMap(keyPair);

                using (var inStream = file.OpenRead())
                {
                    using (var reader = new BinaryReader(inStream))
                    {
                        using (var outStream = File.OpenWrite(saveFilePath))
                        {
                            using (var writer = new BinaryWriter(outStream))
                            {
                                for (long i = 0; i < EncryptedFileLength; i++)
                                {
                                    byte b = reader.ReadByte();
                                    buffer.Enqueue(map[b]);
                                    while (buffer.Count > 0)
                                    {
                                        writer.Write(buffer.Dequeue());
                                    }
                                    buffer.Clear();
                                }
                                while (buffer.Count > 0)
                                {
                                    writer.Write(buffer.Dequeue());
                                }
                            }
                        }
                    }
                }
                return(saveFilePath);
            }
            return(null);
        }