예제 #1
0
        /**
         * Initialize a new cipher object with the given cipher properties
         * If the given algorithm is not implemented in the JCE, it will try to load it from the bouncy castle
         * provider.
         *
         * @param key the secrect key
         * @param cipherAlgorithm the cipher algorithm
         * @param chain the chaining mode
         * @param vec the Initialization vector (IV), can be null
         * @param cipherMode Cipher.DECRYPT_MODE or Cipher.ENCRYPT_MODE
         * @param pAdding
         * @return the requested cipher
         * @throws GeneralSecurityException
         * @throws EncryptedDocumentException if the Initialization failed or if an algorithm was specified,
         *   which depends on a missing bouncy castle provider
         */
        public static Cipher GetCipher(IKey key, CipherAlgorithm cipherAlgorithm, ChainingMode chain, byte[] vec, int cipherMode, String pAdding)
        {
            int keySizeInBytes = key.GetEncoded().Length;

            if (pAdding == null)
            {
                pAdding = "NoPAdding";
            }

            try {
                // Ensure the JCE policies files allow for this sized key
                if (Cipher.GetMaxAllowedKeyLength(cipherAlgorithm.jceId) < keySizeInBytes * 8)
                {
                    throw new EncryptedDocumentException("Export Restrictions in place - please install JCE Unlimited Strength Jurisdiction Policy files");
                }

                Cipher cipher;
                if (cipherAlgorithm == CipherAlgorithm.rc4)
                {
                    cipher = Cipher.GetInstance(cipherAlgorithm.jceId);
                }
                else if (cipherAlgorithm.needsBouncyCastle)
                {
                    registerBouncyCastle();
                    cipher = Cipher.GetInstance(cipherAlgorithm.jceId + "/" + chain.jceId + "/" + pAdding, "BC");
                }
                else
                {
                    cipher = Cipher.GetInstance(cipherAlgorithm.jceId + "/" + chain.jceId + "/" + pAdding);
                }

                if (vec == null)
                {
                    cipher.Init(cipherMode, key);
                }
                else
                {
                    AlgorithmParameterSpec aps;
                    if (cipherAlgorithm == CipherAlgorithm.rc2)
                    {
                        aps = new RC2ParameterSpec(key.GetEncoded().Length * 8, vec);
                    }
                    else
                    {
                        aps = new IvParameterSpec(vec);
                    }
                    cipher.Init(cipherMode, key, aps);
                }
                return(cipher);
            } catch (Exception e) {
                throw new EncryptedDocumentException(e);
            }
        }
예제 #2
0
파일: Cipher.cs 프로젝트: IMULMUL/npoi
        public void Init(int cipherMode, IKey key, AlgorithmParameterSpec aps)
        {
            ICipherParameters cp;

            if (aps is RC2ParameterSpec)
            {
                cp = new RC2Parameters(key.GetEncoded(), (aps as RC2ParameterSpec).GetEffectiveKeyBits());
            }
            else if (aps is IvParameterSpec)
            {
                cp = new KeyParameter(key.GetEncoded());
                cp = new ParametersWithIV(cp, (aps as IvParameterSpec).GetIV());
            }
            else
            {
                throw new NotImplementedException();
            }
            cipherImpl.Init(cipherMode == ENCRYPT_MODE, cp);
        }
예제 #3
0
        public static byte[] CalculateVISLegacyPinBlockCVN_10_18(String newPin, IKey deaKey)
        {
            byte[] block1 = Formatting.HexStringToByteArray(new String(FormatPINBlock(newPin, 0x0)));
            byte[] block2 = new byte[8];
            Array.Copy(deaKey.GetEncoded(), 4, block2, 4, 4);
            byte[] pinBlock = Formatting.Xor(block1, block2);
            byte   length   = (byte)pinBlock.Length;

            pinBlock = Formatting.ConcatArrays(new byte[] { length }, pinBlock);
            pinBlock = EMVDESSecurity.PaddingISO9797Method2(pinBlock);
            return(pinBlock);
        }
예제 #4
0
        public static byte[] ExtractDESKeyMaterial(short keyLength, IKey clearDESKey)
        {
            String keyAlg    = clearDESKey.GetAlgorithm();
            String keyFormat = clearDESKey.GetFormat();

            if (keyFormat.CompareTo("RAW") != 0)
            {
                throw new Exception("Unsupported DES key encoding format: " + keyFormat);
            }
            if (!keyAlg.StartsWith(ALG_DES))
            {
                throw new Exception("Unsupported key algorithm: " + keyAlg);
            }
            byte[] clearKeyBytes = clearDESKey.GetEncoded();
            clearKeyBytes = Util.Trim(clearKeyBytes, GetBytesLength(keyLength));
            return(clearKeyBytes);
        }
예제 #5
0
        private static byte[] DoCryptStuff(byte[] data, IKey key, CipherDirection direction, CipherMode cipherMode, byte[] iv)
        {
            byte[] result;
            String transformation = key.GetAlgorithm();

            if (key.GetAlgorithm().StartsWith(ALG_DES))
            {
                transformation += "/" + ModetoString(cipherMode) + "/" + DES_NO_PADDING;
            }

            ICipherParameters keyparam = new KeyParameter(key.GetEncoded());
            IBufferedCipher   cipher   = CipherUtilities.GetCipher(transformation);

            if (cipherMode != CipherMode.ECB)
            {
                keyparam = new ParametersWithIV(keyparam, iv);
            }

            byte[] output = new byte[cipher.GetOutputSize(data.Length)];
            cipher.Init(direction == CipherDirection.ENCRYPT_MODE ? true : false, keyparam);
            result = cipher.DoFinal(data);

            if (cipherMode != CipherMode.ECB)
            {
                Array.Copy(result, result.Length - 8, iv, 0, iv.Length);
            }

            //AlgorithmParameterSpec aps = null;
            //try
            //{
            //    Cipher c1 = Cipher.getInstance(transformation, provider.getName());
            //    if (cipherMode != CipherMode.ECB)
            //        aps = new IvParameterSpec(iv);
            //    c1.init(direction, key, aps);
            //    result = c1.doFinal(data);
            //    if (cipherMode != CipherMode.ECB)
            //        System.arraycopy(result, result.length - 8, iv, 0, iv.length);
            //}
            //catch (Exception e)
            //{
            //    throw e;
            //}
            return(result);
        }
예제 #6
0
파일: Cipher.cs 프로젝트: IMULMUL/npoi
        public void Init(int cipherMode, IKey key)
        {
            ICipherParameters cp = new RC2Parameters(key.GetEncoded());

            cipherImpl.Init(cipherMode == ENCRYPT_MODE, cp);
        }