Mac下golang實現Java加密方式調用

  • 2019 年 10 月 27 日
  • 筆記

一、環境搭建:

1、先安裝開發工具:

推薦idea,下載macOS作業系統

idea激活碼點擊這裡匯聚了很多

2、安裝Java環境jdk

下載鏈接

2.1、一路雙擊,繼續安裝,輸入密碼就可以安裝成功。

2.2、檢查終端Java是否安裝成功

3、安裝go環境

官網下載安裝包

安裝完畢會在home目錄下usr/local/go默認的安裝目錄。然後這個時候可以配置下環境變數

~/.bash_profile  export GOPATH=$HOME/go  保存退出,然後激活配置文件  source ~/.bash_profile  測試下是否安裝成功  這裡注意如果提示你許可權不夠的話需要使用sudo去執行  比如你現在創建個文件夾在go的安裝目錄s r c下面  sudo mkdir test  sudo vim hello.go  sudo build hello.go  sudo go run hello.go  然後可以看到輸出了hello world。

4、開發加密Java類

package com.xcxyz.cipher;    import javax.crypto.Cipher;  import java.security.Key;  import java.security.Security;    public class CipherEncryptUtil {      private Cipher encryptCipher = null;        public static String byteArr2HexStr(byte[] arrB) throws Exception {          int iLen = arrB.length;          StringBuilder sb = new StringBuilder(iLen * 2);          for (int i = 0; i < iLen; i++) {              int intTmp = arrB[i];              while (intTmp < 0) {                  intTmp = intTmp + 256;              }              if (intTmp < 16) {                  sb.append("0");              }              sb.append(Integer.toString(intTmp, 16));          }          return sb.toString();      }        public CipherEncryptUtil(String strKey) throws Exception {          Security.addProvider(new com.sun.crypto.provider.SunJCE());          Key key = getKey(strKey.getBytes());          encryptCipher = Cipher.getInstance("DES");          encryptCipher.init(Cipher.ENCRYPT_MODE, key);      }        public byte[] encrypt(byte[] arrB) throws Exception {          return encryptCipher.doFinal(arrB);      }        public String encrypt(String strIn) throws Exception {          return byteArr2HexStr(encrypt(strIn.getBytes()));      }        private Key getKey(byte[] arrBTmp) throws Exception {          byte[] arrB = new byte[8];          for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {              arrB[i] = arrBTmp[i];          }          Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");          return key;      }        public static void main(String[] args) throws Exception {          CipherEncryptUtil desUtils = new CipherEncryptUtil("秘鑰");          String encryptStr = desUtils.encrypt("加密字元串");          System.out.println(encryptStr);      }  }  

5、開發解密Java類

package com.xcxyz.cipher;    import javax.crypto.Cipher;  import java.security.Key;  import java.security.Security;    public class CipherDecruptUtil {      private Cipher decryptCipher = null;        public static byte[] hexStr2ByteArr(String strIn) throws Exception {          byte[] arrB = strIn.getBytes();          int iLen = arrB.length;  // 兩個字元表示一個位元組,所以位元組數組長度是字元串長度除以2          byte[] arrOut = new byte[iLen / 2];          for (int i = 0; i < iLen; i = i + 2) {              String strTmp = new String(arrB, i, 2);              arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);          }          return arrOut;      }        public CipherDecruptUtil(String strKey) throws Exception {          Security.addProvider(new com.sun.crypto.provider.SunJCE());          Key key = getKey(strKey.getBytes());          decryptCipher = Cipher.getInstance("DES");          decryptCipher.init(Cipher.DECRYPT_MODE, key);      }        public byte[] decrypt(byte[] arrB) throws Exception {          return decryptCipher.doFinal(arrB);      }        public String decrypt(String strIn) throws Exception {          return new String(decrypt(hexStr2ByteArr(strIn)));      }          private Key getKey(byte[] arrBTmp) throws Exception {          byte[] arrB = new byte[8];          for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {              arrB[i] = arrBTmp[i];          }          Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");          return key;      }          public static void main(String[] args) throws Exception {          CipherDecruptUtil desUtils = new CipherDecruptUtil("秘鑰");          String encryptStr = desUtils.decrypt("bfffa5db87cf3cdd1d75d86e3738ff6f");          System.out.println(encryptStr);      }  }  

6、寫一個main函數

package com.xcxyz;    import com.xcxyz.cipher.CipherEncryptUtil;    public class Main {        public static void main(String[] args) throws Exception{  	// write your code here          if (args.length != 2){              throw new Exception("must input two args for key and strIn");          }          CipherEncryptUtil encrypUtils = new CipherEncryptUtil(args[0]);          String encryptStr = encrypUtils.encrypt(args[1]);          System.out.println(encryptStr);    //        CipherEncryptUtil encrypUtils = new CipherEncryptUtil("秘鑰");  //        String encryptStr = encrypUtils.encrypt("加密字元串");  //        System.out.println(encryptStr);  //  //        CipherDecruptUtil decruptUtils = new CipherDecruptUtil("秘鑰");  //        String decryptStr = decruptUtils.decrypt(encryptStr);  //        System.out.println(decryptStr);      }  }  

7、執行idea導出Java可執行jar包

1、首先點開File文件下的Project Structure  2、選擇Artifacts—->點擊藍色的「+」  3、選中jar—>From modules with dependencies  4、注意Main Class的添加,此處就是選擇你要生成的jar包的工程文件  5、.MF文件就是你生成jar包生成的簽名資訊,第一次生成jar包,會生成相應的.MF簽名文件,若第二次再生成jar包,會報錯,說已經存在,只需將.MF文件刪除即可  6、選擇輸出的目錄,即Output Directory  7.勾選Build on make 點擊ok  8、Build——>Make Project  

驗證jar包執行結果

8、將該jar包放入go項目的文件下如下圖我的go項目名稱TheWayToGo

執行成功,則加密完成,解密同樣道理,快去試試吧。