我想解密它时Java密码的问题

时间:2018-08-31 17:21:02

标签: java encryption aes

我使用javafx制作了一个应用程序,可以编写一些内容并将其保存到数据库中。我的数据库是sqlite。这是一个非常简单的应用程序。尽管我已经将登录应用程序添加到了我的书写应用程序中,但仍然可以通过任何软件打开sqlite。 而不是对sqlite db进行加密(我不知道并且发现这样做确实令人困惑:))我决定对java中的文本进行加密,然后在我想阅读时将其恢复为正常并显示它。

我从this link学到了如何做,并且将其更改为打印字符串而不是写入文件 所以我的最终代码如下:

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

    String textA = "";
    String textB="";

    byte[] thisismykey = "Hello How manyBytes are in@hts A".getBytes();
    SecretKey secKey = new SecretKeySpec(thisismykey, "AES");


    Cipher aesCipher = Cipher.getInstance("AES");

    //turn your original text to byte
    byte[] myoriginaltexttobyte = "Your Plain Text Here".getBytes();
    //activate the encrypt method
    aesCipher.init(Cipher.ENCRYPT_MODE, secKey);
    //encrypt the text and assign the encrypted text to a byte array
    byte[] bytecipheredoforgtext = aesCipher.doFinal(myoriginaltexttobyte);
    //change it to string with new string
    textA = new String(bytecipheredoforgtext);
    System.out.println(textA);

    //get the bytes of encrypted text and assign it to a byte array
    byte[] byteofencryptedtext = textA.getBytes();
    //activate the decrypt mode of the cipher
    aesCipher.init(Cipher.DECRYPT_MODE, secKey);
    //decrypt the encrypted text and assign it to a byte array
    byte[] byteofencryptedbacktonormaltext = aesCipher.doFinal(byteofencryptedtext);
    //change it to string with new string
    textB = new String(byteofencryptedbacktonormaltext);
    System.out.println(textB);
}

现在,加密和解密是在同一方法下,它可以完美地工作,但是我想将其更改为具有不同方法的类,以便我可以使用一种方法对文本进行加密,而使用另一种方法进行解密。但是当我分开东西时,解密无法正常工作。加密工作正常。这是现在的代码:

public class CipherFinalB {

//from https://stackoverflow.com/questions/20796042/aes-encryption-and-decryption-with-java/20796446#20796446
private final byte[] thisismykey;
private final SecretKey secKey;
private final Cipher aesCipher;
public CipherFinalB() throws NoSuchPaddingException, NoSuchAlgorithmException {
    thisismykey = "HellodHowfmanyBytesgarehin@htseA".getBytes();
    secKey = new SecretKeySpec(thisismykey, "AES");

    aesCipher = Cipher.getInstance("AES");

}public String encrypt (String originaltext) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    byte[] myoriginaltexttobyte =originaltext.getBytes();
    aesCipher.init(Cipher.ENCRYPT_MODE, secKey);
    byte[] bytecipheredoforgtext = aesCipher.doFinal(myoriginaltexttobyte);
    String textA = new String(bytecipheredoforgtext);
    System.out.println(textA);
    return new String(bytecipheredoforgtext);
}

public String decrypt (String encryptedtext) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    byte[] byteofencryptedtext = encryptedtext.getBytes();
    aesCipher.init(Cipher.DECRYPT_MODE, secKey);
    byte[] byteofencryptedbacktonormaltext = aesCipher.doFinal(byteofencryptedtext);
    return new String(byteofencryptedbacktonormaltext);
}

}

当我使用加密方法时,它会给我返回一个字符串。当我发送相同的字符串来解密方法时,它不起作用,并给我以下错误:

Exception in thread "main" javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
at java.base/com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:936)
    at java.base/com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:847)
    at java.base/com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446)
    at java.base/javax.crypto.Cipher.doFinal(Cipher.java:2191)
    at main.CipherFinalB.decrypt(CipherFinalB.java:66)
    at main.CipherTest.main(CipherTest.java:16)

我该怎么办?

更新答案:

@Juan说的问题是“加密数据时,数组中可能有任何字节,而不仅仅是可打印字符。”因此,我将方法更改为返回字节以用于加密方法和解密方法。解密方法现在将字节作为参数而不是字符串作为参数,并且现在一切正常。

更新后的代码如下:

 public byte[] encrypt (String originaltext) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    byte[] myoriginaltexttobyte =originaltext.getBytes();
    aesCipher.init(Cipher.ENCRYPT_MODE, secKey);
    byte[] bytecipheredoforgtext = aesCipher.doFinal(myoriginaltexttobyte);
    String textA = new String(bytecipheredoforgtext);
    System.out.println(textA);
    return bytecipheredoforgtext;
}

public byte[] decrypt (byte[] encryptedtext) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    byte[] byteofencryptedtext = encryptedtext;
    aesCipher.init(Cipher.DECRYPT_MODE, secKey);
    byte[] byteofencryptedbacktonormaltext = aesCipher.doFinal(byteofencryptedtext);
    return byteofencryptedbacktonormaltext;
}

1 个答案:

答案 0 :(得分:0)

我不确定错误是否是由此引起的,但是您正在执行的String和byte []之间的转换可能适用于字符集范围内的字节,但是当对数据进行加密时,数组中可能会有任何字节,不仅是可打印的字符。

获得带有密文的byte []后,将其编码为Base64并存储字符串表示形式。

解密时,首先将Base64解码为byte [],然后解密。

See Base64 class