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); 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 internal static ISecretKey GenerateSecretKey(string password, EncryptionVerifier ver, int keySize) { HashAlgorithm hashAlgo = ver.HashAlgorithm; byte[] pwHash = CryptoFunctions.HashPassword(password, hashAlgo, ver.Salt, ver.SpinCount); byte[] blockKey = new byte[4]; LittleEndian.PutInt(blockKey, 0, 0); byte[] finalHash = CryptoFunctions.GenerateKey(pwHash, hashAlgo, blockKey, hashAlgo.hashSize); byte[] x1 = FillAndXor(finalHash, (byte)0x36); byte[] x2 = FillAndXor(finalHash, (byte)0x5c); byte[] x3 = new byte[x1.Length + x2.Length]; Array.Copy(x1, 0, x3, 0, x1.Length); Array.Copy(x2, 0, x3, x1.Length, x2.Length); byte[] key = Arrays.CopyOf(x3, keySize); ISecretKey skey = new SecretKeySpec(key, ver.CipherAlgorithm.jceId); return(skey); }