解密文件的加密内容时出错

时间:2019-04-17 12:00:53

标签: java encryption

我正在开发用于加密和解密的程序,加密工作正常,但是解密引发错误。 我找不到,那个代码是什么问题?

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”作为参数执行上述代码时,将使用加密内容生成crypto.txt文件,此后,当我以“ 1”输入参数时,它将读取加密文件“ encrypt.txt” '和'key.txt'并在相同功能失败的情况下调用解密功能,请帮助

1 个答案:

答案 0 :(得分:1)

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

只需.trim()您的decryptedFileContent(实际上是加密的文件内容...)即可删除换行符。