You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Decryptor.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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;
  16. import org.apache.poi.poifs.filesystem.DocumentInputStream;
  17. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  18. import org.apache.poi.util.LittleEndian;
  19. import javax.crypto.Cipher;
  20. import javax.crypto.CipherInputStream;
  21. import javax.crypto.SecretKey;
  22. import javax.crypto.spec.SecretKeySpec;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.nio.charset.Charset;
  26. import java.security.GeneralSecurityException;
  27. import java.security.MessageDigest;
  28. import java.security.NoSuchAlgorithmException;
  29. import java.util.Arrays;
  30. /**
  31. * @author Maxim Valyanskiy
  32. */
  33. public class Decryptor {
  34. public static final String DEFAULT_PASSWORD="VelvetSweatshop";
  35. private final EncryptionInfo info;
  36. private byte[] passwordHash;
  37. public Decryptor(EncryptionInfo info) {
  38. this.info = info;
  39. }
  40. private void generatePasswordHash(String password) throws NoSuchAlgorithmException {
  41. MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
  42. sha1.update(info.getVerifier().getSalt());
  43. byte[] hash = sha1.digest(password.getBytes(Charset.forName("UTF-16LE")));
  44. byte[] iterator = new byte[4];
  45. for (int i = 0; i<50000; i++) {
  46. sha1.reset();
  47. LittleEndian.putInt(iterator, i);
  48. sha1.update(iterator);
  49. hash = sha1.digest(hash);
  50. }
  51. passwordHash = hash;
  52. }
  53. private byte[] generateKey(int block) throws NoSuchAlgorithmException {
  54. MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
  55. sha1.update(passwordHash);
  56. byte[] blockValue = new byte[4];
  57. LittleEndian.putInt(blockValue, block);
  58. byte[] finalHash = sha1.digest(blockValue);
  59. int requiredKeyLength = info.getHeader().getKeySize()/8;
  60. byte[] buff = new byte[64];
  61. Arrays.fill(buff, (byte) 0x36);
  62. for (int i=0; i<finalHash.length; i++) {
  63. buff[i] = (byte) (buff[i] ^ finalHash[i]);
  64. }
  65. sha1.reset();
  66. byte[] x1 = sha1.digest(buff);
  67. Arrays.fill(buff, (byte) 0x5c);
  68. for (int i=0; i<finalHash.length; i++) {
  69. buff[i] = (byte) (buff[i] ^ finalHash[i]);
  70. }
  71. sha1.reset();
  72. byte[] x2 = sha1.digest(buff);
  73. byte[] x3 = new byte[x1.length + x2.length];
  74. System.arraycopy(x1, 0, x3, 0, x1.length);
  75. System.arraycopy(x2, 0, x3, x1.length, x2.length);
  76. return Arrays.copyOf(x3, requiredKeyLength);
  77. }
  78. public boolean verifyPassword(String password) throws GeneralSecurityException {
  79. generatePasswordHash(password);
  80. Cipher cipher = getCipher();
  81. byte[] verifier = cipher.doFinal(info.getVerifier().getVerifier());
  82. MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
  83. byte[] calcVerifierHash = sha1.digest(verifier);
  84. byte[] verifierHash = Arrays.copyOf(cipher.doFinal(info.getVerifier().getVerifierHash()), calcVerifierHash.length);
  85. return Arrays.equals(calcVerifierHash, verifierHash);
  86. }
  87. private Cipher getCipher() throws GeneralSecurityException {
  88. byte[] key = generateKey(0);
  89. Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
  90. SecretKey skey = new SecretKeySpec(key, "AES");
  91. cipher.init(Cipher.DECRYPT_MODE, skey);
  92. return cipher;
  93. }
  94. public InputStream getDataStream(POIFSFileSystem fs) throws IOException, GeneralSecurityException {
  95. DocumentInputStream dis = fs.createDocumentInputStream("EncryptedPackage");
  96. long size = dis.readLong();
  97. return new CipherInputStream(dis, getCipher());
  98. }
  99. }