12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- package thyyxxk.wxservice_server;
- import org.apache.commons.codec.binary.Base64;
- import org.bouncycastle.jce.provider.BouncyCastleProvider;
- import thyyxxk.wxservice_server.utils.SnowFlakeId;
- import javax.crypto.Cipher;
- import javax.crypto.spec.SecretKeySpec;
- import java.security.Security;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.UUID;
- public class Test {
- public static void main(String[] args) {
- Date now = new Date();
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- try {
- Date sdate = new SimpleDateFormat("yyyy-MM-dd").parse("2023-03-23");
- System.out.println(now.before(sdate));
- } catch (Exception e) {
- }
- //加密前:98134EAF0F594CD6A85CE3BB4367C72A&oao6f0y4oF7jd60QhzPzMD9C3bBU&37156
- //加密后:zTS83k5dG5jaDl0EmRsy35qt37Nliu+kFvebbJxrFddhH/f1/eP4UjDQmeoiBb9W0mbSBJg3nT4KDVYtqGzWNOPM5UyixrFI4Cv8YewVa6I=
- // String uoh = "98134EAF0F594CD6A85CE3BB4367C72A&oao6f0y4oF7jd60QhzPzMD9C3bBU&37156";
- // byte[] contentBytes = uoh.getBytes();
- //
- // String secret = "085181bf219749878035f575e0a1986a";
- // byte[] keyBytes = secret.getBytes();
- //
- // //加密
- // String en = encrypt(contentBytes, keyBytes);
- // System.out.println(en);
- // //解密,用于本地验证
- // byte[] enb = en.getBytes();
- // byte[] de = decrypt(enb, keyBytes);
- // String str = new String(de);
- // System.out.println(str);
- }
- /**
- * AES加密
- *
- * @param contentBytes 由uuid&openid&hospitalId拼接的明文
- * @param keyBytes 开放平台分配的appSecret密钥
- * @return
- */
- public static String encrypt(byte[] contentBytes, byte[] keyBytes) {
- try {
- //添加一个安全管理器提供者
- Security.addProvider(new BouncyCastleProvider());
- //根据一个字节数组构造一个 SecretKey
- SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
- //设置加密类型
- Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
- cipher.init(Cipher.ENCRYPT_MODE, key); //用于将 Cipher 初始化为加密模式的常量。
- byte[] tmp = cipher.doFinal(contentBytes); //操作加密或解密数据
- //返回Base64后的加密串
- return new String(Base64.encodeBase64(tmp));
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- /**
- * AES解密
- *
- * @param contentBytes 加密后的openId字符串
- * @param keyBytes 开放平台分配的appSecret密钥
- * @return
- */
- public static byte[] decrypt(byte[] contentBytes, byte[] keyBytes) {
- try {
- Security.addProvider(new BouncyCastleProvider());
- SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
- Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
- cipher.init(Cipher.DECRYPT_MODE, key);
- byte[] tmp = cipher.doFinal(Base64.decodeBase64(contentBytes));
- return tmp;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- }
|