コード例 #1
0
ファイル: AgileDecryptor.cs プロジェクト: IMULMUL/npoi
        protected internal static byte[] hashInput(IEncryptionInfoBuilder builder, byte[] pwHash, byte[] blockKey, byte[] inputKey, int cipherMode)
        {
            EncryptionVerifier ver  = builder.GetVerifier();
            AgileDecryptor     dec  = (AgileDecryptor)builder.GetDecryptor();
            int           keySize   = dec.GetKeySizeInBytes();
            int           blockSize = dec.GetBlockSizeInBytes();
            HashAlgorithm hashAlgo  = ver.HashAlgorithm;

            byte[] salt = ver.Salt;

            byte[]     intermedKey = CryptoFunctions.GenerateKey(pwHash, hashAlgo, blockKey, keySize);
            ISecretKey skey        = new SecretKeySpec(intermedKey, ver.CipherAlgorithm.jceId);

            byte[] iv     = CryptoFunctions.GenerateIv(hashAlgo, salt, null, blockSize);
            Cipher cipher = CryptoFunctions.GetCipher(skey, ver.CipherAlgorithm, ver.ChainingMode, iv, cipherMode);

            byte[] hashFinal;

            try {
                inputKey  = CryptoFunctions.GetBlock0(inputKey, GetNextBlockSize(inputKey.Length, blockSize));
                hashFinal = cipher.DoFinal(inputKey);
                return(hashFinal);
            } catch (Exception e) {
                throw new EncryptedDocumentException(e);
            }
        }
コード例 #2
0
ファイル: CryptoAPIDecryptor.cs プロジェクト: zjfeng/npoi
        protected internal static Cipher InitCipherForBlock(Cipher cipher, int block,
                                                            IEncryptionInfoBuilder builder, ISecretKey skey, int encryptMode)
        {
            EncryptionVerifier ver      = builder.GetVerifier();
            HashAlgorithm      hashAlgo = ver.HashAlgorithm;

            byte[] blockKey = new byte[4];
            LittleEndian.PutUInt(blockKey, 0, block);
            MessageDigest hashAlg = CryptoFunctions.GetMessageDigest(hashAlgo);

            hashAlg.Update(skey.GetEncoded());
            byte[]           encKey = hashAlg.Digest(blockKey);
            EncryptionHeader header = builder.GetHeader();
            int keyBits             = header.KeySize;

            encKey = CryptoFunctions.GetBlock0(encKey, keyBits / 8);
            if (keyBits == 40)
            {
                encKey = CryptoFunctions.GetBlock0(encKey, 16);
            }
            ISecretKey key = new SecretKeySpec(encKey, skey.GetAlgorithm());

            if (cipher == null)
            {
                cipher = CryptoFunctions.GetCipher(key, header.CipherAlgorithm, null, null, encryptMode);
            }
            else
            {
                cipher.Init(encryptMode, key);
            }
            return(cipher);
        }
コード例 #3
0
 public AgileCipherInputStream(DocumentInputStream stream, long size,
                               IEncryptionInfoBuilder builder, AgileDecryptor decryptor)
     : base(stream, size, 4096, builder, decryptor)
 {
     this.builder   = builder;
     this.decryptor = decryptor;
 }
コード例 #4
0
ファイル: AgileEncryptor.cs プロジェクト: ziyangx/npoi
 public AgileCipherOutputStream(DirectoryNode dir, IEncryptionInfoBuilder builder, ISecretKey skey, AgileEncryptor encryptor)
     : base(dir, 4096, builder, encryptor)
 {
     this.builder   = builder;
     this.skey      = skey;
     this.encryptor = encryptor;
 }
コード例 #5
0
        protected internal static Cipher InitCipherForBlock(Cipher existing, int block, bool lastChunk,
                                                            IEncryptionInfoBuilder builder, ISecretKey skey, int encryptionMode)
        {
            EncryptionHeader header = builder.GetHeader();

            if (existing == null || lastChunk)
            {
                String pAdding = (lastChunk ? "PKCS5PAdding" : "NoPAdding");
                existing = GetCipher(skey, header.CipherAlgorithm, header.ChainingMode, header.KeySalt, encryptionMode, pAdding);
            }

            byte[] blockKey = new byte[4];
            LittleEndian.PutInt(blockKey, 0, block);
            byte[] iv = GenerateIv(header.HashAlgorithm, header.KeySalt, blockKey, header.BlockSize);

            AlgorithmParameterSpec aps;

            if (header.CipherAlgorithm == CipherAlgorithm.rc2)
            {
                aps = new RC2ParameterSpec(skey.GetEncoded().Length * 8, iv);
            }
            else
            {
                aps = new IvParameterSpec(iv);
            }

            existing.Init(encryptionMode, skey, aps);
            return(existing);
        }
