我想在文本文件中解密特定字符串并将其写入另一个文件

时间:2014-06-02 10:24:32

标签: java string encryption

sample.log 包含

FLTR  TID: 0000003756   RPC ID: 0000108159   Queue: Admin   Client-RPC: 390626         USER: **[B@4783165b**   Overlay-Group: 1 

我需要它像这样

FLTR  TID: 0000003756   RPC ID: 0000108159   Queue: Admin   Client-RPC: 390626         USER: "DECRYPTED VALUE"   Overlay-Group: 1 

使用密钥

String key = "ThisIsASecretKey";

这是我试过的,请编辑我写错了代码的地方..

public class Decrypt {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("sample.log"));
        String newStr = null;
        String value = "";
        while ((newStr = br.readLine()) != null) {
            String next = null;
            if (newStr.contains("FLTR")) {
                next = newStr.substring(97, 135); // this gets string **[B@4783165b**
                String collect = CallToDecrypt(next, value);
                system.out.println(collect);
            }
        }
        pt.close();
        br.close();
    }

    private static String CallToDecrypt(String next, String value) {
        String key = "ThisIsASecretKey";
        byte[] raw = key.getBytes(Charset.forName("US-ASCII"));
        if (raw.length != 16) {
            throw new IllegalArgumentException("Invalid key size.");
        }
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[16]));
        byte[] original = cipher.doFinal();
        return new String(original, Charset.forName("US-ASCII"));
    }
}

感谢你们每个人

1 个答案:

答案 0 :(得分:0)

您正试图立即解密:next例程中从不使用字符串CallToDecrypt

您必须执行类似

的操作
byte[] original = cipher.doFinal(next.getBytes());