mirror of
https://github.com/ZSCNetSupportDept/WechatTicketSystem.git
synced 2025-10-30 01:46:18 +08:00
use RSA to encrypt login
This commit is contained in:
@@ -1,51 +0,0 @@
|
||||
package love.sola.netsupport.util;
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class AESUtil {
|
||||
|
||||
public static final byte[] initVector = "RandomInitVector".getBytes(StandardCharsets.UTF_8);
|
||||
public static final byte[] key = "$bitch@sola.love".getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
public static String encrypt(String value) {
|
||||
try {
|
||||
IvParameterSpec iv = new IvParameterSpec(initVector);
|
||||
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
|
||||
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
|
||||
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
|
||||
|
||||
byte[] encrypted = cipher.doFinal(value.getBytes());
|
||||
|
||||
return Base64.encodeBase64String(encrypted);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String decrypt(String encrypted) {
|
||||
try {
|
||||
IvParameterSpec iv = new IvParameterSpec(initVector);
|
||||
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
|
||||
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
|
||||
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
|
||||
|
||||
byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));
|
||||
|
||||
return new String(original);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,13 +12,17 @@ import javax.servlet.http.HttpSession;
|
||||
*/
|
||||
public class Checker {
|
||||
|
||||
public static boolean nonNull(Object... v) {
|
||||
for (Object o : v) if (o == null) return false;
|
||||
return true;
|
||||
public static boolean hasNull(Object... v) {
|
||||
for (Object o : v) if (o == null) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean authorized(HttpSession s, Command c) {
|
||||
return s != null && s.getAttribute("authorized") == c;
|
||||
}
|
||||
|
||||
public static boolean operator(HttpSession s) {
|
||||
return s != null && s.getAttribute("operator") != null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ public class Crypto {
|
||||
}
|
||||
|
||||
public static boolean check(String plain, String hash) {
|
||||
return BCrypt.checkpw(AESUtil.decrypt(plain), hash);
|
||||
return BCrypt.checkpw(RSAUtil.decrypt(plain), hash);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -23,11 +23,9 @@ public class ParseUtil {
|
||||
.append(lang("Ticket_Info_Id")).append(t.getId()).append("\n")
|
||||
.append(lang("Ticket_Info_Desc")).append(t.getDescription()).append("\n")
|
||||
.append(lang("Ticket_Info_Submit_Time")).append(dateFormat.format(t.getSubmitTime())).append("\n");
|
||||
if (t.getUpdateTime() != null) {
|
||||
sb.append(lang("Ticket_Info_Operator")).append(t.getOperator().getId()).append("\n");
|
||||
sb.append(lang("Ticket_Info_Remark")).append(t.getRemark()).append("\n");
|
||||
sb.append(lang("Ticket_Info_Update_Time")).append(dateFormat.format(t.getUpdateTime())).append("\n");
|
||||
}
|
||||
if (t.getOperator() != null) sb.append(lang("Ticket_Info_Operator")).append(t.getOperator().getId()).append("\n");
|
||||
if (t.getRemark() != null) sb.append(lang("Ticket_Info_Remark")).append(t.getRemark()).append("\n");
|
||||
if (t.getUpdateTime() != null) sb.append(lang("Ticket_Info_Update_Time")).append(dateFormat.format(t.getUpdateTime())).append("\n");
|
||||
sb.append(lang("Ticket_Info_Status")).append(Status.getLocalized(t.getStatus()));
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
61
src/main/java/love/sola/netsupport/util/RSAUtil.java
Normal file
61
src/main/java/love/sola/netsupport/util/RSAUtil.java
Normal file
@@ -0,0 +1,61 @@
|
||||
package love.sola.netsupport.util;
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.Key;
|
||||
import java.security.KeyPair;
|
||||
import java.security.KeyPairGenerator;
|
||||
|
||||
public class RSAUtil {
|
||||
|
||||
|
||||
public static Key publicKey;
|
||||
public static Key privateKey;
|
||||
public static String publicKey_s;
|
||||
public static String privateKey_s;
|
||||
|
||||
static {
|
||||
genKeyPair();
|
||||
}
|
||||
|
||||
public static void genKeyPair() {
|
||||
try {
|
||||
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
|
||||
kpg.initialize(1024);
|
||||
KeyPair kp = kpg.genKeyPair();
|
||||
publicKey = kp.getPublic();
|
||||
privateKey = kp.getPrivate();
|
||||
publicKey_s = Base64.encodeBase64String(publicKey.getEncoded());
|
||||
privateKey_s = Base64.encodeBase64String(privateKey.getEncoded());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static String encrypt(String value) {
|
||||
try {
|
||||
Cipher cipher = Cipher.getInstance("RSA");
|
||||
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
|
||||
byte[] encrypted = cipher.doFinal(value.getBytes(StandardCharsets.UTF_8));
|
||||
return Base64.encodeBase64String(encrypted);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String decrypt(String encrypted) {
|
||||
try {
|
||||
Cipher cipher = Cipher.getInstance("RSA");
|
||||
cipher.init(Cipher.DECRYPT_MODE, privateKey);
|
||||
byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));
|
||||
return new String(original, StandardCharsets.UTF_8);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user