コード例 #6
0
 protected ChunkedCipherInputStream(
     InputStream stream,
     long size,
     int chunkSize,
     IEncryptionInfoBuilder builder,
     Decryptor decryptor)
     : this(stream, size, chunkSize, 0, builder, decryptor)
 {
 }
コード例 #7
0
 public ChunkedCipherInputStream(ILittleEndianInput stream, long size, int chunkSize
                                 , IEncryptionInfoBuilder builder, Decryptor decryptor)
     : base((Stream)stream)
 {
     _size          = size;
     this.chunkSize = chunkSize;
     chunkMask      = chunkSize - 1;
     chunkBits      = Number.BitCount(chunkMask);
     this.builder   = builder;
     this.decryptor = decryptor;
     _cipher        = InitCipherForBlock(null, 0);
 }
コード例 #8
0
        public ChunkedCipherOutputStream(DirectoryNode dir, int chunkSize, IEncryptionInfoBuilder builder, Encryptor encryptor)
            : base(null)
        {
            this.chunkSize = chunkSize;
            chunkMask      = chunkSize - 1;
            chunkBits      = Number.BitCount(chunkMask);
            _chunk         = new byte[chunkSize];

            fileOut = TempFile.CreateTempFile("encrypted_package", "crypt");
            //fileOut.DeleteOnExit();
            this.out1      = fileOut.Create();
            this.dir       = dir;
            this.builder   = builder;
            this.encryptor = encryptor;
            _cipher        = InitCipherForBlock(null, 0, false);
        }
コード例 #9
0
        protected static IEncryptionInfoBuilder GetBuilder(EncryptionMode encryptionMode)
        {
            Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
            Type       t          = null;

            foreach (Assembly assembly in assemblies)
            {
                t = assembly.GetType(encryptionMode.Builder);
                if (t != null)
                {
                    break;
                }
            }
            if (t == null)
            {
                throw new EncryptedDocumentException("Not found type " + encryptionMode.Builder);
            }
            IEncryptionInfoBuilder eib = null;

            eib = (IEncryptionInfoBuilder)t.Assembly.CreateInstance(encryptionMode.Builder);
            return(eib);
        }
コード例 #10
0
ファイル: BinaryRC4Decryptor.cs プロジェクト: zzy092/npoi
        protected internal static Cipher InitCipherForBlock(Cipher cipher, int block,
                                                            IEncryptionInfoBuilder builder, ISecretKey skey, int encryptMode)
        {
            EncryptionVerifier ver      = builder.GetVerifier();
            HashAlgorithm      hashAlgo = ver.HashAlgorithm;

            byte[] blockKey = new byte[4];
            LittleEndian.PutUInt(blockKey, 0, block);
            byte[]     encKey = CryptoFunctions.GenerateKey(skey.GetEncoded(), hashAlgo, blockKey, 16);
            ISecretKey key    = new SecretKeySpec(encKey, skey.GetAlgorithm());

            if (cipher == null)
            {
                EncryptionHeader em = builder.GetHeader();
                cipher = CryptoFunctions.GetCipher(key, em.CipherAlgorithm, null, null, encryptMode);
            }
            else
            {
                cipher.Init(encryptMode, key);
            }
            return(cipher);
        }
コード例 #11
0
        protected ChunkedCipherInputStream(
            InputStream stream,
            long size,
            int chunkSize,
            int initialPos,
            IEncryptionInfoBuilder builder,
            Decryptor decryptor)
            : base(stream)
        {
            this._size      = size;
            this._pos       = initialPos;
            this._chunkSize = chunkSize;

            this.builder   = builder;
            this.decryptor = decryptor;

            var cs = chunkSize == -1 ? 4096 : chunkSize;

            this._chunk     = IOUtils.SafelyAllocate(cs, CryptoFunctions.MAX_RECORD_LENGTH);
            this._plain     = IOUtils.SafelyAllocate(cs, CryptoFunctions.MAX_RECORD_LENGTH);
            this._chunkBits = Number.BitCount(_chunk.Length - 1);
            this._lastIndex = (int)(_pos >> _chunkBits);
            this._cipher    = InitCipherForBlock(null, _lastIndex);
        }
コード例 #12
0
ファイル: StandardDecryptor.cs プロジェクト: FistChina/npoi-1
 internal StandardDecryptor(IEncryptionInfoBuilder builder)
     : base(builder)
 {
 }
コード例 #13
0
ファイル: Decryptor.cs プロジェクト: zjfeng/npoi
 protected Decryptor(IEncryptionInfoBuilder builder)
 {
     this.builder = builder;
 }