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); } }
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); }
public AgileCipherInputStream(DocumentInputStream stream, long size, IEncryptionInfoBuilder builder, AgileDecryptor decryptor) : base(stream, size, 4096, builder, decryptor) { this.builder = builder; this.decryptor = decryptor; }
public AgileCipherOutputStream(DirectoryNode dir, IEncryptionInfoBuilder builder, ISecretKey skey, AgileEncryptor encryptor) : base(dir, 4096, builder, encryptor) { this.builder = builder; this.skey = skey; this.encryptor = encryptor; }
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); }
protected ChunkedCipherInputStream( InputStream stream, long size, int chunkSize, IEncryptionInfoBuilder builder, Decryptor decryptor) : this(stream, size, chunkSize, 0, builder, decryptor) { }
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); }
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); }
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); }
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); }
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); }
internal StandardDecryptor(IEncryptionInfoBuilder builder) : base(builder) { }
protected Decryptor(IEncryptionInfoBuilder builder) { this.builder = builder; }