Пример #1
0
 public ParametersWithRandom(
     CipherParameters parameters,
     SecureRandom random)
 {
     this.random     = random;
     this.parameters = parameters;
 }
Пример #2
0
        /**
         * initialise a SKIPJACK cipher.
         *
         * @param forEncryption whether or not we are for encryption.
         * @param params the parameters required to set up the cipher.
         * @exception ArgumentException if the params argument is
         * inappropriate.
         */
        public void init(
            bool encrypting,
            CipherParameters parameters)
        {
            if (!(typeof(KeyParameter).IsInstanceOfType(parameters)))
            {
                throw new ArgumentException("invalid parameter passed to SKIPJACK init - " + parameters.GetType().ToString());
            }

            byte[] keyBytes = ((KeyParameter)parameters).getKey();

            this.encrypting = encrypting;
            this.key0       = new int[32];
            this.key1       = new int[32];
            this.key2       = new int[32];
            this.key3       = new int[32];

            //
            // expand the key to 128 bytes in 4 parts (saving us a modulo, multiply
            // and an addition).
            //
            for (int i = 0; i < 32; i++)
            {
                key0[i] = keyBytes[(i * 4) % 10] & 0xff;
                key1[i] = keyBytes[(i * 4 + 1) % 10] & 0xff;
                key2[i] = keyBytes[(i * 4 + 2) % 10] & 0xff;
                key3[i] = keyBytes[(i * 4 + 3) % 10] & 0xff;
            }
        }
Пример #3
0
        public virtual void  init(bool forSigning, CipherParameters param)
        {
            RSAKeyParameters kParam = null;

            if (param is ParametersWithRandom)
            {
                ParametersWithRandom p = (ParametersWithRandom)param;

                kParam = (RSAKeyParameters)p.getParameters();
                random = p.getRandom();
            }
            else
            {
                kParam = (RSAKeyParameters)param;
                if (forSigning)
                {
                    random = new SecureRandom();
                }
            }

            cipher.init(forSigning, kParam);

            emBits = kParam.getModulus().bitLength() - 1;

            block = new byte[(emBits + 7) / 8];

            reset();
        }
Пример #4
0
        /**
         * initialise a DESede cipher.
         *
         * @param forEncryption whether or not we are for encryption.
         * @param params the parameters required to set up the cipher.
         * @exception IllegalArgumentException if the params argument is
         * inappropriate.
         */
        public override void init(
            bool encrypting,
            CipherParameters parameters)
        {
            if (!(typeof(KeyParameter).IsInstanceOfType(parameters)))
            {
                throw new ArgumentException("invalid parameter passed to DESede init - " + parameters.GetType().ToString());
            }

            byte[] keyMaster = ((KeyParameter)parameters).getKey();
            byte[] key1      = new byte[8], key2 = new byte[8], key3 = new byte[8];

            this.forEncryption = encrypting;

            if (keyMaster.Length == 24)
            {
                Array.Copy(keyMaster, 0, key1, 0, key1.Length);
                Array.Copy(keyMaster, 8, key2, 0, key2.Length);
                Array.Copy(keyMaster, 16, key3, 0, key3.Length);

                workingKey1 = generateWorkingKey(encrypting, key1);
                workingKey2 = generateWorkingKey(!encrypting, key2);
                workingKey3 = generateWorkingKey(encrypting, key3);
            }
            else                    // 16 byte key
            {
                Array.Copy(keyMaster, 0, key1, 0, key1.Length);
                Array.Copy(keyMaster, 8, key2, 0, key2.Length);

                workingKey1 = generateWorkingKey(encrypting, key1);
                workingKey2 = generateWorkingKey(!encrypting, key2);
                workingKey3 = workingKey1;
            }
        }
Пример #5
0
        public void init(
            CipherParameters parameters)
        {
            reset();

            cipher.init(true, parameters);
        }
