예제 #1
0
        public static RSAKey ReadKey(string fileName)
        {
            RSAKey     key        = new RSAKey();
            FileStream fileStream = new FileStream(fileName, FileMode.Open);

            string fileContents;

            using (StreamReader streamReader = new StreamReader(fileStream))
            {
                fileContents = streamReader.ReadToEnd();
            }

            fileStream.Close();

            Int64[] dataArray = fileContents.Split().Select(x => Int64.Parse(x)).ToArray();

            if (dataArray.Length != 2)
            {
                throw new Exception("Error reading key from file.");
            }

            key.Data = dataArray[0];
            key.N    = dataArray[1];
            return(key);
        }
예제 #2
0
        public void GenerateKeys()
        {
            random     = new Random((int)DateTime.Now.Ticks % Int32.MaxValue);
            simpleList = GenerateSimpleNumbersList(1000);

            Int64 P = GenerateRandomNumberInt16();
            Int64 Q = P;

            while (P == Q)
            {
                Q = GenerateRandomNumberInt16();
            }
            Int64 N = P * Q;
            Int64 M = (P - 1) * (Q - 1);
            Int64 E = 0;

            do
            {
                E = GenerateRandomNumberInt32();
            }while (EuclidsAlgorithm(E, M) != 1);
            Int64 D = ExtendedEuclid(E, M);

            RSAKey publicKey = new RSAKey();

            publicKey.data = E;
            publicKey.N    = N;

            RSAKey privateKey = new RSAKey();

            privateKey.data = D;
            privateKey.N    = N;

            KeyFileWriter.WriteKey(publicKey, PUBLIC_KEY_FILE_NAME);
            KeyFileWriter.WriteKey(privateKey, PRIVATE_KEY_FILE_NAME);
        }
 public static void WriteKey(RSAKey key, string fileName)
 {
     using (StreamWriter streamWriter = new StreamWriter(fileName))
     {
         streamWriter.Write("{0} {1}", key.Data, key.N);
     }
 }
예제 #4
0
        public void Decrypt(string sousreFileName, string destFileName, string keyFilename)
        {
            RSAKey     privateKey       = KeyFileReader.ReadKey(keyFilename);
            FileStream sourseFileStream = new FileStream(sousreFileName, FileMode.Open);
            FileStream destFileStream   = new FileStream(destFileName, FileMode.Create);

            using (BinaryWriter binaryWriter = new BinaryWriter(destFileStream))
            {
                using (BinaryReader binaryReader = new BinaryReader(sourseFileStream))
                {
                    while (binaryReader.BaseStream.Position != binaryReader.BaseStream.Length)
                    {
                        binaryWriter.Write(Convert.ToByte(PowModFast(binaryReader.ReadInt64(), privateKey.data, privateKey.N)));
                    }
                }
            }
            sourseFileStream.Close();
            destFileStream.Close();
        }
예제 #5
0
        public void Encrypt(string sourceFileName, string destinationFileName, string keyFileName)
        {
            RSAKey     publickey             = KeyFileReader.ReadKey(keyFileName);
            FileStream sourceFileStream      = new FileStream(sourceFileName, FileMode.Open);
            FileStream destinationFileStream = new FileStream(destinationFileName, FileMode.Create);

            using (BinaryWriter binaryWriter = new BinaryWriter(destinationFileStream))
            {
                using (BinaryReader binaryReader = new BinaryReader(sourceFileStream))
                {
                    while (binaryReader.BaseStream.Position != binaryReader.BaseStream.Length)
                    {
                        binaryWriter.Write(PowModFast(Convert.ToInt64(binaryReader.ReadByte()), publicKey.Data, publicKey.N));
                    }
                }
            }

            sourceFileStream.Close();
            destinationFileStream.Close();
        }