Пример #1
0
        public override void GenerateKey(ECCurve curve)
        {
            curve.Validate();
            int keySizeInByted = curve.Prime.Length;

            KeySize = keySizeInByted * 8;

            BigInteger
                prime         = Normalize(new BigInteger(curve.Prime), s_modulus),
                subgroupOrder = Normalize(new BigInteger(curve.Order), s_modulus) / Normalize(new BigInteger(curve.Cofactor), s_modulus),
                a             = Normalize(new BigInteger(curve.A), s_modulus);

            byte[]     privateKey = new byte[keySizeInByted];
            BigInteger key;

            do
            {
                StaticRandomNumberGenerator.GetBytes(privateKey);
                key = Normalize(new BigInteger(privateKey), s_modulus);
            } while (BigInteger.Zero >= key || key >= subgroupOrder);

            var basePoint = new BigIntegerPoint(curve.G, s_modulus);

            ECPoint publicKey = BigIntegerPoint.Multiply(basePoint, key, prime, a).ToECPoint(KeySize);

            EraseData(ref _privateKey);
            _curve         = curve.Clone();
            _publicKey     = publicKey;
            _privateKey    = privateKey;
            _parametersSet = true;
        }
Пример #2
0
        public UserTestBuilder()
        {
            var firstName = RandomTestDataGenerator.GetFirstName();

            _entity = new User()
            {
                FirstName    = firstName,
                LastName     = RandomTestDataGenerator.GetLastName(),
                Email        = string.Format("{0}{1}@gmail.com", firstName, StaticRandomNumberGenerator.Next(1, 1000)),
                Address      = RandomTestDataGenerator.GetAddress(),
                City         = RandomTestDataGenerator.GetCity(),
                Country      = RandomTestDataGenerator.GetCountry(),
                Organization = RandomTestDataGenerator.GetCompany(),
                Phone        = RandomTestDataGenerator.GetPhone(),
                Tickets      = new List <Ticket>()
            };
        }
Пример #3
0
 internal static byte[] GenerateRandom(int keySize)
 {
     byte[] data = new byte[keySize];
     StaticRandomNumberGenerator.GetBytes(data);
     return(data);
 }
 public static string GetPhone()
 {
     return(string.Format("+1 {0} {1} {2}", StaticRandomNumberGenerator.Next(100, 999),
                          StaticRandomNumberGenerator.Next(100, 999), StaticRandomNumberGenerator.Next(1000, 9999)));
 }
 public static string GetAddress()
 {
     return(string.Format("{0} st. {1}", _streets.TakeRandom(), StaticRandomNumberGenerator.Next(100) + 1));
 }
        public static T TakeRandom <T>(this IEnumerable <T> list)
        {
            int rnd = StaticRandomNumberGenerator.Next(1, list.Count()) - 1;

            return(list.ToList()[rnd]);
        }
Пример #7
0
        /// <summary>
        /// Generates a digital signature for the specified hash value.
        /// </summary>
        /// <param name="hash">
        /// The hash value of the data that is being signed.
        /// </param>
        /// <returns>
        /// A digital signature that consists of the given hash value encrypted with the private key.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// The <paramref name="hash"/> parameter is <c>null</c>.
        /// </exception>
        public override byte[] SignHash(byte[] hash)
        {
            if (hash == null)
            {
                throw new ArgumentNullException(nameof(hash));
            }

            ThrowIfDisposed();

            if (KeySize / 8 != hash.Length)
            {
                throw new CryptographicException(string.Format(CultureInfo.CurrentCulture, CryptographicInvalidHashSize, KeySize / 8));
            }

            int keySizeInByted = KeySize / 8;

            if (!_parametersSet)
            {
                GenerateKey(GetDefaultCurve());
            }

            BigInteger
                subgroupOrder = Normalize(new BigInteger(_curve.Order), s_modulus) / Normalize(new BigInteger(_curve.Cofactor), s_modulus);

            BigInteger e = Normalize(new BigInteger(hash), s_modulus) % subgroupOrder;

            if (e == BigInteger.Zero)
            {
                e = BigInteger.One;
            }

            BigInteger
                prime = Normalize(new BigInteger(_curve.Prime), s_modulus),
                a = Normalize(new BigInteger(_curve.A), s_modulus),
                d = Normalize(new BigInteger(_privateKey), s_modulus),
                k, r, s;

            var rgb = new byte[keySizeInByted];

            do
            {
                do
                {
                    do
                    {
                        StaticRandomNumberGenerator.GetBytes(rgb);
                        k = Normalize(new BigInteger(rgb), s_modulus);
                    } while (k <= BigInteger.Zero || k >= subgroupOrder);

                    r = BigIntegerPoint.Multiply(new BigIntegerPoint(_curve.G, s_modulus), k, prime, a).X;
                } while (r == BigInteger.Zero);

                s = (r * d + k * e) % subgroupOrder;
            } while (s == BigInteger.Zero);

            byte[]
            signature = new byte[keySizeInByted * 2],
            array = s.ToByteArray();

            BlockCopy(array, 0, signature, 0, Min(array.Length, keySizeInByted));
            array = r.ToByteArray();
            BlockCopy(array, 0, signature, keySizeInByted, Min(array.Length, keySizeInByted));

            return(signature);
        }
Пример #8
0
        private byte[] CreatePadding(int lonelyBytes)
        {
            int padSize = 0;

            byte[] padBytes = null;

            // check the padding mode and make sure we have enough outputBuffer to handle any padding we have to do
            switch (_paddingMode)
            {
            case PaddingMode.None:
                if (lonelyBytes != 0)
                {
                    throw new CryptographicException(CryptographicInvalidDataSize);
                }
                break;

            case PaddingMode.Zeros:
                if (lonelyBytes != 0)
                {
                    padSize = InputBlockSize - lonelyBytes;
                }
                break;

            case PaddingMode.PKCS7:
#if NET45
            case PaddingMode.ANSIX923:
            case PaddingMode.ISO10126:
#endif
                padSize = InputBlockSize - lonelyBytes;
                break;
            }

            if (padSize != 0)
            {
                padBytes = new byte[padSize];

                switch (_paddingMode)
                {
                case PaddingMode.None:
                    break;

                case PaddingMode.Zeros:
                    // padBytes is already initialized with zeros
                    break;

                case PaddingMode.PKCS7:
                    for (int index = 0; index < padSize; index++)
                    {
                        padBytes[index] = (byte)padSize;
                    }
                    break;

#if NET45
                case PaddingMode.ANSIX923:
                    // padBytes is already initialized with zeros. Simply change the last byte
                    padBytes[padSize - 1] = (byte)padSize;
                    break;

                case PaddingMode.ISO10126:
                    // generate random bytes
                    StaticRandomNumberGenerator.GetBytes(padBytes);
                    // and change the last byte
                    padBytes[padSize - 1] = (byte)padSize;
                    break;
#endif
                }
            }

            return(padBytes);
        }