当前位置:网站首页>Precautions for passing parameters with byte array

Precautions for passing parameters with byte array

2022-06-13 07:25:00 Small side good side

First look at one dmeo

public static void main(String[] args) {
    
        String s1 = "hello world";
        byte[] b = s1.getBytes(Charset.defaultCharset());
        printByte(b);
        String s2 = new String(b, Charset.defaultCharset());
        System.out.println(s2);
        printByte(s2.getBytes(Charset.defaultCharset()));
        
        SecureRandom secureRandom = new SecureRandom();
        byte[] key = new byte[16];
        secureRandom.nextBytes(key);
        printByte(key);
        String keyStr = new String(key, Charset.defaultCharset());
        System.out.println(keyStr);
        byte[] key2 = keyStr.getBytes(Charset.defaultCharset());
        printByte(key2);
    }

    private static void printByte(byte[] bytes){
    
        if(bytes!=null){
    
            for (int i = 0; i < bytes.length; i++) {
    
                System.out.print(bytes[i]+" ");
            }
            System.out.println();
        }
    }

Running results
 Insert picture description here
Is it strange? ? Why? hello world Can accurately convert each other , And random byte When the array is turned back, it changes . Many people think that as long as the coding format is set correctly , Going around shouldn't have any effect , It's not .
Usually we have a lot of scenes that need to be byte An array is passed as a parameter , But if you switch randomly in the middle , It may change when you turn back , Such as encryption and decryption , Almost all the encryption and decryption at the bottom are byte[] For the parameter of . Someone once wrote such code , I haven't found the reason for it for a long time , Because everyone thinks iv Set the length at the beginning ,encrypt There must be 16 Bit …

private static String test(String data){
    
        String key = "";// Key obtained from key management 
        // Generate random iv
        byte[] iv = new byte[16];
        SecureRandom random = new SecureRandom();
        random.nextBytes(iv);
        String ivStr = new String(iv, Charset.defaultCharset());
    }

    private static String encrypt(String key, String iv, String data){
    
        byte[] ivBytes = iv.getBytes(Charset.defaultCharset());
        if(ivBytes.length!=16){
    
            return null;
        }
        return AESUtil.encrypt(data, key.getBytes(Charset.defaultCharset()), ivBytes);
    }

In fact, this kind of scene is what beginners will write , Usually, encryption and decryption tools , The parameters passed by the upper layer are all strings , But these strings go through base64 Encoding , Whether it's in plain text 、 Ciphertext 、 Key, vector salt value, etc base64 code . after base64 Encoded ciphertext 、 Vectors, etc. will not be distorted even in network transmission .

原网站

版权声明
本文为[Small side good side]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270549072148.html