无法从文本文件解密字符串

nafvub8i  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(362)

**结束。**此问题需要详细的调试信息。它目前不接受答案。
**想改进这个问题吗?**更新问题,使其成为堆栈溢出的主题。

11天前关门了。
改进这个问题
在我掌握java的过程中,我开始做一些aes加密和解密,但遇到了一些问题。我可以加密和解密只是罚款使用我下面的2个方法。

public static byte[] encryptString(String datatoEncrypt, String myKey, Cipher cipher)
{
    try
    {
        byte[] text = datatoEncrypt.getBytes(UNICODE_FORMAT);
        byte[] decodedKey = Base64.getDecoder().decode(myKey);
        SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES"); 
        cipher.init(Cipher.ENCRYPT_MODE, originalKey);
        byte[] textEncrpted = cipher.doFinal(text);

        return textEncrpted;
    }
    catch(Exception e)
    {
        return null;
    }
}

以及解密方法

public static String decryptString(byte[] datatoDecrypt, String myKey, Cipher cipher)
{
    try
    {
        System.out.println("Key " + myKey);
        byte[] decodedKey = Base64.getDecoder().decode(myKey);
        SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES"); 
        cipher.init(Cipher.DECRYPT_MODE, originalKey);
        byte[] textDecrpted = cipher.doFinal(datatoDecrypt);
        String result = new String(textDecrpted, StandardCharsets.UTF_8);

        return result;
    }
    catch(Exception e)
    {
        return null;
    }
}

我的问题是当我加密时,我把加密的文件保存到一个文本文件中,现在我试图解密这个文本文件,但它不会解密。

public static void task1()
{
    System.out.println("Key in file path name: ");        
    String filename = keyboard.nextLine();

    try
    {
        Scanner inputFile = new Scanner(new File(filename));

        String message = readFile(filename);
        String key = generateKey();
        Cipher chipher;
        chipher = Cipher.getInstance("AES");
        System.out.println("Key is " +key);
        byte[] encryptedData = encryptString(message, key, chipher);
        String encryptedString = new String(encryptedData);
        System.out.println("Encrypted data is: " + encryptedString);
        PrintWriter writer = new PrintWriter("ciphertext.txt");
        writer.println(encryptedString);
        writer.close();

        byte[] tryagain;
        System.out.println("enter File name");
        String filename1 = keyboard.nextLine();

        tryagain = Files.readAllBytes(Paths.get(filename1));

        System.out.println("Enter Key");
        String keyString = keyboard.nextLine();
        String decrypted = decryptString(tryagain,keyString,chipher);
        System.out.println("Decrypted data: "  + decrypted);
    }
    catch(Exception e)
    {
        System.out.println("Error " );
    }
}

它一直给我回空,我相信问题是文本文件,因为当不使用文本文件它运行得很好。下面是一些代码正常操作时,不使用文本文件。

String text = "Santosisverycool";
String key = generateKey();
Cipher chipher;
chipher = Cipher.getInstance("AES");
System.out.println("Key is " +key);
byte[] encryptedData = encryptString(text, key, chipher);
String encryptedString = new String(encryptedData);
System.out.println("Encrypted data is: " + encryptedString);

String decrypted = decryptString(encryptedData,key,chipher);
System.out.println("decrypted: " +decrypted);

有人有什么解决办法吗?我在这个问题上已经坚持了至少一个星期,还没有进一步解决它。

6ojccjat

6ojccjat1#

writer.println(加密字符串); println 打印字符串,并在字符串后加一个换行符。你不需要新行,只需要字符串。替换为:

writer.print(encryptedString);

正如注解中指出的,字节数组很可能包含不可打印的字符,从而导致结果数据损坏。取而代之的是:
只需跳过字符串转换,将字节数组直接写入具有 FileOutputStream :

try (FileOutputStream fos = new FileOutputStream("ciphertext.txt")) {
    fos.write(encryptedData);
}

相关问题