Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

BinaryRC4Decryptor.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.poifs.crypt.binaryrc4;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.security.GeneralSecurityException;
  19. import java.security.MessageDigest;
  20. import java.util.Arrays;
  21. import javax.crypto.Cipher;
  22. import javax.crypto.SecretKey;
  23. import javax.crypto.spec.SecretKeySpec;
  24. import org.apache.poi.EncryptedDocumentException;
  25. import org.apache.poi.poifs.crypt.*;
  26. import org.apache.poi.poifs.filesystem.DirectoryNode;
  27. import org.apache.poi.poifs.filesystem.DocumentInputStream;
  28. import org.apache.poi.util.LittleEndian;
  29. import org.apache.poi.util.StringUtil;
  30. public class BinaryRC4Decryptor extends Decryptor implements Cloneable {
  31. private long length = -1L;
  32. private int chunkSize = 512;
  33. private class BinaryRC4CipherInputStream extends ChunkedCipherInputStream {
  34. @Override
  35. protected Cipher initCipherForBlock(Cipher existing, int block)
  36. throws GeneralSecurityException {
  37. return BinaryRC4Decryptor.this.initCipherForBlock(existing, block);
  38. }
  39. public BinaryRC4CipherInputStream(DocumentInputStream stream, long size)
  40. throws GeneralSecurityException {
  41. super(stream, size, chunkSize);
  42. }
  43. public BinaryRC4CipherInputStream(InputStream stream, int size, int initialPos)
  44. throws GeneralSecurityException {
  45. super(stream, size, chunkSize, initialPos);
  46. }
  47. }
  48. protected BinaryRC4Decryptor() {
  49. }
  50. @Override
  51. public boolean verifyPassword(String password) {
  52. EncryptionVerifier ver = getEncryptionInfo().getVerifier();
  53. SecretKey skey = generateSecretKey(password, ver);
  54. try {
  55. Cipher cipher = initCipherForBlock(null, 0, getEncryptionInfo(), skey, Cipher.DECRYPT_MODE);
  56. byte encryptedVerifier[] = ver.getEncryptedVerifier();
  57. byte verifier[] = new byte[encryptedVerifier.length];
  58. cipher.update(encryptedVerifier, 0, encryptedVerifier.length, verifier);
  59. setVerifier(verifier);
  60. byte encryptedVerifierHash[] = ver.getEncryptedVerifierHash();
  61. byte verifierHash[] = cipher.doFinal(encryptedVerifierHash);
  62. HashAlgorithm hashAlgo = ver.getHashAlgorithm();
  63. MessageDigest hashAlg = CryptoFunctions.getMessageDigest(hashAlgo);
  64. byte calcVerifierHash[] = hashAlg.digest(verifier);
  65. if (Arrays.equals(calcVerifierHash, verifierHash)) {
  66. setSecretKey(skey);
  67. return true;
  68. }
  69. } catch (GeneralSecurityException e) {
  70. throw new EncryptedDocumentException(e);
  71. }
  72. return false;
  73. }
  74. @Override
  75. public Cipher initCipherForBlock(Cipher cipher, int block)
  76. throws GeneralSecurityException {
  77. return initCipherForBlock(cipher, block, getEncryptionInfo(), getSecretKey(), Cipher.DECRYPT_MODE);
  78. }
  79. protected static Cipher initCipherForBlock(Cipher cipher, int block,
  80. EncryptionInfo encryptionInfo, SecretKey skey, int encryptMode)
  81. throws GeneralSecurityException {
  82. EncryptionVerifier ver = encryptionInfo.getVerifier();
  83. HashAlgorithm hashAlgo = ver.getHashAlgorithm();
  84. byte blockKey[] = new byte[4];
  85. LittleEndian.putUInt(blockKey, 0, block);
  86. byte encKey[] = CryptoFunctions.generateKey(skey.getEncoded(), hashAlgo, blockKey, 16);
  87. SecretKey key = new SecretKeySpec(encKey, skey.getAlgorithm());
  88. if (cipher == null) {
  89. EncryptionHeader em = encryptionInfo.getHeader();
  90. cipher = CryptoFunctions.getCipher(key, em.getCipherAlgorithm(), null, null, encryptMode);
  91. } else {
  92. cipher.init(encryptMode, key);
  93. }
  94. return cipher;
  95. }
  96. protected static SecretKey generateSecretKey(String password, EncryptionVerifier ver) {
  97. if (password.length() > 255) {
  98. password = password.substring(0, 255);
  99. }
  100. HashAlgorithm hashAlgo = ver.getHashAlgorithm();
  101. MessageDigest hashAlg = CryptoFunctions.getMessageDigest(hashAlgo);
  102. byte hash[] = hashAlg.digest(StringUtil.getToUnicodeLE(password));
  103. byte salt[] = ver.getSalt();
  104. hashAlg.reset();
  105. for (int i = 0; i < 16; i++) {
  106. hashAlg.update(hash, 0, 5);
  107. hashAlg.update(salt);
  108. }
  109. hash = new byte[5];
  110. System.arraycopy(hashAlg.digest(), 0, hash, 0, 5);
  111. SecretKey skey = new SecretKeySpec(hash, ver.getCipherAlgorithm().jceId);
  112. return skey;
  113. }
  114. @Override
  115. @SuppressWarnings("resource")
  116. public ChunkedCipherInputStream getDataStream(DirectoryNode dir) throws IOException,
  117. GeneralSecurityException {
  118. DocumentInputStream dis = dir.createDocumentInputStream(DEFAULT_POIFS_ENTRY);
  119. length = dis.readLong();
  120. return new BinaryRC4CipherInputStream(dis, length);
  121. }
  122. @Override
  123. public InputStream getDataStream(InputStream stream, int size, int initialPos)
  124. throws IOException, GeneralSecurityException {
  125. return new BinaryRC4CipherInputStream(stream, size, initialPos);
  126. }
  127. @Override
  128. public long getLength() {
  129. if (length == -1L) {
  130. throw new IllegalStateException("Decryptor.getDataStream() was not called");
  131. }
  132. return length;
  133. }
  134. @Override
  135. public void setChunkSize(int chunkSize) {
  136. this.chunkSize = chunkSize;
  137. }
  138. @Override
  139. public BinaryRC4Decryptor clone() throws CloneNotSupportedException {
  140. return (BinaryRC4Decryptor)super.clone();
  141. }
  142. }