一, 环境搭建:
1, 先安装开发工具:
推荐 idea, 下载 macOS 操作系统
idea 激活码点击这里汇聚了很多
2, 安装 Java 环境 jdk
下载链接
2.1, 一路双击, 继续安装, 输入密码就可以安装成功. 2.2, 检查终端 Java 是否安装成功
3, 安装 go 环境
官网下载安装包 https://golang.org/dl/
安装完毕会在 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 包
, 首先点开 File 文件下的 Project Structure
, 选择 Artifacts--> 点击蓝色的 "+"
, 选中 jar->From modules with dependencies
, 注意 Main Class 的添加, 此处就是选择你要生成的 jar 包的工程文件
,.MF 文件就是你生成 jar 包生成的签名信息, 第一次生成 jar 包, 会生成相应的. MF 签名文件, 若第二次再生成 jar 包, 会报错, 说已经存在, 只需将. MF 文件删除即可
, 选择输出的目录, 即 Output Directory
勾选 Build on make 点击 ok
,Build-->Make Project
验证 jar 包执行结果
8, 将该 jar 包放入 go 项目的文件下如下图我的 go 项目名称 TheWayToGo
执行成功, 则加密完成, 解密同样道理, 快去试试吧.
来源: https://www.qcloud.com/developer/article/1527993