Skip to content

Commit 68f0367

Browse files
woofyzhaobruceneenhl
authored andcommitted
[INLONG-4790][SDK] Upgrade DES encryption decryption to AES (apache#4795)
1 parent d73f3a4 commit 68f0367

File tree

7 files changed

+175
-168
lines changed

7 files changed

+175
-168
lines changed

inlong-sdk/dataproxy-sdk/src/main/java/org/apache/inlong/sdk/dataproxy/codec/ProtocolEncoder.java

+21-20
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,10 @@
1818

1919
package org.apache.inlong.sdk.dataproxy.codec;
2020

21-
import static org.apache.inlong.sdk.dataproxy.ConfigConstants.FLAG_ALLOW_AUTH;
22-
import static org.apache.inlong.sdk.dataproxy.ConfigConstants.FLAG_ALLOW_COMPRESS;
23-
import static org.apache.inlong.sdk.dataproxy.ConfigConstants.FLAG_ALLOW_ENCRYPT;
24-
2521
import io.netty.buffer.ByteBuf;
2622
import io.netty.buffer.ByteBufAllocator;
2723
import io.netty.channel.ChannelHandlerContext;
2824
import io.netty.handler.codec.MessageToMessageEncoder;
29-
import java.io.ByteArrayOutputStream;
30-
import java.io.IOException;
31-
import java.io.UnsupportedEncodingException;
32-
import java.nio.ByteBuffer;
33-
import java.security.SecureRandom;
34-
import java.util.Iterator;
35-
36-
import java.util.List;
3725
import org.apache.inlong.sdk.dataproxy.config.EncryptConfigEntry;
3826
import org.apache.inlong.sdk.dataproxy.config.EncryptInfo;
3927
import org.apache.inlong.sdk.dataproxy.network.Utils;
@@ -42,7 +30,20 @@
4230
import org.slf4j.LoggerFactory;
4331
import org.xerial.snappy.Snappy;
4432

33+
import java.io.ByteArrayOutputStream;
34+
import java.io.IOException;
35+
import java.io.UnsupportedEncodingException;
36+
import java.nio.ByteBuffer;
37+
import java.security.SecureRandom;
38+
import java.util.Iterator;
39+
import java.util.List;
40+
41+
import static org.apache.inlong.sdk.dataproxy.ConfigConstants.FLAG_ALLOW_AUTH;
42+
import static org.apache.inlong.sdk.dataproxy.ConfigConstants.FLAG_ALLOW_COMPRESS;
43+
import static org.apache.inlong.sdk.dataproxy.ConfigConstants.FLAG_ALLOW_ENCRYPT;
44+
4545
public class ProtocolEncoder extends MessageToMessageEncoder<EncodeObject> {
46+
4647
private static final Logger logger = LoggerFactory
4748
.getLogger(ProtocolEncoder.class);
4849

@@ -119,7 +120,7 @@ private ByteBuf writeToBuf8(EncodeObject object) {
119120
}
120121

121122
private ByteBuf constructBody(byte[] body, EncodeObject object,
122-
int totalLength, int cnt) throws UnsupportedEncodingException {
123+
int totalLength, int cnt) throws UnsupportedEncodingException {
123124
ByteBuf buf = null;
124125
if (body != null) {
125126
if (object.isCompress()) {
@@ -134,9 +135,9 @@ private ByteBuf constructBody(byte[] body, EncodeObject object,
134135
}
135136
EncryptInfo encryptInfo = encryptEntry.getRsaEncryptInfo();
136137
endAttr = endAttr + "_userName=" + object.getUserName()
137-
+ "&_encyVersion=" + encryptInfo.getVersion()
138-
+ "&_encyDesKey=" + encryptInfo.getRsaEncryptedKey();
139-
body = EncryptUtil.desEncrypt(body, encryptInfo.getDesKey());
138+
+ "&_encyVersion=" + encryptInfo.getVersion()
139+
+ "&_encyAesKey=" + encryptInfo.getRsaEncryptedKey();
140+
body = EncryptUtil.aesEncrypt(body, encryptInfo.getAesKey());
140141
}
141142
}
142143
if (!object.isGroupIdTransfer()) {
@@ -287,8 +288,8 @@ private ByteBuf writeToBuf5(EncodeObject object) {
287288
EncryptInfo encryptInfo = encryptEntry.getRsaEncryptInfo();
288289
msgAttrs = msgAttrs + "_userName=" + object.getUserName()
289290
+ "&_encyVersion=" + encryptInfo.getVersion()
290-
+ "&_encyDesKey=" + encryptInfo.getRsaEncryptedKey();
291-
body = EncryptUtil.desEncrypt(body, encryptInfo.getDesKey());
291+
+ "&_encyAesKey=" + encryptInfo.getRsaEncryptedKey();
292+
body = EncryptUtil.aesEncrypt(body, encryptInfo.getAesKey());
292293
}
293294
}
294295
if (Utils.isNotBlank(object.getMsgUUID())) {
@@ -377,8 +378,8 @@ private ByteBuf writeToBuf3(EncodeObject object) {
377378
EncryptInfo encryptInfo = encryptEntry.getRsaEncryptInfo();
378379
msgAttrs = msgAttrs + "_userName=" + object.getUserName()
379380
+ "&_encyVersion=" + encryptInfo.getVersion()
380-
+ "&_encyDesKey=" + encryptInfo.getRsaEncryptedKey();
381-
body = EncryptUtil.desEncrypt(body, encryptInfo.getDesKey());
381+
+ "&_encyAesKey=" + encryptInfo.getRsaEncryptedKey();
382+
body = EncryptUtil.aesEncrypt(body, encryptInfo.getAesKey());
382383
}
383384
}
384385
if (Utils.isNotBlank(object.getMsgUUID())) {

inlong-sdk/dataproxy-sdk/src/main/java/org/apache/inlong/sdk/dataproxy/config/EncryptConfigEntry.java

+25-24
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,33 @@
1818

1919
package org.apache.inlong.sdk.dataproxy.config;
2020

21-
import java.net.URLEncoder;
22-
import java.security.interfaces.RSAPublicKey;
23-
import java.util.concurrent.atomic.AtomicLong;
24-
2521
import org.apache.commons.codec.binary.Base64;
2622
import org.apache.inlong.sdk.dataproxy.utils.EncryptUtil;
2723
import org.slf4j.Logger;
2824
import org.slf4j.LoggerFactory;
2925

26+
import java.net.URLEncoder;
27+
import java.security.interfaces.RSAPublicKey;
28+
import java.util.concurrent.atomic.AtomicLong;
29+
3030
/**
3131
* Created by lamberliu on 2016/5/13.
3232
*/
3333
public class EncryptConfigEntry implements java.io.Serializable {
34+
3435
private static final Logger logger = LoggerFactory.getLogger(EncryptConfigEntry.class);
3536
private String userName = "";
3637
private String version;
3738
private String pubKey;
38-
private byte[] desKey;
39+
private byte[] aesKey;
3940
private String rsaEncryptedKey;
4041
private AtomicLong lastUpdateTime = new AtomicLong(0);
4142

4243
public EncryptConfigEntry(final String userName, final String version, final String pubKey) {
4344
this.userName = userName;
4445
this.version = version;
4546
this.pubKey = pubKey;
46-
this.desKey = null;
47+
this.aesKey = null;
4748
this.rsaEncryptedKey = null;
4849
// this.rsaKey = EncryptUtil.loadPublicKeyByText(pubKey);
4950
}
@@ -52,27 +53,35 @@ public String getVersion() {
5253
return version;
5354
}
5455

56+
public void setVersion(String version) {
57+
this.version = version;
58+
}
59+
5560
public String getPubKey() {
5661
return pubKey;
5762
}
5863

64+
public void setPubKey(String pubKey) {
65+
this.pubKey = pubKey;
66+
}
67+
5968
public String getUserName() {
6069
return userName;
6170
}
6271

63-
public synchronized byte[] getDesKey() {
64-
if (desKey == null) {
65-
desKey = EncryptUtil.generateDesKey();
72+
public synchronized byte[] getAesKey() {
73+
if (aesKey == null) {
74+
aesKey = EncryptUtil.generateAesKey();
6675
}
6776

68-
return desKey;
77+
return aesKey;
6978
}
7079

7180
public String getRsaEncryptedKey() {
7281
if (rsaEncryptedKey == null) {
7382
RSAPublicKey rsaKey = EncryptUtil.loadPublicKeyByText(pubKey);
7483
try {
75-
byte[] encryptedKey = EncryptUtil.rsaEncrypt(rsaKey, getDesKey());
84+
byte[] encryptedKey = EncryptUtil.rsaEncrypt(rsaKey, getAesKey());
7685
String tmpKey = Base64.encodeBase64String(encryptedKey);
7786
rsaEncryptedKey = URLEncoder.encode(tmpKey, "utf8");
7887
this.lastUpdateTime.set(System.currentTimeMillis());
@@ -90,7 +99,7 @@ public EncryptInfo getRsaEncryptInfo() {
9099
EncryptInfo encryptInfo = null;
91100
long visitTime = this.lastUpdateTime.get();
92101
if (rsaEncryptedKey != null && (System.currentTimeMillis() - visitTime) <= 3 * 60 * 1000) {
93-
encryptInfo = new EncryptInfo(this.version, this.rsaEncryptedKey, this.desKey);
102+
encryptInfo = new EncryptInfo(this.version, this.rsaEncryptedKey, this.aesKey);
94103
if (visitTime == this.lastUpdateTime.get()) {
95104
return encryptInfo;
96105
}
@@ -99,20 +108,20 @@ public EncryptInfo getRsaEncryptInfo() {
99108
synchronized (this.lastUpdateTime) {
100109
if (visitTime == this.lastUpdateTime.get()) {
101110
RSAPublicKey rsaKey = EncryptUtil.loadPublicKeyByText(pubKey);
102-
this.desKey = EncryptUtil.generateDesKey();
111+
this.aesKey = EncryptUtil.generateAesKey();
103112
try {
104-
byte[] encryptedKey = EncryptUtil.rsaEncrypt(rsaKey, this.desKey);
113+
byte[] encryptedKey = EncryptUtil.rsaEncrypt(rsaKey, this.aesKey);
105114
String tmpKey = Base64.encodeBase64String(encryptedKey);
106115
rsaEncryptedKey = URLEncoder.encode(tmpKey, "utf8");
107116
this.lastUpdateTime.set(System.currentTimeMillis());
108-
return new EncryptInfo(this.version, this.rsaEncryptedKey, this.desKey);
117+
return new EncryptInfo(this.version, this.rsaEncryptedKey, this.aesKey);
109118
} catch (Throwable e) {
110119
logger.error("getRsaEncryptInfo failure, RSA Encrypt error {}", e);
111120
return null;
112121
}
113122
}
114123
}
115-
return new EncryptInfo(this.version, this.rsaEncryptedKey, this.desKey);
124+
return new EncryptInfo(this.version, this.rsaEncryptedKey, this.aesKey);
116125
}
117126

118127
@Override
@@ -129,14 +138,6 @@ public boolean equals(Object other) {
129138
&& (this.pubKey == info.getPubKey());
130139
}
131140

132-
public void setVersion(String version) {
133-
this.version = version;
134-
}
135-
136-
public void setPubKey(String pubKey) {
137-
this.pubKey = pubKey;
138-
}
139-
140141
public String toString() {
141142
return "{\"version\":\"" + version + "\",\"public_key\":\"" + pubKey + "\",\"groupId\":\"" + userName + "\"}";
142143
}

inlong-sdk/dataproxy-sdk/src/main/java/org/apache/inlong/sdk/dataproxy/config/EncryptInfo.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919
package org.apache.inlong.sdk.dataproxy.config;
2020

2121
public class EncryptInfo {
22+
2223
private String version;
23-
private byte[] desKey;
24+
private byte[] aesKey;
2425
private String rsaEncryptedKey;
2526

26-
public EncryptInfo(String version, String rsaEncryptedKey, byte[] desKey) {
27+
public EncryptInfo(String version, String rsaEncryptedKey, byte[] aesKey) {
2728
this.version = version;
2829
this.rsaEncryptedKey = rsaEncryptedKey;
29-
this.desKey = desKey;
30+
this.aesKey = aesKey;
3031
}
3132

3233
public String getVersion() {
@@ -37,12 +38,12 @@ public void setVersion(String version) {
3738
this.version = version;
3839
}
3940

40-
public byte[] getDesKey() {
41-
return desKey;
41+
public byte[] getAesKey() {
42+
return aesKey;
4243
}
4344

44-
public void setDesKey(byte[] desKey) {
45-
this.desKey = desKey;
45+
public void setAesKey(byte[] aesKey) {
46+
this.aesKey = aesKey;
4647
}
4748

4849
public String getRsaEncryptedKey() {

inlong-sdk/dataproxy-sdk/src/main/java/org/apache/inlong/sdk/dataproxy/example/UdpClientExample.java

+27-25
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818

1919
package org.apache.inlong.sdk.dataproxy.example;
2020

21-
import static org.apache.inlong.sdk.dataproxy.ConfigConstants.FLAG_ALLOW_COMPRESS;
22-
import static org.apache.inlong.sdk.dataproxy.ConfigConstants.FLAG_ALLOW_ENCRYPT;
23-
2421
import io.netty.bootstrap.Bootstrap;
2522
import io.netty.buffer.ByteBuf;
2623
import io.netty.buffer.ByteBufAllocator;
@@ -31,16 +28,6 @@
3128
import io.netty.channel.nio.NioEventLoopGroup;
3229
import io.netty.channel.socket.DatagramPacket;
3330
import io.netty.channel.socket.nio.NioDatagramChannel;
34-
import java.io.IOException;
35-
import java.nio.charset.StandardCharsets;
36-
import java.util.concurrent.TimeUnit;
37-
38-
import java.io.ByteArrayOutputStream;
39-
import java.io.UnsupportedEncodingException;
40-
import java.net.InetSocketAddress;
41-
import java.nio.ByteBuffer;
42-
import java.util.Iterator;
43-
import java.util.Random;
4431
import org.apache.inlong.sdk.dataproxy.codec.EncodeObject;
4532
import org.apache.inlong.sdk.dataproxy.config.EncryptConfigEntry;
4633
import org.apache.inlong.sdk.dataproxy.config.EncryptInfo;
@@ -51,12 +38,27 @@
5138
import org.slf4j.LoggerFactory;
5239
import org.xerial.snappy.Snappy;
5340

41+
import java.io.ByteArrayOutputStream;
42+
import java.io.IOException;
43+
import java.io.UnsupportedEncodingException;
44+
import java.net.InetSocketAddress;
45+
import java.nio.ByteBuffer;
46+
import java.nio.charset.StandardCharsets;
47+
import java.security.SecureRandom;
48+
import java.util.Iterator;
49+
import java.util.concurrent.TimeUnit;
50+
51+
import static org.apache.inlong.sdk.dataproxy.ConfigConstants.FLAG_ALLOW_COMPRESS;
52+
import static org.apache.inlong.sdk.dataproxy.ConfigConstants.FLAG_ALLOW_ENCRYPT;
53+
5454
public class UdpClientExample {
5555

5656
private static final Logger logger = LoggerFactory.getLogger(UdpClientExample.class);
5757

5858
private static SequentialID idGenerator = new SequentialID(Utils.getLocalIp());
5959

60+
private static SecureRandom random = new SecureRandom();
61+
6062
public static void main(String[] args) {
6163
long sentCount = 10;
6264
String groupId = "test_group_id";
@@ -92,6 +94,16 @@ public static void main(String[] args) {
9294
}
9395
}
9496

97+
public static String getRandomString(int length) {
98+
StringBuilder sb = new StringBuilder();
99+
String string = "i am bus test client!";
100+
for (int i = 0; i < length; i++) {
101+
int number = random.nextInt(string.length());
102+
sb.append(string.charAt(number));
103+
}
104+
return sb.toString();
105+
}
106+
95107
public boolean sendUdpMessage(Channel channel, String ip, int port, ByteBuf msg) {
96108
try {
97109
channel.writeAndFlush(new DatagramPacket(msg, new InetSocketAddress(ip, port))).sync();
@@ -187,9 +199,9 @@ private ByteBuf writeToBuf7(EncodeObject object) {
187199
}
188200
EncryptInfo encryptInfo = encryptEntry.getRsaEncryptInfo();
189201
endAttr = endAttr + "_userName=" + object.getUserName() + "&_encyVersion="
190-
+ encryptInfo.getVersion() + "&_encyDesKey="
202+
+ encryptInfo.getVersion() + "&_encyAesKey="
191203
+ encryptInfo.getRsaEncryptedKey();
192-
body = EncryptUtil.desEncrypt(body, encryptInfo.getDesKey());
204+
body = EncryptUtil.aesEncrypt(body, encryptInfo.getAesKey());
193205
}
194206
}
195207
if (!object.isGroupIdTransfer()) {
@@ -281,14 +293,4 @@ protected void channelRead0(ChannelHandlerContext var1,
281293
}
282294
return channel;
283295
}
284-
285-
public static String getRandomString(int length) {
286-
StringBuffer sb = new StringBuffer();
287-
String string = "i am bus test client!";
288-
for (int i = 0; i < length; i++) {
289-
int number = new Random().nextInt(string.length());
290-
sb.append(string.charAt(number));
291-
}
292-
return sb.toString();
293-
}
294296
}

0 commit comments

Comments
 (0)