reghao преди 3 години
родител
ревизия
d62e038269

+ 96 - 0
jdk/src/main/java/cn/reghao/jutil/jdk/security/crypto/AesEncrypt.java

@@ -0,0 +1,96 @@
+package cn.reghao.jutil.jdk.security.crypto;
+
+import javax.crypto.Cipher;
+import javax.crypto.KeyGenerator;
+import javax.crypto.SecretKey;
+import javax.crypto.spec.SecretKeySpec;
+import java.nio.charset.StandardCharsets;
+import java.security.NoSuchAlgorithmException;
+import java.security.SecureRandom;
+
+/**
+ * 对称加密
+ *
+ * @author reghao
+ * @date 2022-01-16 00:21:15
+ */
+public class AesEncrypt {
+    private static final String ALGORITHM = "AES";
+    private static final Integer KEY_LENGTH = 128;
+    private static final String CHARSET = "utf-8";
+
+    private static SecretKeySpec generateKey(String password) throws NoSuchAlgorithmException {
+        SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
+        secureRandom.setSeed(password.getBytes(StandardCharsets.UTF_8));
+
+        KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
+        keyGenerator.init(KEY_LENGTH, secureRandom);
+        SecretKey secretKey = keyGenerator.generateKey();
+        byte[] encFormat = secretKey.getEncoded();
+        return new SecretKeySpec(encFormat, ALGORITHM);
+    }
+
+    public static String encrypt(String pText, String password) throws Exception {
+        SecretKeySpec key = generateKey(password);
+        Cipher cipher = Cipher.getInstance(ALGORITHM);
+        byte[] pBytes = pText.getBytes(CHARSET);
+        //IvParameterSpec iv = new IvParameterSpec(password.getBytes(StandardCharsets.UTF_8));
+        cipher.init(Cipher.ENCRYPT_MODE, key);
+
+        byte[] cBytes = cipher.doFinal(pBytes);
+        return bytes2Hex(cBytes);
+    }
+
+    public static byte[] encrypt(byte[] pBytes, String password) throws Exception {
+        SecretKeySpec key = generateKey(password);
+        Cipher cipher = Cipher.getInstance(ALGORITHM);
+        //IvParameterSpec iv = new IvParameterSpec(password.getBytes(StandardCharsets.UTF_8));
+        cipher.init(Cipher.ENCRYPT_MODE, key);
+
+        byte[] cBytes = cipher.doFinal(pBytes);
+        return cBytes;
+    }
+
+    private static String bytes2Hex(byte[] bytes) {
+        StringBuilder sb = new StringBuilder();
+        for (byte aByte : bytes) {
+            String hex = Integer.toHexString(aByte & 0xff);
+            if (hex.length() == 1) {
+                hex = '0' + hex;
+            }
+            sb.append(hex.toLowerCase());
+        }
+        return sb.toString();
+    }
+
+    public static String decrypt(String cText, String password) throws Exception {
+        SecretKeySpec key = generateKey(password);
+        Cipher cipher = Cipher.getInstance(ALGORITHM);
+        //IvParameterSpec iv = new IvParameterSpec(password.getBytes(StandardCharsets.UTF_8));
+        cipher.init(Cipher.DECRYPT_MODE, key);
+
+        byte[] cBytes = hex2Bytes(cText);
+        byte[] pBytes = cipher.doFinal(cBytes);
+        return new String(pBytes, CHARSET);
+    }
+
+    public static byte[] decrypt(byte[] cBytes, String password) throws Exception {
+        SecretKeySpec key = generateKey(password);
+        Cipher cipher = Cipher.getInstance(ALGORITHM);
+        //IvParameterSpec iv = new IvParameterSpec(password.getBytes(StandardCharsets.UTF_8));
+        cipher.init(Cipher.DECRYPT_MODE, key);
+
+        byte[] pBytes = cipher.doFinal(cBytes);
+        return pBytes;
+    }
+
+    private static byte[] hex2Bytes(String hex) {
+        byte[] bytes = new byte[hex.length()/2];
+        for (int i = 0;i < hex.length()/2; i++) {
+            int high = Integer.parseInt(hex.substring(i*2, i*2+1), 16);
+            int low = Integer.parseInt(hex.substring(i*2+1, i*2+2), 16);
+            bytes[i] = (byte) (high * 16 + low);
+        }
+        return bytes;
+    }
+}