Пример #6
0
        public void init(
            CipherParameters param)
        {
            AsymmetricKeyParameter kParam;

            if (typeof(ParametersWithRandom).IsInstanceOfType(param))
            {
                ParametersWithRandom rParam = (ParametersWithRandom)param;

                this.random = rParam.getRandom();
                kParam      = (AsymmetricKeyParameter)rParam.getParameters();
            }
            else
            {
                this.random = new SecureRandom();
                kParam      = (AsymmetricKeyParameter)param;
            }

            if (!(typeof(DHPrivateKeyParameters).IsInstanceOfType(kParam)))
            {
                throw new ArgumentException("DHEngine expects DHPrivateKeyParameters");
            }

            this.key      = (DHPrivateKeyParameters)kParam;
            this.dhParams = key.getParameters();
        }
Пример #7
0
        public void init(bool forSigning, CipherParameters param)
        {
            this.forSigning = forSigning;

            AsymmetricKeyParameter k;

            if (param is ParametersWithRandom)
            {
                k = (AsymmetricKeyParameter)((ParametersWithRandom)param).getParameters();
            }
            else
            {
                k = (AsymmetricKeyParameter)param;
            }

            if (forSigning && !k.isPrivate())
            {
                throw (new Exception("Signing Requires Private Key."));
            }

            if (!forSigning && k.isPrivate())
            {
                throw (new Exception("Verification Requires Public Key."));
            }

            reset();

            dsaSigner.init(forSigning, param);
        }
Пример #8
0
 /**
  * Sets the crypto information to sign.
  * @param privKey the private key
  * @param certChain the certificate chain
  * @param crlList the certificate revocation list. It can be <CODE>null</CODE>
  */
 public void SetSignInfo(CipherParameters privKey, X509Certificate[] certChain, object[] crlList)
 {
     pkcs = new PdfPKCS7(privKey, certChain, crlList, hashAlgorithm, PdfName.ADBE_PKCS7_SHA1.Equals(Get(PdfName.SUBFILTER)));
     pkcs.SetExternalDigest(externalDigest, externalRSAdata, digestEncryptionAlgorithm);
     if (PdfName.ADBE_X509_RSA_SHA1.Equals(Get(PdfName.SUBFILTER)))
     {
         MemoryStream bout = new MemoryStream();
         for (int k = 0; k < certChain.Length; ++k)
         {
             byte[] tmp = certChain[k].getEncoded();
             bout.Write(tmp, 0, tmp.Length);
         }
         bout.Close();
         Cert     = bout.ToArray();
         Contents = pkcs.GetEncodedPKCS1();
     }
     else
     {
         Contents = pkcs.GetEncodedPKCS7();
     }
     name = PdfPKCS7.GetSubjectFields(pkcs.SigningCertificate).GetField("CN");
     if (name != null)
     {
         Put(PdfName.NAME, new PdfString(name, PdfObject.TEXT_UNICODE));
     }
     pkcs = new PdfPKCS7(privKey, certChain, crlList, hashAlgorithm, PdfName.ADBE_PKCS7_SHA1.Equals(Get(PdfName.SUBFILTER)));
     pkcs.SetExternalDigest(externalDigest, externalRSAdata, digestEncryptionAlgorithm);
 }
Пример #9
0
        /**
         * Initialise the cipher and, possibly, the initialisation vector (IV).
         * If an IV isn't passed as part of the parameter, the IV will be all zeros.
         *
         * @param forEncryption if true the cipher is initialised for
         *  encryption, if false for decryption.
         * @param param the key and other data required by the cipher.
         * @exception IllegalArgumentException if the params argument is
         * inappropriate.
         */
        public void init(
            bool encrypting,
            CipherParameters parameters)
        //throws IllegalArgumentException
        {
            this.encrypting = encrypting;

            if (typeof(ParametersWithIV).IsInstanceOfType(parameters))
            {
                ParametersWithIV ivParam = (ParametersWithIV)parameters;
                byte[]           iv      = ivParam.getIV();

                if (iv.Length != blockSize)
                {
                    throw new ArgumentException("initialisation vector must be the same length as block size");
                }

                Array.Copy(iv, 0, IV, 0, iv.Length);

                reset();

                cipher.init(encrypting, ivParam.getParameters());
            }
            else
            {
                reset();

                cipher.init(encrypting, parameters);
            }
        }
