day07 7.2 常用加密
【1】字符串与字节
- 反编译回来的代码有些字符串使用字节表示
- 隐藏真正字符串
String salt = "sign";
System.out.println(Arrays.toString(salt.getBytes()));
String v4 = new String(new byte[]{115, 105, 103, 110});
System.out.println(salt);
System.out.println(v4);
- python转换
v1 = [115, 105, 103, 110]
def java_arr_to_python_str(v1):
num_list = bytearray()
for i in v1:
if i < 0:
i = i + 256
num_list.append(i)
return num_list.decode('utf-8')
if __name__ == '__main__':
print(java_arr_to_python_str(v1))
【2】UUID
import java.util.UUID;
public class Hello {
public static void main(String[] args){
String uid = UUID.randomUUID().toString();
System.out.println(uid);
}
}
// python
import uuid
uid = str(uuid.uuid4())
print(uid)
![3](imgs/day07 7.1 Java中的面向对象之类与对象.assets/3.png)
【3】随机值
- java
// 随机生成80位,10个字节
BigInteger v4 = new BigInteger(80, new SecureRandom());
// 让字节以16进制展示
String res = v4.toString(16);
System.out.println(res);
- python3.9+
# python 3.9
import random
data = random.randbytes(10) # python3.9
ele_list = []
for item in data:
ele = hex(item)[2:]
ele_list.append(ele)
res = "".join(ele_list)
print(res)
# 或者
import random
data = "".join([hex(item)[2:] for item in random.randbytes(10)])
print(data)
- python3.9-
# 3.9以前
import random
byte_list = [random.randint(0, 255) for i in range(10)]
print([item for item in byte_list])
print([hex(item)[2:] for item in byte_list])
print([hex(item)[2:].rjust(2, "0") for item in byte_list])
print("".join([hex(item)[2:].rjust(2, "0") for item in byte_list]))
【4】时间戳
String t1 = String.valueOf(System.currentTimeMillis() / 1000); // 秒级别
String t2 = String.valueOf(System.currentTimeMillis()); // 毫秒级别
System.out.println(t1);
System.out.println(t2);
![4](imgs/day07 7.1 Java中的面向对象之类与对象.assets/4.png)
- python
import time
v1 = int(time.time()) # 秒级别
v2 = int(time.time()*1000)# 毫秒级别
print(v1)
print(v2)
【5】十六进制的字符串
![5](imgs/day07 7.1 Java中的面向对象之类与对象.assets/5.png)
s='pengyuyan'.encode('utf-8')
l=[]
for i in s:
l.append(hex(i)[2:].rjust(2,'0')) # 转16进制,只去后两位,如果不足两位,补0
print(l)
【6】md5
![6](imgs/day07 7.1 Java中的面向对象之类与对象.assets/6.png)
import hashlib
obj = hashlib.md5('yyy'.encode('utf-8'))
obj.update('xxxxx'.encode('utf-8'))
# java中没有这个功能,把每个字节转换成16进制
v1 = obj.hexdigest()
print(v1) # d3c0aaaf596e3299f695591f00fd75cb
v2 = obj.digest()
print(v2) # b'\xd3\xc0\xaa\xafYn2\x99\xf6\x95Y\x1f\x00\xfdu\xcb'
【7】base64编码
- java
String name = "彭于晏";
// 编码
Base64.Encoder encoder = Base64.getEncoder();
String res = encoder.encodeToString(name.getBytes());
System.out.println(res); //5b2t5LqO5pmP
// 解码
Base64.Decoder decoder = Base64.getDecoder();
byte[] origin = decoder.decode(res);
String data = new String(origin);
System.out.println(data); // 彭于晏
- python
import base64
name = "彭于晏"
res = base64.b64encode(name.encode('utf-8'))
print(res) # b'5b2t5LqO5pmP'
data = base64.b64decode(res)
origin = data.decode('utf-8')
print(origin) # "彭于晏"
标签:加密,String,list,day07,System,7.2,item,res,print
From: https://www.cnblogs.com/dream-ze/p/17557893.html