xjar 解密不超过15行代码

在使用xjar加密的时候,肯定也有需要解密的时候
代码如下:
首先maven依赖:

  <dependency>
            <groupId>com.github.core-libgroupId>
            <artifactId>xjarartifactId>
            <version>4.0.1version>
        dependency>

如果你的包是这样的结构:
在这里插入图片描述
生成xjar.go 和 application-1.0.0.xjar
其中xjar.go存的内容入下:

package main

import (
	"bytes"
	"crypto/md5"
	"crypto/sha1"
	"errors"
	"hash"
	"io"
	"os"
	"os/exec"
	"path/filepath"
)

var xJar = XJar{
	md5:  []byte{128, 17, 185, 6, 73, 101, 143, 126, 58, 0, 111, 99, 206, 32, 219, 215},
	sha1: []byte{47, 158, 202, 147, 45, 102, 166, 178, 60, 160, 242, 113, 40, 106, 30, 138, 11, 22, 161, 5},
}

var xKey = XKey{
	algorithm: []byte{65, 69, 83, 47, 67, 66, 67, 47, 80, 75, 67, 83, 53, 80, 97, 100, 100, 105, 110, 103},
	keysize:   []byte{49, 50, 56},
	ivsize:    []byte{49, 50, 56},
	password:  []byte{105, 111, 46, 120, 106, 97, 114},
}

func main() {
	// search the jar to start
	jar, err := JAR(os.Args)
	if err != nil {
		panic(err)
	}

	// parse jar name to absolute path
	path, err := filepath.Abs(jar)
	if err != nil {
		panic(err)
	}

	// verify jar with MD5
	MD5, err := MD5(path)
	if err != nil {
		panic(err)
	}
	if bytes.Compare(MD5, xJar.md5) != 0 {
		panic(errors.New("invalid jar with MD5"))
	}

	// verify jar with SHA-1
	SHA1, err := SHA1(path)
	if err != nil {
		panic(err)
	}
	if bytes.Compare(SHA1, xJar.sha1) != 0 {
		panic(errors.New("invalid jar with SHA-1"))
	}

	// start java application
	java := os.Args[1]
	args := os.Args[2:]
	key := bytes.Join([][]byte{
		xKey.algorithm, {13, 10},
		xKey.keysize, {13, 10},
		xKey.ivsize, {13, 10},
		xKey.password, {13, 10},
	}, []byte{})
	cmd := exec.Command(java, args...)
	cmd.Stdin = bytes.NewReader(key)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	err = cmd.Run()
	if err != nil {
		panic(err)
	}
}

// find jar name from args
func JAR(args []string) (string, error) {
	var jar string

	l := len(args)
	for i := 1; i < l-1; i++ {
		arg := args[i]
		if arg == "-jar" {
			jar = args[i+1]
		}
	}

	if jar == "" {
		return "", errors.New("unspecified jar name")
	}

	return jar, nil
}

// calculate file's MD5
func MD5(path string) ([]byte, error) {
	return HASH(path, md5.New())
}

// calculate file's SHA-1
func SHA1(path string) ([]byte, error) {
	return HASH(path, sha1.New())
}

// calculate file's HASH value with specified HASH Algorithm
func HASH(path string, hash hash.Hash) ([]byte, error) {
	file, err := os.Open(path)

	if err != nil {
		return nil, err
	}

	_, _err := io.Copy(hash, file)
	if _err != nil {
		return nil, _err
	}

	sum := hash.Sum(nil)

	return sum, nil
}

type XJar struct {
	md5  []byte
	sha1 []byte
}

type XKey struct {
	algorithm []byte
	keysize   []byte
	ivsize    []byte
	password  []byte
}

找到其中的XKey里面的password复制出来:
重点来了:

  public static void main(String[] args) {
        String str = "{\n" +//xjar.go文件内容
                "\talgorithm: []byte{65, 69, 83, 47, 67, 66, 67, 47, 80, 75, 67, 83, 53, 80, 97, 100, 100, 105, 110, 103},\n" +
                "\tkeysize:   []byte{49, 50, 56},\n" +
                "\tivsize:    []byte{49, 50, 56},\n" +
                "\tpassword:  []byte{105, 111, 46, 120, 106, 97, 114},\n" +
                "}";

        byte[] bytes = {105, 111, 46, 120, 106, 97, 114};//password内容拿出来直接转换
        String xKey = new String(bytes);
        try {
            XCryptos.decrypt("F:/724/nativesphere-cicd-1.0.0.xjar"//源文件,"F:/724/7/a.jar"//目标文件,需要在你要指定的文件夹创建一个x.jar的文件,用于接收输出内容,xKey);
        } catch (Exception e) {
            System.out.println("qnmd"+e);
            throw new RuntimeException(e);
        }
    }

你可能感兴趣的:(java)