From bebac2619eb4a4fec36537c5621804c18110d4ae Mon Sep 17 00:00:00 2001 From: gsd Date: Thu, 10 Aug 2023 13:34:14 +0300 Subject: [PATCH] crypto 4 discord --- .../java/app/services/db/DetectService.java | 23 +++++ src/main/java/app/utils/CryptedCookie.java | 31 ++++++ src/main/java/app/utils/CryptoMethods.java | 98 +++++++++++++++++++ src/main/java/app/utils/SteamIDConverter.java | 4 + src/test/java/app/utils/CryptoTest.java | 51 ++++++++++ 5 files changed, 207 insertions(+) create mode 100644 src/main/java/app/services/db/DetectService.java create mode 100644 src/main/java/app/utils/CryptedCookie.java create mode 100644 src/main/java/app/utils/CryptoMethods.java create mode 100644 src/test/java/app/utils/CryptoTest.java diff --git a/src/main/java/app/services/db/DetectService.java b/src/main/java/app/services/db/DetectService.java new file mode 100644 index 0000000..9e0ee21 --- /dev/null +++ b/src/main/java/app/services/db/DetectService.java @@ -0,0 +1,23 @@ +package app.services.db; + +import app.entities.other.SteamID; +import app.utils.SteamIDConverter; +import jakarta.persistence.EntityManager; +import jakarta.persistence.PersistenceContext; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class DetectService { + + @PersistenceContext + EntityManager entityManager; + + public List getAccountsPerSteamID(SteamID steamID) { + return entityManager.createNativeQuery("select `steam_id` FROM all_users_con_2 WHERE `connect_ip` in (SELECT `connect_ip` FROM `all_users_con_2` WHERE `steam_id` LIKE ?1) GROUP BY `steam_id`") + .setParameter(1, steamID.steam2) + .getResultStream().map(SteamIDConverter::getSteamID).toList(); + } + //SELECT CONCAT(SUBSTRING_INDEX(connect_ip,'.', 1),'.',SUBSTRING_INDEX(SUBSTRING_INDEX(connect_ip,'.', 2),'.',-1),'.',SUBSTRING_INDEX(SUBSTRING_INDEX(connect_ip,'.', 3),'.',-1),'.','%') FROM `all_users_con_2` WHERE `steam_id` LIKE 'STEAM_0:1:768781968'; +} diff --git a/src/main/java/app/utils/CryptedCookie.java b/src/main/java/app/utils/CryptedCookie.java new file mode 100644 index 0000000..6dc6ba7 --- /dev/null +++ b/src/main/java/app/utils/CryptedCookie.java @@ -0,0 +1,31 @@ +package app.utils; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component +public class CryptedCookie { + @Value("${backend.auth.crypto.key}") + private String key; + + @Value("${backend.auth.crypto.iv}") + private String iv; + + private final Logger logger = LoggerFactory.getLogger(CryptedCookie.class); + + + public boolean Validate(String value) { + return CryptoMethods.decrypt(value, key, iv) != null; + } + + public String ReadCh(String value) { + return CryptoMethods.decrypt(value, key, iv); + } + + public String Hashed(String value) { + return CryptoMethods.encrypt(value, key, iv); + } +} diff --git a/src/main/java/app/utils/CryptoMethods.java b/src/main/java/app/utils/CryptoMethods.java new file mode 100644 index 0000000..224cc21 --- /dev/null +++ b/src/main/java/app/utils/CryptoMethods.java @@ -0,0 +1,98 @@ +package app.utils; + +import javax.crypto.Cipher; +import javax.crypto.KeyGenerator; +import javax.crypto.SecretKey; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.SecretKeySpec; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; +import java.util.Base64; + +public class CryptoMethods { + + public static final String algorithm = "AES/CBC/PKCS5Padding"; + + public static String generateIv() { + byte[] iv = new byte[16]; + new SecureRandom().nextBytes(iv); + return convertIvToString(new IvParameterSpec(iv)); + } + + public static String generateKey(int n){ + try { + KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); + keyGenerator.init(n); + SecretKey key = keyGenerator.generateKey(); + return convertSecretKeyToString(key); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + public static String convertSecretKeyToString(SecretKey secretKey) throws NoSuchAlgorithmException { + byte[] rawData = secretKey.getEncoded(); + return Base64.getEncoder().encodeToString(rawData); + } + + public static String convertIvToString(IvParameterSpec iv) { + byte[] rawData = iv.getIV(); + return Base64.getEncoder().encodeToString(rawData); + } + + public static SecretKey convertStringToSecretKeyto(String encodedKey) { + byte[] decodedKey = Base64.getDecoder().decode(encodedKey); + return new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES"); + } + + public static IvParameterSpec convertStringToIv(String iv) { + byte[] decodedIv = Base64.getDecoder().decode(iv); + return new IvParameterSpec(decodedIv, 0, decodedIv.length); + } + + public static String encrypt(String input, String key, String iv) { + return encrypt(algorithm, input, convertStringToSecretKeyto(key), convertStringToIv(iv)); + } + + public static String encrypt(String algorithm, String input, String key, + String iv) { + return encrypt(algorithm, input, convertStringToSecretKeyto(key), convertStringToIv(iv)); + } + + public static String encrypt(String algorithm, String input, SecretKey key, + IvParameterSpec iv) { + try { + Cipher cipher = Cipher.getInstance(algorithm); + cipher.init(Cipher.ENCRYPT_MODE, key, iv); + byte[] cipherText = cipher.doFinal(input.getBytes()); + return Base64.getEncoder() + .encodeToString(cipherText); + } catch (Exception e) { + return null; + } + } + + public static String decrypt(String cipherText, String key, String iv) { + return decrypt(algorithm, cipherText, convertStringToSecretKeyto(key), convertStringToIv(iv)); + } + + public static String decrypt(String algorithm, String cipherText, String key, + String iv) { + return decrypt(algorithm, cipherText, convertStringToSecretKeyto(key), convertStringToIv(iv)); + } + + public static String decrypt(String algorithm, String cipherText, SecretKey key, + IvParameterSpec iv) { + try { + Cipher cipher = Cipher.getInstance(algorithm); + cipher.init(Cipher.DECRYPT_MODE, key, iv); + byte[] plainText = cipher.doFinal(Base64.getDecoder() + .decode(cipherText)); + return new String(plainText); + } catch (Exception e) { + return null; + } + } + +} diff --git a/src/main/java/app/utils/SteamIDConverter.java b/src/main/java/app/utils/SteamIDConverter.java index 15173cc..4e6b51a 100644 --- a/src/main/java/app/utils/SteamIDConverter.java +++ b/src/main/java/app/utils/SteamIDConverter.java @@ -18,6 +18,10 @@ public class SteamIDConverter { private final Pattern PatternCustomUrl = Pattern.compile("steamcommunity\\.com\\/id\\/([A-Za-z0-9-_]{2,32})"); private final Pattern PatternMaybeCustomUrl = Pattern.compile("^([A-Za-z0-9-_]{2,32})$"); + public static SteamID getSteamID(Object obj){ + return getSteamID((String) obj); + } + public static SteamID getSteamID(String text){ boolean isSteamID = false; diff --git a/src/test/java/app/utils/CryptoTest.java b/src/test/java/app/utils/CryptoTest.java new file mode 100644 index 0000000..9bd87f1 --- /dev/null +++ b/src/test/java/app/utils/CryptoTest.java @@ -0,0 +1,51 @@ +package app.utils; + +import org.junit.Test; + +public class CryptoTest { + final String input = "super secret string"; + + final String global_key = "dquvH4zht89h/ivv9CSS3g=="; + final String global_iv = "+ORfU8i3xRxP0kYEejQmtw=="; + final String global_ch = "GoeHW2BKvZyvl3Fu1/6K+ftH+hRK2Nuqk+0zqO0Vt0s="; + + @Test + public void Crypt() throws Exception { + String key = CryptoMethods.generateKey(128); + String iv = CryptoMethods.generateIv(); + System.out.println(key); + System.out.println(iv); + String ciptxt = CryptoMethods.encrypt( + CryptoMethods.algorithm, + input, + key, + iv + ); + System.out.println(ciptxt); + } + + @Test + public void DeCrypt() throws Exception { + String ch_input = global_ch; + String key = global_key; + String iv = global_iv; + System.out.println(key); + System.out.println(iv); + String out = CryptoMethods.decrypt(CryptoMethods.algorithm, ch_input, key, iv); + System.out.println(out); + } + + @Test + public void DeCryptWithBadCh() throws Exception{ + String key = global_key; + String iv = global_iv; + String ch_input = "GoeHW2BKvZyvl3Fu1/6K+ftH+hRK2Nfuqk+0zqO0Vt0s="; + String out = CryptoMethods.decrypt(CryptoMethods.algorithm, ch_input, key, iv); + System.out.println(out); + } + + @Test + public void CreateKeyAndIv() throws Exception{ + System.out.println(String.format("Key: %s\nIv: %s", CryptoMethods.generateKey(256), CryptoMethods.generateIv())); + } +}