Пример #10
0
 /**
  * initialise the RSA engine.
  *
  * @param forEncryption true if we are encrypting, false otherwise.
  * @param param the necessary RSA key parameters.
  */
 public void init(
     bool forEncryption,
     CipherParameters param)
 {
     this.key           = (RSAKeyParameters)param;
     this.forEncryption = forEncryption;
 }
Пример #11
0
        /**
         * initialise a RC5-32 cipher.
         *
         * @param forEncryption whether or not we are for encryption.
         * @param params the parameters required to set up the cipher.
         * @exception ArgumentException if the params argument is
         * inappropriate.
         */
        public void init(
            bool forEncryption,
            CipherParameters parameters)
        {
            if (typeof(RC5Parameters).IsInstanceOfType(parameters))
            {
                RC5Parameters p = (RC5Parameters)parameters;

                _noRounds = p.getRounds();

                setKey(p.getKey());
            }
            else if (typeof(KeyParameter).IsInstanceOfType(parameters))
            {
                KeyParameter p = (KeyParameter)parameters;

                setKey(p.getKey());
            }
            else
            {
                throw new ArgumentException("invalid parameter passed to RC532 init - " + parameters.GetType().ToString());
            }

            this.forEncryption = forEncryption;
        }
Пример #12
0
        /**
         * Initialise the cipher and, possibly, the initialisation vector (IV).
         * If an IV isn't passed as part of the parameter, the IV will be all zeros.
         * An IV which is too short is handled in FIPS compliant fashion.
         *
         * @param param the key and other data required by the cipher.
         * @exception ArgumentException if the params argument is
         * inappropriate.
         */
        public void init(
            CipherParameters parameters)
        //throws ArgumentException
        {
            //this.encrypting = true;

            if (typeof(ParametersWithIV).IsInstanceOfType(parameters))
            {
                ParametersWithIV ivParam = (ParametersWithIV)parameters;
                byte[]           iv      = ivParam.getIV();

                if (iv.Length < IV.Length)
                {
                    Array.Copy(iv, 0, IV, IV.Length - iv.Length, iv.Length);
                }
                else
                {
                    Array.Copy(iv, 0, IV, 0, IV.Length);
                }

                reset();

                cipher.init(true, ivParam.getParameters());
            }
            else
            {
                reset();

                cipher.init(true, parameters);
            }
        }
Пример #13
0
        public ParametersWithSalt(CipherParameters parameters, byte[] salt, int saltOff, int saltLen)
        {
            this.salt       = new byte[saltLen];
            this.parameters = parameters;

            Array.Copy(salt, saltOff, this.salt, 0, saltLen);
        }
Пример #14
0
        public override void RunImpl()
        {
            CipherParameters key = _client.RsaPair._privateKey;
            RSAEngine        rsa = new RSAEngine();

            rsa.init(false, key);

            byte[] decrypt = rsa.processBlock(Raw, 0, 128);

            if (decrypt.Length < 128)
            {
                byte[] temp = new byte[128];
                Array.Copy(decrypt, 0, temp, 128 - decrypt.Length, decrypt.Length);
                decrypt = temp;
            }

            string username = Encoding.ASCII.GetString(decrypt, 0x5e, 14).Replace("\0", string.Empty);
            string password = Encoding.ASCII.GetString(decrypt, 0x6c, 16).Replace("\0", string.Empty);

            AccountModel account = AccountService.GetAccountByLogin(username);

            if (account == null)
            {
                if (Config.Config.Instance.ServerConfig.AutoCreate)
                {
                    account = AccountService.CreateAccount(username, L2Security.HashPassword(password));
                }
                else
                {
                    _client.Send(LoginFail.ToPacket(LoginFailReason.ReasonUserOrPassWrong));
                    _client.Close();
                    return;
                }
            }
            else
            {
                if (!AccountService.CheckIfAccountIsCorrect(username, L2Security.HashPassword(password)))
                {
                    _client.Send(LoginFail.ToPacket(LoginFailReason.ReasonUserOrPassWrong));
                    _client.Close();
                    return;
                }

                if (ServerThreadPool.Instance.LoggedAlready(username.ToLower()))
                {
                    _client.Send(LoginFail.ToPacket(LoginFailReason.ReasonAccountInUse));
                    _client.Close();
                    return;
                }
            }

            Random rnd = new Random();

            _client.ActiveAccount = account;
            _client.SetLoginPair(rnd.Next(), rnd.Next());
            _client.SetPlayPair(rnd.Next(), rnd.Next());

            _client.Send(LoginOk.ToPacket(_client));
        }
