当前位置:网站首页>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
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 .
边栏推荐
- C#合并多个richtextbox内容时始终存在换行符的解决方法
- Table access among Oracle database users
- It's called the next generation monitoring system. Let's see how awesome it is
- How to write an amazing design document?
- I always don't understand the high address and high position
- Vsys of Isis (virtual system)
- 平衡二叉树学习笔记------一二熊猫
- Reflection of C # Foundation
- 微隔离(MSG)
- Calculate running total / running balance
猜你喜欢
基于ESP32CAM实现WebSocket服务器实时点灯
Try to use renderdoc to view the shader code of UE
redis-4. Redis' message subscription, pipeline, transaction, modules, bloom filter, and cache LRU
RT thread simulator lvgl control: switch switch button control
One article of quantitative framework backtrader read analyzer
redis-7. Redis master-slave replication, cap, Paxos, cluster sharding cluster 02
C # related knowledge points
JMeter encryption interface test
About database: pgadmin4 editing SQL window
C语言:如何给全局变量起一个别名?
随机推荐
简单了解C语言基本语
Implementation of fruit mall wholesale platform based on SSM
C drawing table and sending mail function
部署RDS服务
Vsys of Isis (virtual system)
9. process control
Performance tuning can't just depend on tapping the brain
FTP_ Manipulate remote files
oracle问题,字段里面的数据被逗号隔开,取逗号两边数据
对绘制丘岭密度图ridge plot的详细说明、重叠核密度估计曲线overlapping densities、FacetGrid对象、函数sns.kdeplot、函数FacetGrid.map
Test development programmers, are you still confused? You can't define yourself as a yard farmer
Nodejs file module FS
Sorting of numbers and strings
量化框架backtrader之一文讀懂Analyzer分析器
NFV基本概述
redis-4. Redis' message subscription, pipeline, transaction, modules, bloom filter, and cache LRU
C # using multithreading
论文笔记: 多标签学习 BP-MLL
A. Vacations (dp 贪心
redis-6. Redis master-slave replication, cap, Paxos, cluster sharding cluster 01