Пример #1
0
        public static Rsa Inject(BigInteger e, int keyLen, int certainty, byte[] publicKey)
        {
            byte[] privateData           = new byte[MontgomeryCurve25519.PrivateKeySizeInBytes];
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();

            // Генерируем секретное значение для инициализации ГПСЧ
            rng.GetBytes(privateData);

            // Создаём полезную нагрузку, шифруя seed
            byte[] payload = MontgomeryCurve25519.GetPublicKey(privateData);
            byte[] seed    = MontgomeryCurve25519.KeyExchange(publicKey, privateData);

            Rsa rsa = new Rsa(e, keyLen, certainty, new Random(seed.PackToInt()));

            Rsa.RsaParams rsap = rsa.Params;

            // Вшиваем полезную нагрузку в модуль n
            byte[] mod = rsap.n.ToByteArray();
            Replace(mod, payload, 80);
            BigInteger n = new BigInteger(mod);

            // q = NextPrime(n' / p)
            rsap.q = (n.Divide(rsap.p)).NextProbablePrime();

            if (rsap.p.CompareTo(rsap.q) < 0)   // Если q больше p, меняем их местами
            {
                BigInteger tmp = rsap.p;
                rsap.p = rsap.q;
                rsap.q = tmp;
            }

            // Заново считаем остальные параметры
            rsa.GenerateKeys(rsap.p, rsap.q);
            return(rsa);
        }
Пример #2
0
        public static Rsa Extract(BigInteger e, BigInteger mod, int certainty, byte[] privateKey)
        {
            byte[] modulus = mod.ToByteArray();
            byte[] payload = new byte[MontgomeryCurve25519.PublicKeySizeInBytes];

            // Вытаскиваем полезную нагрузку и расшифровываем seed
            Array.Copy(modulus, 80, payload, 0, 32);
            byte[] seed = MontgomeryCurve25519.KeyExchange(payload, privateKey);

            Rsa rsa = new Rsa(e, mod.BitLength, certainty, new Random(seed.PackToInt()));

            Rsa.RsaParams rsap = rsa.Params;

            // Вшиваем полезную нагрузку в модуль n
            modulus = rsap.n.ToByteArray();
            Replace(modulus, payload, 80);
            BigInteger n = new BigInteger(modulus);

            // q = NextPrime(n' / p)
            rsap.q = (n.Divide(rsap.p)).NextProbablePrime();

            if (rsap.p.CompareTo(rsap.q) < 0)   // Если q больше p, меняем их местами
            {
                BigInteger tmp = rsap.p;
                rsap.p = rsap.q;
                rsap.q = tmp;
            }

            // Заново считаем остальные параметры
            rsa.GenerateKeys(rsap.p, rsap.q);
            return(rsa);
        }
Пример #3
0
        static void BackdoorSpeedTest(int e, int keyLen, int certainty, string pubKey)
        {
            byte[]     bytes = Encoding.ASCII.GetBytes(SAMPLE);
            BigInteger var   = new BigInteger(bytes);

            Console.WriteLine("[Backdoor] <Speed> test: keyLen = {0}", keyLen);

            DateTime   start      = DateTime.Now;
            Rsa        backdoored = RsaBackdoor.Inject(BigInteger.ValueOf(e), keyLen, certainty, StrToBytes(pubKey));
            BigInteger enc        = backdoored.Encrypt(var);

            var = backdoored.Decrypt(enc);
            TimeSpan timeSpan = DateTime.Now - start;

            Console.WriteLine("Time: {0}", timeSpan.TotalMilliseconds);
            GC.Collect();
        }
Пример #4
0
        static void SpeedTest(int e, int keyLen, int certainty, int seed, bool optimize)
        {
            byte[]     bytes = Encoding.ASCII.GetBytes(SAMPLE);
            BigInteger var   = new BigInteger(bytes);

            Console.WriteLine("<Speed> test: keyLen = {0}, optimize = {1}", keyLen, optimize);

            DateTime   start = DateTime.Now;
            Rsa        rsa   = new Rsa(BigInteger.ValueOf(e), keyLen, certainty, new Random(seed), optimize);
            BigInteger enc   = rsa.Encrypt(var);

            var = rsa.Decrypt(enc);
            TimeSpan timeSpan = DateTime.Now - start;

            Console.WriteLine("Time: {0}", timeSpan.TotalMilliseconds);
            GC.Collect();
        }
Пример #5
0
        static bool BackdoorTest(int e, int keyLen, int certainty, string pubKey, string privKey)
        {
            Rsa backdoored = RsaBackdoor.Inject(BigInteger.ValueOf(e), keyLen, certainty, StrToBytes(pubKey));
            Rsa recovered  = RsaBackdoor.Extract(BigInteger.ValueOf(e), backdoored.Params.n, certainty, StrToBytes(privKey));

            byte[]     result, bytes = Encoding.ASCII.GetBytes(SAMPLE);
            BigInteger enc           = backdoored.Encrypt(new BigInteger(1, bytes));

            try
            {
                result = recovered.Decrypt(enc).ToByteArray();
            }
            catch (ArgumentOutOfRangeException)
            {
                return(false);
            }
            return(Encoding.ASCII.GetString(result) == SAMPLE);
        }
Пример #6
0
        static bool SelfTest(int e, int keyLen, int certainty, bool optimize)
        {
            Rsa rsa = new Rsa(BigInteger.ValueOf(e), keyLen, certainty, new Random(), optimize);

            byte[] result, bytes = Encoding.ASCII.GetBytes(SAMPLE);

            BigInteger enc = rsa.Encrypt(new BigInteger(1, bytes));

            try
            {
                result = rsa.Decrypt(enc).ToByteArray();
            }
            catch (ArgumentOutOfRangeException)
            {
                return(false);
            }
            return(Encoding.ASCII.GetString(result) == SAMPLE);
        }
Пример #7
0
        static bool OriginTest(int e, int keyLen, int certainty)
        {
            Rsa testRsa = new Rsa(BigInteger.ValueOf(e), keyLen, certainty, new Random(), true);
            RSACryptoServiceProvider origin = new RSACryptoServiceProvider();

            RSAParameters originParams = new RSAParameters();

            Rsa.RsaParams testParams = testRsa.Params;

            originParams.D        = testParams.d.ToByteArrayUnsigned();
            originParams.Exponent = testParams.e.ToByteArrayUnsigned();
            originParams.P        = testParams.p.ToByteArrayUnsigned();
            originParams.Q        = testParams.q.ToByteArrayUnsigned();
            originParams.Modulus  = testParams.n.ToByteArrayUnsigned();
            originParams.DP       = testParams.dP.ToByteArrayUnsigned();
            originParams.DQ       = testParams.dQ.ToByteArrayUnsigned();
            originParams.InverseQ = testParams.qInv.ToByteArrayUnsigned();

            try
            {
                origin.ImportParameters(originParams);
            }
            catch (CryptographicException)
            {
                return(false);
            }

            byte[]     testBytes = Encoding.ASCII.GetBytes(SAMPLE);
            BigInteger src       = new BigInteger(1, testBytes);

            byte[]     originEnc = origin.Encrypt(testBytes, false);
            BigInteger testEnc   = testRsa.Encrypt(src);

            byte[]     originDec = origin.Decrypt(originEnc, false);
            BigInteger testDec   = testRsa.Decrypt(testEnc);

            return(Encoding.ASCII.GetString(testDec.ToByteArray()) ==
                   Encoding.ASCII.GetString(originDec));
        }