如何在java中将数据加密为没有特殊字符的纯文本

Sac*_*-17 6 java encryption aes

我需要一种加密算法,我可以将数据加密为简单的纯文本。

目前我正在使用AES 加密算法,该算法转换为包含特殊字符的加密字符串。

在这里,如果我通过 url 中的查询字符串发送这个字符串,我会丢失一些像“+”这样的字符。

所以我需要一个安全且仅包含字母表的加密逻辑。

这是我的加密逻辑:

      public static String encrypt(String Data) throws Exception {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.ENCRYPT_MODE, key);
        byte[] encVal = c.doFinal(Data.getBytes());
        String encryptedValue = new BASE64Encoder().encode(encVal);
        return encryptedValue;
    }

    @SuppressWarnings("restriction")
    public static String decrypt(String encryptedData) throws Exception {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.DECRYPT_MODE, key);
        byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
        byte[] decValue = c.doFinal(decordedValue);
        String decryptedValue = new String(decValue);
        return decryptedValue;
    }
Run Code Online (Sandbox Code Playgroud)

在这里我得到一个加密的字符串“ TEj+TWBQExpz8/p5SAjIhA==

当我通过查询字符串发送时

本地主机:8080/myproject/home?code=TEj+TWBQExpz8/p5SAjIhA==

我得到字符串在控制器原样
TEJ TWBQExpz8 / p5SAjIhA ==

“+”符号丢失,这就是为什么我在解密时遇到问题。

请提出任何新算法或任何解决方案以避免特殊字符。

谢谢你。

Kai*_*hel 6

您可以使用 URLEncoder 对加密部分进行编码

URLEncoder.encode("TEj+TWBQExpz8/p5SAjIhA==", "UTF-8")
Run Code Online (Sandbox Code Playgroud)

使其对 URL 有效。

http://docs.oracle.com/javase/8/docs/api/java/net/URLEncoder.html