21xrx.com
2025-06-22 06:10:14 Sunday
登录
文章检索 我的文章 写文章
Java技巧:破解wps打开密码
2023-06-16 11:49:51 深夜i     23     0
Java技巧 破解 wps 密码 加密 解密 盐值 密匙

文章:

最近,在打开wps表格时,不管密码是否正确,都提示密码错误,无法打开。有没有什么方法可以破解这个问题呢?本文将介绍一种使用Java技巧的方法来破解wps打开密码。

首先,我们需要了解一下wps表格的解密流程。当我们输入密码后,wps会对密码进行加密后得到一个密钥,然后用这个密钥来解密文件。因此,我们需要使用Java代码来模拟这个过程。

步骤:

1. 获取文件加密的盐值和密文。

2. 通过盐值获取用于密钥加密的DES密钥,再通过密钥解密密文得到明文。

3. 将明文保存到文件中,然后打开文件,即可成功破解wps的打开密码。

在此过程中,需要注意的是,文件的盐值和密文需要通过程序获取。我将在下面的代码中介绍如何使用Java代码获取这些参数。

Java代码:

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
public class WPSDecrypt {
  private static final String DEFAULT_PASSWORD_SALT = "Kingsoft";
  private static final String DATA_STREAM_NAME = "Workbook";
  private static final byte[] PASSWORD_DEFAULT_CIPHER = Base64.decodeBase64("RXhwbG9yZXI=");
  public String decrypt(String password, String filePath) throws Exception {
    byte[] passwordSalt = DEFAULT_PASSWORD_SALT.getBytes("UTF-8");
    byte[] passwordBytes = DigestUtils.md5(password.getBytes("UTF-8"));
    DESKeySpec keySpec = new DESKeySpec(passwordBytes);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey key = keyFactory.generateSecret(keySpec);
    Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, key);
    byte[] fileData = new FileInputStream(new File(filePath)).readAllBytes();
    ByteArrayInputStream bais = new ByteArrayInputStream(fileData);
    byte[] headerBytes = new byte[76];
    bais.read(headerBytes);
    int streamOffset = readIntAt(headerBytes, 32) + 76;
    int streamSize = readIntAt(headerBytes, 40);
    byte[] encryptedDataStream = new byte[streamSize];
    bais.skip(streamOffset - 76);
    bais.read(encryptedDataStream);
    byte[] decryptedData = cipher.doFinal(encryptedDataStream);
    File outFile = new File(filePath + ".out");
    FileOutputStream fos = new FileOutputStream(outFile);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    baos.write(headerBytes);
    baos.write(decryptedData);
    fos.write(baos.toByteArray());
    fos.flush();
    fos.close();
    return outFile.getAbsolutePath();
  }
  private static int readIntAt(byte[] bytes, int offset) {
    return ((bytes[offset + 3] << 24) & 0xff000000) |
        ((bytes[offset + 2] << 16) & 0x00ff0000) |
        ((bytes[offset + 1] << 8) & 0x0000ff00) |
        (bytes[offset] & 0x000000ff);
  }
  public static void main(String[] args) throws Exception {
    WPSDecrypt wpsDecrypt = new WPSDecrypt();
    String password = "123456";         //输入密码
    String filePath = "/Users/user/wps.xlsx";  //文件路径
    String outFilePath = wpsDecrypt.decrypt(password, filePath);
    System.out.println("解密成功!解密后文件路径为:" + outFilePath);
  }
}

  
  

评论区