java – PBE:在尝试解密之前验证密码

我正在用Java创建一个应用程序,我希望允许用户使用他们选择的密码加密文件(或文件夹 – 我将目录压缩).我目前有以下方法:

static Cipher createCipher(int mode, String password) throws Exception {
            PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
            SecretKey key = keyFactory.generateSecret(keySpec);
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update("input".getBytes());
            byte[] digest = md.digest();
            byte[] salt = new byte[8];
            for (int i = 0; i < 8; ++i)
              salt[i] = digest[i];
            PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 20);
            Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
            cipher.init(mode, key, paramSpec);
            return cipher;
    }

     static void applyCipher(String inFile, String outFile, Cipher cipher) throws Exception {
            String decryption = "";
            CipherInputStream in = new CipherInputStream(new FileInputStream(inFile), cipher);
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outFile));
            int BUFFER_SIZE = 8;
            byte[] buffer = new byte[BUFFER_SIZE];
            int numRead = 0;
            do {
              numRead = in.read(buffer);
              System.out.println(buffer + ", 0, " + numRead);
              if (numRead > 0){
                out.write(buffer, 0, numRead);
                System.out.println(toHexString(buffer, 0, numRead));
              }
             } while (numRead == 8);
            in.close();
            out.flush();
            out.close();
          }
     private static char[] hex_table = {
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
            'a', 'b', 'c', 'd', 'e', 'f'};

     public static String toHexString(byte[] data, int offset, int length)
     {
       StringBuffer s = new StringBuffer(length*2);
       int end = offset+length;

       for (int i = offset; i < end; i++)
       {
         int high_nibble = (data[i] & 0xf0) >>> 4;
         int low_nibble = (data[i] & 0x0f);
         s.append(hex_table[high_nibble]);
         s.append(hex_table[low_nibble]);
       }

       return s.toString();
     }

但是,为了使程序更加用户友好,我希望能够在生成文件之前检查用户是否输入了正确的密码.我不想“把钥匙留在门垫下面”或完全取消安全等等 – 我只是想防止在用户输入错误的密码时生成错误的文件…

任何想法将不胜感激.如果您需要更多详情,请不要犹豫.

提前致谢

解决方法:

使用PBKDF2WithHmacSHA1而不是PBEWithMD5AndDES.后来的用户有两种不同的过时原语.前者是现行标准.

你有两个选择

>快但不太安全:
在加密文件的开头加上一个简短的已知值,或者在相同的密码下加密完全不同的短文件.解密此文件时,请检查已知值.

很明显,这很快就能奏效.它的安全性稍差,因为这意味着攻击者试图强行密码可以更快地丢弃猜测的密码:而不必查看整个文件,他们只需要检查该值.这不是一个大问题,因为你的密钥派生函数应该足够困难,而且它们仍然必须运行它
>也存储加密文件的哈希值,并在解密时验证哈希值.
更安全的是,攻击者必须解密整个文件并通过它进行读取,但同样的道理,它很慢.

上一篇:【luogu P5496】【模板】回文自动机(PAM)(回文树)


下一篇:java – 在log4j.properties文件中隐藏或加密密码SMTP Appender