Пример #15
0
        public BigInteger calculateAgreement(
            CipherParameters pubKey)
        {
            ECPublicKeyParameters pub = (ECPublicKeyParameters)pubKey;
            ECPoint P = pub.getQ().multiply(key.getD());

            // if ( p.isInfinity() ) throw new RuntimeException("d*Q == infinity");

            return(P.getX().toBigInteger());
        }
Пример #16
0
        public ParametersWithIV(
            CipherParameters parameters,
            byte[]              iv,
            int ivOff,
            int ivLen)
        {
            this.iv         = new byte[ivLen];
            this.parameters = parameters;

            Array.Copy(iv, ivOff, this.iv, 0, ivLen);
        }
Пример #17
0
        public BigInteger calculateAgreement(
            CipherParameters pubKey)
        {
            ECPublicKeyParameters pub        = (ECPublicKeyParameters)pubKey;
            ECDomainParameters    parameters = pub.getParameters();
            ECPoint P = pub.getQ().multiply(parameters.getH().multiply(key.getD()));

            // if ( p.isInfinity() ) throw new RuntimeException("Invalid public key");

            return(P.getX().toBigInteger());
        }
Пример #18
0
 /**
  * Initialise the encryptor.
  *
  * @param forEncryption whether or not this is encryption/decryption.
  * @param privParam our private key parameters
  * @param pubParam the recipient's/sender's public key parameters
  * @param param encoding and derivation parameters.
  */
 public void init(
     bool forEncryption,
     CipherParameters privParam,
     CipherParameters pubParam,
     CipherParameters param)
 {
     this.forEncryption = forEncryption;
     this.privParam     = privParam;
     this.pubParam      = pubParam;
     this.param         = (IESParameters)param;
 }
Пример #19
0
        /**
         * initialise the cipher.
         *
         * @param forEncryption if true the cipher is initialised for
         *  encryption, if false for decryption.
         * @param param the key and other data required by the cipher.
         * @exception IllegalArgumentException if the params argument is
         * inappropriate.
         */
        public virtual void init(
            bool forEncryption,
            CipherParameters parameters)
        //throws IllegalArgumentException
        {
            this.forEncryption = forEncryption;

            reset();

            cipher.init(forEncryption, parameters);
        }
Пример #20
0
        /**
         * given a short term public key from a given party calculate the next
         * message in the agreement sequence.
         */
        public BigInteger calculateAgreement(
            CipherParameters pubKey)
        {
            DHPublicKeyParameters pub = (DHPublicKeyParameters)pubKey;

            if (!pub.getParameters().Equals(dhParams))
            {
                throw new ArgumentException("Diffie-Hellman public key has wrong parameters.");
            }

            return(pub.getY().modPow(key.getX(), dhParams.getP()));
        }
Пример #21
0
        public virtual void  init(bool forSigning, CipherParameters param)
        {
            RSAKeyParameters kParam = (RSAKeyParameters)param;

            cipher.init(forSigning, kParam);

            keyBits = kParam.getModulus().bitLength();

            block = new byte[(keyBits + 7) / 8];
            mBuf  = new byte[block.Length - 1];

            reset();
        }
