首页 > 解决方案 > 解密文件的加密内容时出错

问题描述

我正在开发一个加密和解密程序,加密工作正常,但解密抛出错误。我找不到,该代码有什么问题?

key.txt
ssshhhhhhhhhhh!!!!

plaintext.txt
naddarbhatia.com





public class hello {

private static SecretKeySpec secretKey;
private static byte[] key;

public static void setKey(String myKey)
{
    MessageDigest sha = null;
    try {
        key = myKey.getBytes("UTF-8");
        sha = MessageDigest.getInstance("SHA-1");
        key = sha.digest(key);
        key = Arrays.copyOf(key, 16);
        secretKey = new SecretKeySpec(key, "AES");
    }
    catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}

public static String encrypt(String strToEncrypt, String secret)
{
    try
    {
        setKey(secret);
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
    }
    catch (Exception e)
    {
        System.out.println("Error while encrypting: " + e.toString());
    }
    return null;
}

public static String decrypt(String strToDecrypt, String secret)
{
    try
    {
        setKey(secret);
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
    }
    catch (Exception e)
    {
        System.out.println("Error while decrypting: " + e.toString());
    }
    return null;
}

static String readFile(String path, Charset encoding) 
          throws IOException 
        {
          byte[] encoded = Files.readAllBytes(Paths.get(path));
          return new String(encoded, encoding);
        }




public static void main(String[] args) throws IOException
{


    String en_de_flag = args[0];

    String secretKey="";
    try {
        secretKey = readFile("/Users/amulbhatia/Documents/EclipseProjects/HelloProject/src/key.txt",StandardCharsets.UTF_8);
        //System.out.println(secretKey);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    String originalString="";
    String encryptedString="";

    try {
        originalString = readFile("/Users/amulbhatia/Documents/EclipseProjects/HelloProject/src/plaintext.txt",StandardCharsets.UTF_8);
    } catch (IOException e) {

        e.printStackTrace();
    }


    if(en_de_flag.equals("0")) {

        encryptedString = hello.encrypt(originalString, secretKey) ;
        PrintWriter out = new PrintWriter("encrypt.txt");
        out.println(encryptedString);
        System.out.println("Encrypted File Generated !! 'encrypt.txt' , Please check now");
        out.close();
    }

    if(en_de_flag.equals("1")) {

        String decryptedFileContent = readFile("/Users/amulbhatia/Documents/EclipseProjects/HelloProject/src/encrypt.txt",StandardCharsets.UTF_8);

        System.out.println("decryptedFileContent:" + decryptedFileContent);
        System.out.println("Secret Key:" + secretKey);

        String decryptedString = hello.decrypt(decryptedFileContent, secretKey) ;
        //System.out.println("Read Encrypted File, Now Decrypting..");
        //System.out.println(decryptedString);
    }


}}


STACKTRACE

java.lang.IllegalArgumentException: Input byte array has incorrect ending byte at 44
    at java.base/java.util.Base64$Decoder.decode0(Base64.java:771)
    at java.base/java.util.Base64$Decoder.decode(Base64.java:535)
    at java.base/java.util.Base64$Decoder.decode(Base64.java:558)
    at hello.decrypt(hello.java:63)
    at hello.main(hello.java:12

8)

在命令行执行上述代码时,参数为“0”时,将使用加密内容生成 encrypt.txt 文件,之后当我输入参数为“1”时,它会读取加密文件“encrypt.txt”和“ key.txt' 并在相同功能失败的情况下调用解密功能,请帮助

标签: javaencryption

解决方案


该错误是由 Base64 解码器引发的,因为您的密文以行终止符结尾,由PrintWriter.

只是.trim()你的decryptedFileContent(这实际上是 encryptedfilecontent...)来删除换行符。


推荐阅读