简体   繁体   English

加密不带特殊字符(“ =”)的数据

[英]Encrypt data without special character (“=”)

I have written simple encryption and decryption program in java. 我用Java编写了简单的加密和解密程序。 And I am using "AES" algorithm for encryption and decryption. 我正在使用"AES" algorithm进行加密和解密。 It is working fine, but in encrypted data I am getting special characters like "/","=" etc. 它工作正常,但是在加密数据中,我得到了特殊字符,例如"/","="等。

But I don't want the special characters in encrypted data specially "=" operator. 但是我不希望加密数据中的特殊字符特别是“ =”运算符。 Because it causing issue for my further processing. 因为这会导致我进一步处理的问题。 Is there any way to avoid special characters or single "=" operator in encrypted data. 有什么方法可以避免在加密数据中使用特殊字符或单个"="运算符。

I googled it and I got some suggestion like, convert the data into hashcode, so hashcode encryption will not contains special character. 我用谷歌搜索,得到了一些建议,例如将数据转换为哈希码,因此哈希码加密将不包含特殊字符。 But as per the suggestions, hashcode encryption is not secret key based, I needed the encryption using secret key 但是根据建议,哈希码加密不是基于secret key的,我需要使用secret key进行加密

How can I achive this? 我该如何实现?

Any help will appriciated. 任何帮助都将适用。 Thanks 谢谢

Following is program I have written in java: 以下是我用Java编写的程序:

public class EncDec
{
    private static final String ALGO = "AES";
    private static final byte[] keyValue = "1234567891234567".getBytes();
    public static void main(String[] args) throws Exception 
    {
        String testData = "ABC";
        String enc = encrypt(testData);
        System.out.println("Encrypted data: "+enc);
        String dec = decrypt(enc);
        System.out.println("Decrypted data: "+enc);
    }
    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;
    }
    public static String decrypt(String encryptedData) throws Exception 
    {
        try{
            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;
        }catch(Exception e)
        {
            System.out.println("Something wrong..");
            return "";
        }
    }
    private static Key generateKey() throws Exception
    {
        Key key = new SecretKeySpec(keyValue, ALGO);
        return key;
    }
}

And I got the Result like: 我得到的结果是:

Encrypted data: /ia3VXrqaaUls7fon4RBhQ== 加密的数据: /ia3VXrqaaUls7fon4RBhQ==

Decrypted data: ABC . 解密的数据: ABC

URL-safe base 64 as defined in RFC 4648 section-5 could be used. 可以使用RFC 4648第5节中定义的 URL安全基础64。

To use URL-safe base 64 it is possible to use the new Base64 class in java.util (since Java 8). 要使用URL安全的base 64,可以java.util使用新的Base64 (从Java 8开始)。 If the = must be avoided then it is possible to specify to not use padding. 如果必须避免= ,则可以指定不使用填充。 The decoder should of course be configured in the same way: 解码器当然应该以相同的方式配置:

Encoder urlEncoder = java.util.Base64.getUrlEncoder().withoutPadding();
String encoded = urlEncoder.encodeToString(new byte[] { (byte) 0xFF, (byte) 0xE0});
System.out.println(encoded);

Decoder urlDecoder = java.util.Base64.getUrlDecoder();
byte[] decoded = urlDecoder.decode(encoded);
System.out.printf("(byte) 0x%02X, (byte) 0x%02X%n", decoded[0], decoded[1]);

results in: 结果是:

_-A
(byte) 0xFF, (byte) 0xE0

Note that it is probably not OK to use base 64 and simply remove the padding. 请注意,使用base 64并仅删除填充物可能不适合。 In that case + and / characters may be returned depending on the input. 在这种情况下,根据输入,可能会返回+/字符。

Since the output of many cryptographic primitives - especially those used for encryption - is indistinguishable from random, it is possible to get these characters at any time, even for the same plaintext. 由于许多密码原语(尤其是用于加密的原语)的输出与随机是无法区分的,因此即使对于相同的明文,也可以随时获取这些字符。

This is also why URL encoding the result is a less optimal solution; 这也是为什么对结果进行URL编码不是最佳解决方案的原因。 you don't know how many characters need to be escaped in advance making the output size unpredictable. 您不知道需要预先转义多少个字符以使输出大小不可预测。

Instead of converting into base64 encoding use HEX encoding . 使用HEX encoding而不是转换为base64 encoding HEX encoding That worked fine for me 对我来说很好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM