From d9576bf2755b5b018a5dd8d271273f4310734528 Mon Sep 17 00:00:00 2001 From: insanity Date: Mon, 2 Jul 2018 20:37:23 +0900 Subject: [PATCH] encrypt --- .../service/CentralNoAuthProbeService.java | 5 ++-- .../service/CentralSensorConfigService.java | 26 +++++++++---------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/loafle/overflow/central/module/noauthprobe/service/CentralNoAuthProbeService.java b/src/main/java/com/loafle/overflow/central/module/noauthprobe/service/CentralNoAuthProbeService.java index 0b70c9f..b1f8fe2 100644 --- a/src/main/java/com/loafle/overflow/central/module/noauthprobe/service/CentralNoAuthProbeService.java +++ b/src/main/java/com/loafle/overflow/central/module/noauthprobe/service/CentralNoAuthProbeService.java @@ -27,6 +27,8 @@ import com.loafle.overflow.service.central.probe.ProbeHostService; import com.loafle.overflow.service.central.probe.ProbeService; import com.fasterxml.jackson.databind.ObjectMapper; + +import org.apache.commons.lang.RandomStringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -36,7 +38,6 @@ import inet.ipaddr.IPAddressString; import java.io.IOException; import java.util.Date; import java.util.List; -import java.util.UUID; import javax.transaction.Transactional; @@ -156,7 +157,7 @@ public class CentralNoAuthProbeService implements NoAuthProbeService { DomainMember domainMember = domainMemberService.readByMemberEmail(memberEmail); Probe probe = new Probe(); - probe.setEncryptionKey(UUID.randomUUID().toString() + UUID.randomUUID().toString()); + probe.setEncryptionKey(RandomStringUtils.randomAlphanumeric(8)); probe.setProbeKey(GenerateKey.getKey()); probe.setDomain(new Domain(apiKey.getDomain().getId())); probe.setAuthorizeMember(new Member(domainMember.getMember().getId())); diff --git a/src/main/java/com/loafle/overflow/central/module/sensor/service/CentralSensorConfigService.java b/src/main/java/com/loafle/overflow/central/module/sensor/service/CentralSensorConfigService.java index edb7270..a235c42 100644 --- a/src/main/java/com/loafle/overflow/central/module/sensor/service/CentralSensorConfigService.java +++ b/src/main/java/com/loafle/overflow/central/module/sensor/service/CentralSensorConfigService.java @@ -69,15 +69,16 @@ public class CentralSensorConfigService { } private String toEncryptString(SensorConfig sensorConfig, String encryptKey) throws OverflowException { + try { String sensorConfigJsonStr = this.objectMapper.writeValueAsString(sensorConfig); // gzip - ByteArrayOutputStream obj = new ByteArrayOutputStream(); - GZIPOutputStream gzip = new GZIPOutputStream(obj); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + GZIPOutputStream gzip = new GZIPOutputStream(bos); gzip.write(sensorConfigJsonStr.getBytes("UTF-8")); gzip.flush(); gzip.close(); - byte[] compressedByte = obj.toByteArray(); + byte[] compressedByte = bos.toByteArray(); // DES encryption Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); @@ -98,22 +99,21 @@ public class CentralSensorConfigService { /* * private String testDecrypt(String encryptKey, String encrypted) throws * OverflowException { try { byte[] inputBytes = - * Base64.getDecoder().decode(encrypted.getBytes()); - * - * Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); SecretKeyFactory - * keyFactory = SecretKeyFactory.getInstance("DES"); DESKeySpec desKeySpec = new + * Base64.getDecoder().decode(encrypted); + * + * Cipher cipher = Cipher.getInstance("DES"); SecretKeyFactory keyFactory = + * SecretKeyFactory.getInstance("DES"); DESKeySpec desKeySpec = new * DESKeySpec(encryptKey.getBytes()); cipher.init(Cipher.DECRYPT_MODE, - * keyFactory.generateSecret(desKeySpec)); - * - * byte[] outputBytes = cipher.doFinal(inputBytes); - * + * keyFactory.generateSecret(desKeySpec)); byte[] outputBytes = + * cipher.doFinal(inputBytes); + * * GZIPInputStream gis = new GZIPInputStream(new * ByteArrayInputStream(outputBytes)); BufferedReader bf = new * BufferedReader(new InputStreamReader(gis, "UTF-8")); String result = ""; * String line; while ((line = bf.readLine()) != null) { result += line; } - * + * * return result; - * + * * } catch (Exception e) { throw new OverflowException("", e); } } */