Esempio n. 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);
        }
Esempio n. 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);
        }
Esempio n. 3
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));
        }