Exemplo n.º 1
0
        static public void EncryptFile(string sourceFilePath, string encryptFilePath, string fileKeyPath)
        {
            Console.WriteLine($"Start encrypting");
            var openKey = RSAKey.ReadFromFile(fileKeyPath);

            using (var encodeFile = File.CreateText(encryptFilePath))
            {
                var bytesFromSourceFile = File.ReadAllBytes(sourceFilePath);
                var isFirstByte         = true;
                var count = 0;
                foreach (byte @byte in bytesFromSourceFile)
                {
                    Console.WriteLine($"Byte number {count++} from {bytesFromSourceFile.Length}");
                    var code = Encrypt(new BigInteger(@byte), openKey);
                    if (!isFirstByte)
                    {
                        encodeFile.Write(Separator);
                    }
                    else
                    {
                        isFirstByte = false;
                    }
                    encodeFile.Write(code);
                }
            }
        }
Exemplo n.º 2
0
        public static void GenerateKeyPair(out RSAKey PublicKey, out RSAKey PrivateKey)
        {
            // Generate a large prime number for P
            BigInteger P = GetLargeRandomPrime();

            // generate a large prime number for Q
            BigInteger Q = GetLargeRandomPrime();

            BigInteger N = P * Q;

            BigInteger Phi = (P - 1) * (Q - 1);

            BigInteger e;

            e = 65537;

            while (GCD(e, Phi) != 1)
            {
                e = GetFirstPrime(e);
            }

            BigInteger d = ModInverse(e, Phi);

            PublicKey.N   = N;
            PublicKey.Key = e;

            PrivateKey.N   = N;
            PrivateKey.Key = d;

            Console.WriteLine("Public Key is: \n" + PublicKey.ToString("X"));
            Console.WriteLine("Private Key is: \n" + PrivateKey.ToString("X"));
        }
Exemplo n.º 3
0
        public static void Bonus2()
        {
            int start = Environment.TickCount;

            RSAKeyGenerator keyGenerator = new RSAKeyGenerator();
            RSAKey          key          = keyGenerator.GenerateRSAKeys();
            int             end          = Environment.TickCount;

            float seconds = (end - start) / 1000.0f;

            Console.WriteLine(key.ToString());
            Console.WriteLine("TOTAL   TIME = " + (end - start).ToString().PadRight(6) + " ms = " + (seconds).ToString().PadRight(5) + " sec");
        }
Exemplo n.º 4
0
        static public void DecryptFile(string encryptFilePath, string decryptFilePath, string fileKeyPath)
        {
            Console.WriteLine($"Start decrypting");
            var closeKey = RSAKey.ReadFromFile(fileKeyPath);

            using (var decodedFile = File.Create(decryptFilePath))
            {
                var encodedInformation = File.ReadAllText(encryptFilePath).Split(Separator);
                var count = 0;
                foreach (var block in encodedInformation)
                {
                    Console.WriteLine($"Byte number {count++} from {encodedInformation.Length}");
                    var decodedBlock = Decrypt(BigInteger.Parse(block), closeKey).ToByteArray()[0];
                    decodedFile.WriteByte(decodedBlock);
                }
            }
        }
Exemplo n.º 5
0
        static public void GenerateKeys(out RSAKey openKey, out RSAKey closeKey)
        {
            Console.WriteLine("Start generating keys");
            var content = File.ReadAllText("primes.txt").Split('\n');
            //var p = BigInteger.Parse(content[0]);
            //var q = BigInteger.Parse(content[1]);
            var p = GenetateRandomPrime();

            Console.WriteLine($"p is generated == {p}");
            var q = GenetateRandomPrime();

            Console.WriteLine($"q is generated == {p}");
            var n           = p * q;
            var eulerResult = (p - 1) * (q - 1);
            // For open key used fifth number Fermat.
            var e = 65537;

            Console.WriteLine($"Start generating e");
            EuclidExtended(e, eulerResult, out BigInteger d, out BigInteger temp);
            d = (d % eulerResult + eulerResult) % eulerResult;

            openKey  = new RSAKey(e, n);
            closeKey = new RSAKey(d, n);
        }
Exemplo n.º 6
0
 public static BigInteger Encrypt(BigInteger M, RSAKey EncryptionKey)
 {
     return(BigInteger.ModPow(M, EncryptionKey.Key, EncryptionKey.N));
 }
Exemplo n.º 7
0
 static private BigInteger Decrypt(BigInteger cipher, RSAKey closeKey)
 {
     return(BigInteger.ModPow(cipher, closeKey.Key, closeKey.Module));
 }
Exemplo n.º 8
0
 static private BigInteger Encrypt(BigInteger cipher, RSAKey openKey)
 {
     return(BigInteger.ModPow(cipher, openKey.Key, openKey.Module));
 }