Пример #22
0
        /**
         * initialise an IDEA cipher.
         *
         * @param forEncryption whether or not we are for encryption.
         * @param params the parameters required to set up the cipher.
         * @exception ArgumentException if the params argument is
         * inappropriate.
         */
        public void init(
            bool forEncryption,
            CipherParameters parameters)
        {
            if (typeof(KeyParameter).IsInstanceOfType(parameters))
            {
                workingKey = generateWorkingKey(forEncryption,
                                                ((KeyParameter)parameters).getKey());
                return;
            }

            throw new ArgumentException("invalid parameter passed to IDEA init - " + parameters.GetType().ToString());
        }
Пример #23
0
        /**
         * initialise the buffer and the underlying cipher.
         *
         * @param forEncryption if true the cipher is initialised for
         *  encryption, if false for decryption.
         * @param param the key and other data required by the cipher.
         */
        public void init(
            bool forEncryption,
            CipherParameters parameters)
        {
            this.forEncryption = forEncryption;

            reset();

            cipher.init(forEncryption, parameters);

            buf    = new byte[cipher.getInputBlockSize()];
            bufOff = 0;
        }
Пример #24
0
        private int X0, X1, X2, X3;                             // registers

        /**
         * initialise a Serpent cipher.
         *
         * @param forEncryption whether or not we are for encryption.
         * @param params the parameters required to set up the cipher.
         * @exception ArgumentException if the params argument is
         * inappropriate.
         */
        public void init(
            bool encrypting,
            CipherParameters parameters)
        {
            if (typeof(KeyParameter).IsInstanceOfType(parameters))
            {
                this.encrypting = encrypting;
                this.wKey       = makeWorkingKey(((KeyParameter)parameters).getKey());
                return;
            }

            throw new ArgumentException("invalid parameter passed to Serpent init - " + parameters.GetType().ToString());
        }
Пример #25
0
        /**
         * initialise a RC5-32 cipher.
         *
         * @param forEncryption whether or not we are for encryption.
         * @param params the parameters required to set up the cipher.
         * @exception ArgumentException if the params argument is
         * inappropriate.
         */
        public void init(
            bool forEncryption,
            CipherParameters parameters)
        {
            if (!(typeof(KeyParameter).IsInstanceOfType(parameters)))
            {
                throw new ArgumentException("invalid parameter passed to RC6 init - " + parameters.GetType().ToString());
            }

            KeyParameter p = (KeyParameter)parameters;

            this.forEncryption = forEncryption;
            setKey(p.getKey());
        }
Пример #26
0
        public static EncryptedPrivateKeyInfo createEncryptedPrivateKeyInfo(
            String algorithm,
            char[] passPhrase,
            byte[] salt,
            int iterationCount,
            PrivateKeyInfo keyInfo)
        {
            if (!PBEUtil.isPBEAlgorithm(algorithm))
            {
                throw new Exception("attempt to use non-PBE algorithm with PBE EncryptedPrivateKeyInfo generation");
            }

            ASN1Encodable    parameters    = PBEUtil.generateAlgorithmParameters(algorithm, salt, iterationCount);
            CipherParameters keyParameters = PBEUtil.generateCipherParameters(algorithm, passPhrase, parameters);

            byte[] encoding = null;
            Object engine   = PBEUtil.createEngine(algorithm);

            if (engine is BufferedBlockCipher)
            {
                BufferedBlockCipher cipher = (BufferedBlockCipher)engine;

                cipher.init(true, keyParameters);

                byte[] keyBytes = keyInfo.getEncoded();

                int encLen = cipher.getOutputSize(keyBytes.Length);

                encoding = new byte[encLen];

                int off = cipher.processBytes(keyBytes, 0, keyBytes.Length, encoding, 0);

                cipher.doFinal(encoding, off);
            }
            else if (engine is StreamCipher)
            {
                StreamCipher cipher = (StreamCipher)engine;

                cipher.init(true, keyParameters);

                byte[] keyBytes = keyInfo.getEncoded();

                encoding = new byte[keyBytes.Length];

                cipher.processBytes(keyBytes, 0, keyBytes.Length, encoding, 0);
            }

            return(new EncryptedPrivateKeyInfo(new AlgorithmIdentifier(PBEUtil.getObjectIdentifier(algorithm), parameters), encoding));
        }
