Mac下golang实现Java加密方式调用
- 2019 年 10 月 27 日
- 筆記
一、环境搭建:
1、先安装开发工具:

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

执行成功,则加密完成,解密同样道理,快去试试吧。