Wrecked

Holding You Back As I Breath You In


  • 首页

  • 关于

  • 标签26

  • 分类15

  • 归档34

  • 搜索

Java DES(对称加密)

发表于 2019-01-16 | 更新于 2019-02-22 | 分类于 Java |
简单一种对称加密与解密
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class DESUtils {

/*
* 生成密钥
*/
public static byte[] initKey() throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance("DES");
keyGen.init(56);
SecretKey secretKey = keyGen.generateKey();
return secretKey.getEncoded();
}

/*
* DES 加密
*/
public static String encrypt(byte[] data, byte[] key) throws Exception {
SecretKey secretKey = new SecretKeySpec(key, "DES");

Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] cipherBytes = cipher.doFinal(data);
String result = byte2Hex(cipherBytes);
return result;
}

/*
* DES 解密
*/
public static String decrypt(byte[] data, byte[] key) throws Exception {
SecretKey secretKey = new SecretKeySpec(key, "DES");
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] plainBytes = cipher.doFinal(data);
String result = new String(plainBytes);
return result;
}

private static int toByte(char c) {
byte b = (byte) "0123456789ABCDEF".indexOf(c);
return b;
}

public static byte[] hexToByte(String hex) {
int len = (hex.length() / 2);
byte[] result = new byte[len];
char[] achar = hex.toCharArray();
for (int i = 0; i < len; i++) {
int pos = i * 2;
result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
}
return result;
}

public static String byte2Hex(byte[] b) {
String stmp = "";
StringBuilder sb = new StringBuilder("");
for (int n = 0; n < b.length; n++) {
stmp = Integer.toHexString(b[n] & 0xFF);
sb.append((stmp.length() == 1) ? "0" + stmp : stmp);
}
return sb.toString().toUpperCase().trim();
}

//加密
public static String encryptStr(String str, String secreytKey) throws Exception {
String enResult = encrypt(str.getBytes(), hexToByte(secreytKey));
return enResult;
}

//解密
public static String decryptStr(String str, String secreytKey) throws Exception {
String deResult = decrypt(hexToByte(str), hexToByte(secreytKey));
return deResult;
}

/***
*
秘钥769bfbae9d20100d
加密结果e3f6b60180661bb0d541de00260afb10
解密结果hello_world
*
* @param args
* @throws Exception
*/

public static void main(String[] args) throws Exception {
//生成秘钥
byte[] desKey = initKey();
String secreytKey = byte2Hex(desKey);
System.out.println("秘钥" + secreytKey.toLowerCase());
//加密
String hello_world = encryptStr("hello_world", secreytKey);
System.out.println("加密结果" + hello_world.toLowerCase());
//解密
String deResult = decryptStr(hello_world, secreytKey);
System.out.println("解密结果" + deResult);

}
}

ThreadLocal

发表于 2018-12-04 | 更新于 2019-02-22 | 分类于 Java |

ThreadLocal 类结构图

image

get

1
2
3
4
5
6
7
8
9
10
11
12
13
public T get() {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null) {
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null) {
@SuppressWarnings("unchecked")
T result = (T)e.value;
return result;
}
}
return setInitialValue();
}

set

1
2
3
4
5
6
7
8
public void set(T value) {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
}

remove

1
2
3
4
5
public void remove() {
ThreadLocalMap m = getMap(Thread.currentThread());
if (m != null)
m.remove(this);
}

setInitialValue

1
2
3
4
5
6
7
8
9
10
private T setInitialValue() {
T value = initialValue();
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
return value;
}

initialValue

1
2
3
protected T initialValue() {
return null;
}

getMap

1
2
3
ThreadLocalMap getMap(Thread t) {
return t.threadLocals;
}

createMap

1
2
3
void createMap(Thread t, T firstValue) {
t.threadLocals = new ThreadLocalMap(this, firstValue);
}

Linux Top命令

发表于 2018-11-30 | 更新于 2019-06-04 | 分类于 Linux |

uptime / top

image

1
16:36:50 up 108 days,  7:24,  2 users,  load average: 61.27, 61.50, 61.60

Field Desc
16:36:50 时间
up 1:22 系统运行时间
users 当前登录用户数
load average 三个数值分别为 1分钟、5分钟、15分钟前到现在的平均负载

英文文档链接 Understanding-Load-Averages

中文文档链接 Understanding-Load-Averages

Tasks

key value Desc
29 total 进程总数
1 running 正在运行的进程数
28 sleeping 睡眠的进程数
0 stopped 停止的进程数
0 zombie 僵尸进程数

Cpu(s)

key value Desc
0.3% us 用户空间占用CPU百分比
1.0% sy 内核空间占用CPU百分比
0.0% ni 用户进程空间内改变过优先级的进程占用CPU百分比
98.7% id 空闲CPU百分比
0.0% wa 等待输入输出的CPU时间百分比

Mem

key value Desc
191272k total 物理内存总量
173656k used 使用的物理内存总量
17616k free 空闲内存总量
22052k buffers 用作内核缓存的内存量

Swap

key value Desc
192772k total 交换区总量
0k used 使用的交换区总量
192772k free 空闲交换区总量
123988k cached 缓冲的交换区总量。

详细信息

Key Desc
PID 进程id
PPID 父进程id
RUSER Real user name
UID 进程所有者的用户id
USER 进程所有者的用户名
GROUP 进程所有者的组名
TTY 启动进程的终端名。不是从终端启动的进程则显示为 ?
PR 优先级
NI nice值。负值表示高优先级,正值表示低优先级
P 最后使用的CPU,仅在多CPU环境下有意义
%CPU 上次更新到现在的CPU时间占用百分比
TIME 进程使用的CPU时间总计,单位秒
TIME+ 进程使用的CPU时间总计,单位1/100秒
%MEM 进程使用的物理内存百分比
VIRT 进程使用的虚拟内存总量,单位kb。VIRT=SWAP+RES
SWAP 进程使用的虚拟内存中,被换出的大小,单位kb。
RES 进程使用的、未被换出的物理内存大小,单位kb。RES=CODE+DATA
CODE 可执行代码占用的物理内存大小,单位kb
DATA 可执行代码以外的部分(数据段+栈)占用的物理内存大小,单位kb
SHR 共享内存大小,单位kb
nFLT 页面错误次数
nDRT 最后一次写入到现在,被修改过的页面数。
S 进程状态。
S D=不可中断的睡眠状态
S R=运行
S S=睡眠
S T=跟踪/停止
S Z=僵尸进程
COMMAND 命令名/命令行
WCHAN 若该进程在睡眠,则显示睡眠中的系统函数名
Flags 任务标志,参考 sched.h
1…101112
Wrecked

Wrecked

34 日志
15 分类
26 标签
RSS
GitHub E-Mail
Links
  • WeChat
  • Java2s
© 2020 本博客所有文章除特别声明外。转载请注明出处!