+ 93 - 0
jdk/src/main/java/cn/reghao/jutil/jdk/security/crypto/RsaEncrypt.java

@@ -0,0 +1,93 @@
+package cn.reghao.jutil.jdk.security.crypto;
+
+import cn.reghao.jutil.jdk.text.TextFile;
+
+import javax.crypto.Cipher;
+import java.io.File;
+import java.io.IOException;
+import java.security.*;
+import java.security.interfaces.RSAPrivateKey;
+import java.security.interfaces.RSAPublicKey;
+import java.security.spec.EncodedKeySpec;
+import java.security.spec.PKCS8EncodedKeySpec;
+import java.security.spec.X509EncodedKeySpec;
+import java.util.Base64;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * 非对称加密
+ *
+ * @author reghao
+ * @date 2022-01-16 00:21:15
+ */
+public class RsaEncrypt {
+    private static final String ALGORITHM = "RSA";
+    private static final Integer KEY_LENGTH = 2048;
+    private static final String CHARSET = "utf-8";
+
+    private static Map<String, String> genKeyPair() throws NoSuchAlgorithmException, IOException {
+        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);
+        keyPairGenerator.initialize(KEY_LENGTH, new SecureRandom());
+        KeyPair keyPair = keyPairGenerator.generateKeyPair();
+
+        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
+        String privateKeyStr = Base64.getEncoder().encodeToString(privateKey.getEncoded());
+        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
+        String publicKeyStr = Base64.getEncoder().encodeToString(publicKey.getEncoded());
+
+        String prikey = "rsa.prikey";
+        String pubkey = "rsa.pubkey";
+        TextFile textFile = new TextFile();
+        textFile.write(new File(prikey), privateKeyStr);
+        textFile.write(new File(pubkey), publicKeyStr);
+        Map<String, String> keyMap = new HashMap<>();
+        keyMap.put("privateKey", privateKeyStr);
+        keyMap.put("publicKey", publicKeyStr);
+        return keyMap;
+    }
+
+    public static String encrypt(String pText, String publicKey) throws Exception {
+        byte[] decoded = Base64.getDecoder().decode(publicKey);
+        EncodedKeySpec encodedKeySpec = new X509EncodedKeySpec(decoded);
+        RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance(ALGORITHM).generatePublic(encodedKeySpec);
+
+        Cipher cipher = Cipher.getInstance(ALGORITHM);
+        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
+        byte[] cBytes = cipher.doFinal(pText.getBytes(CHARSET));
+        return Base64.getEncoder().encodeToString(cBytes);
+    }
+
+    public static String encrypt(byte[] pText, String publicKey) throws Exception {
+        byte[] decoded = Base64.getDecoder().decode(publicKey);
+        EncodedKeySpec encodedKeySpec = new X509EncodedKeySpec(decoded);
+        RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance(ALGORITHM).generatePublic(encodedKeySpec);
+
+        Cipher cipher = Cipher.getInstance(ALGORITHM);
+        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
+        byte[] cBytes = cipher.doFinal(pText);
+        return Base64.getEncoder().encodeToString(cBytes);
+    }
+
+    public static String decrypt(String cText, String privateKey) throws Exception {
+        byte[] decoded = Base64.getDecoder().decode(privateKey);
+        EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(decoded);
+        RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance(ALGORITHM).generatePrivate(encodedKeySpec);
+
+        Cipher cipher = Cipher.getInstance(ALGORITHM);
+        cipher.init(Cipher.DECRYPT_MODE, priKey);
+        byte[] cBytes = Base64.getDecoder().decode(cText);
+        return new String(cipher.doFinal(cBytes));
+    }
+
+    public static String decrypt(byte[] cText, String privateKey) throws Exception {
+        byte[] decoded = Base64.getDecoder().decode(privateKey);
+        EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(decoded);
+        RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance(ALGORITHM).generatePrivate(encodedKeySpec);
+
+        Cipher cipher = Cipher.getInstance(ALGORITHM);
+        cipher.init(Cipher.DECRYPT_MODE, priKey);
+        byte[] cBytes = Base64.getDecoder().decode(cText);
+        return new String(cipher.doFinal(cBytes));
+    }
+}