示例#1
0
        public RSAKeyPair[] GenerateKeyPairs(BigInteger p, BigInteger q)
        {
            BigInteger n          = p * q;
            BigInteger pin        = (p - 1) * (q - 1);
            var        encryptKey = new RSAKeyPair();
            var        decryptKey = new RSAKeyPair();

            encryptKey.number = decryptKey.number = n;
            encryptKey.value  = CalculateEncryptionKey(pin);
            decryptKey.value  = CalculateDecryptionKey(pin, encryptKey.value);
            return(new RSAKeyPair[] { encryptKey, decryptKey });
        }
示例#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);
        }
        public IDictionary <byte, byte> CreateByteMap(RSAKeyPair keyPair)
        {
            Dictionary <byte, byte> map = new Dictionary <byte, byte>();

            for (int i = 0; i < 256; i++)
            {
                byte idx = Convert.ToByte(i);
                byte b   = ConvertByte(ref keyPair, idx);
                map.Add(idx, b);
                if (i % 16 == 0)
                {
                    Task.Delay(1);
                }
            }
            return(map);
        }
示例#4
0
 public BigInteger Encrypt(RSAKeyPair keyPair, BigInteger b)
 {
     return(BigInteger.ModPow(b, keyPair.value, keyPair.number));
 }
 private byte ConvertByte(ref RSAKeyPair keyPair, byte b)
 {
     return(Convert.ToByte((int)(RSAEncrypter.Instance.Encrypt(keyPair, b) % Prime)));
 }
示例#6
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);
        }