/// <summary>
 /// CryptographyRSA constructor
 /// </summary>
 /// <param name="keySize"></param>
 /// <param name="OAEP"></param>
 public CryptographyRSA(BitsEnum bits, bool oAEP = false)
 {
     this.Bits            = bits;
     this.ServiceProvider = new RSACryptoServiceProvider((int)this.Bits);
     this.PrivateKey      = this.ServiceProvider.ExportParameters(true);
     this.PublicKey       = this.ServiceProvider.ExportParameters(false);
     this.OAEP            = oAEP;
 }
        /// <summary>
        /// GenerateKey
        /// </summary>
        /// <param name="bits"></param>
        /// <returns>byte[BitsEnum / 8]</returns>
        public static string GenerateKey(BitsEnum bits = BitsEnum.bit256)
        {
            var bytes = new byte[(int)bits / 8];

            using (var crypto = new RNGCryptoServiceProvider())
            {
                crypto.GetBytes(bytes);
            }
            return(Convert.ToBase64String(bytes));
        }
        /// <summary>
        /// CryptographyRSA constructor
        /// </summary>
        /// <param name="privateKey"></param>
        /// <param name="publicKey"></param>
        /// <param name="OAEP"></param>
        public CryptographyRSA(RSAParameters privateKey, RSAParameters publicKey, bool oAEP = false)
        {
            if (privateKey.Modulus.Length != publicKey.Modulus.Length)
            {
                throw new ArgumentException("Private key size differs from public key!");
            }

            this.Bits            = (BitsEnum)(privateKey.Modulus.Length * 8);
            this.ServiceProvider = new RSACryptoServiceProvider((int)this.Bits);
            this.PrivateKey      = privateKey;
            this.PublicKey       = publicKey;
            this.OAEP            = oAEP;
        }
        /// <summary>
        /// Cryptography AES constructor
        /// </summary>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <param name="bits"></param>
        public CryptographyAES(string key, byte[] iv = null, BitsEnum bits = BitsEnum.bit256)
        {
            int numberKeyBytes = ((int)bits) / 8;
            int numberIVBytes  = 16;

            if (iv != null && iv.Length != numberIVBytes)
            {
                throw new ArgumentException($"The IV must contain {numberIVBytes} positions.");
            }

            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentException("The key can not null or empty.");
            }
            if (key.Length < numberKeyBytes)
            {
                throw new ArgumentException($"Key must contain {numberKeyBytes} or more positions.");
            }
            if (!key.Any(c => char.IsDigit(c)))
            {
                throw new ArgumentException("The key must contain at least one digit.");
            }
            if (!key.Any(c => char.IsUpper(c)))
            {
                throw new ArgumentException("The key must contain at least one upperrcase letter.");
            }
            if (!key.Any(c => char.IsLower(c)))
            {
                throw new ArgumentException("The key must contain at least one lowercase letter.");
            }

            this.IV          = (iv ?? (new byte[16] {
                0x12, 0x23, 0x45, 0x56, 0x78, 0xff, 0xab, 0x89, 0xFF, 0x74, 0x19, 0x33, 0x53, 0x4C, 0xA7, 0xB3
            }));
            this.Key         = key;
            this.Bits        = bits;
            this.Initialized = !(this.IV == null && string.IsNullOrEmpty(this.Key) && !Enum.IsDefined(typeof(BitsEnum), this.Bits));
        }
Exemple #5
0
 public BitsEnum(KaitaiStream p__io, KaitaiStruct p__parent = null, BitsEnum p__root = null) : base(p__io)
 {
     m_parent = p__parent;
     m_root   = p__root ?? this;
     _read();
 }