Пример #27
0
        public void init(bool forEncryption, CipherParameters parameters)
        //throws IllegalArgumentException
        {
            this.encrypting = forEncryption;

            if (typeof(ParametersWithIV).IsInstanceOfType(parameters))
            {
                ParametersWithIV ivParam = (ParametersWithIV)parameters;
                byte[]           iv      = ivParam.getIV();
                Array.Copy(iv, 0, IV, 0, IV.Length);

                reset();
                cipher.init(true, ivParam.getParameters());
            }
        }
Пример #28
0
        /**
         * initialise a Twofish cipher.
         *
         * @param forEncryption whether or not we are for encryption.
         * @param params the parameters required to set up the cipher.
         * @exception ArgumentException if the params argument is
         * inappropriate.
         */
        public void init(
            bool encrypting,
            CipherParameters parameters)
        {
            if (typeof(KeyParameter).IsInstanceOfType(parameters))
            {
                this.encrypting = encrypting;
                this.workingKey = ((KeyParameter)parameters).getKey();
                this.k64Cnt     = (this.workingKey.Length / 8);             // pre-padded ?
                setKey(this.workingKey);

                return;
            }

            throw new ArgumentException("invalid parameter passed to Twofish init - " + parameters.GetType().ToString());
        }
Пример #29
0
        /// <summary> Initialise the signer.
        ///
        /// </summary>
        /// <param name="forSigning">true if for signing, false if for verification.
        /// </param>
        /// <param name="param">parameters for signature generation/verification. If the
        /// parameters are for generation they should be a ParametersWithRandom,
        /// a ParametersWithSalt, or just an RSAKeyParameters object. If RSAKeyParameters
        /// are passed in a SecureRandom will be created.
        /// </param>
        /// <exception cref="ArgumentException"> IllegalArgumentException if wrong parameter type or a fixed
        /// salt is passed in which is the wrong length.
        /// </exception>
        public virtual void  init(bool forSigning, CipherParameters param)
        {
            RSAKeyParameters kParam = null;
            int lengthOfSalt        = saltLength;

            if (param is ParametersWithRandom)
            {
                ParametersWithRandom p = (ParametersWithRandom)param;

                kParam = (RSAKeyParameters)p.getParameters();
                random = p.getRandom();
            }
            else if (param is ParametersWithSalt)
            {
                ParametersWithSalt p = (ParametersWithSalt)param;

                kParam       = (RSAKeyParameters)p.getParameters();
                standardSalt = p.getSalt();
                lengthOfSalt = standardSalt.Length;
            }
            else
            {
                kParam = (RSAKeyParameters)param;
                if (forSigning)
                {
                    random = new SecureRandom();
                }
            }

            cipher.init(forSigning, kParam);

            keyBits = kParam.getModulus().bitLength();

            block = new byte[(keyBits + 7) / 8];

            if (trailer == TRAILER_IMPLICIT)
            {
                mBuf = new byte[block.Length - digest.getDigestSize() - lengthOfSalt - 1 - 1];
            }
            else
            {
                mBuf = new byte[block.Length - digest.getDigestSize() - lengthOfSalt - 1 - 2];
            }

            reset();
        }
Пример #30
0
        /**
         * initialise the ElGamal engine.
         *
         * @param forEncryption true if we are encrypting, false otherwise.
         * @param param the necessary ElGamal key parameters.
         */
        public void init(
            bool forEncryption,
            CipherParameters param)
        {
            if (typeof(ParametersWithRandom).IsInstanceOfType(param))
            {
                ParametersWithRandom p = (ParametersWithRandom)param;

                this.key    = (ElGamalKeyParameters)p.getParameters();
                this.random = p.getRandom();
            }
            else
            {
                this.key    = (ElGamalKeyParameters)param;
                this.random = new SecureRandom();
            }

            this.forEncryption = forEncryption;
        }