package com.demo.common;

import com.demo.util.SecretUtil;
import com.reyun.Algorithm;


/**
 * 用于特殊处理加密
 */
public class SpecialSecret {

//    public static  final String dllPath = "C:\\eclipseWorkSpace\\deviceCheat\\lib\\cryptoEx.dll";

    public static  final String dllPath = "/data/application/deviceCheck/crypto/libcrypto.so";
    static {
        System.load(dllPath);
    }



    public static byte [] aes256EncodeB(byte [] data,byte [] key){
        Algorithm alg = new Algorithm();
        return alg.aes256EncodeB(data, key);
    }

    /**
     *
     * 解密数据
     * @param secretData
     * @param key
     * @return
     */
    public static String getData(String secretData, String key){
        Algorithm alg = new Algorithm();
        return alg.aes256Decode(secretData, key);
    }

    /**
     * aes加密data
     * @param data
     * @param key
     * @return
     */
    public static String aesData(String data, String key){
        Algorithm alg = new Algorithm();
        return alg.aes256Encode(data, key);
    }

    /**
     *
     *
     * 对于AES密钥的处理
     * 6字节随机数+真实密钥+8字节随机数->反转
     * 然后拿私钥钥进行RSA加密
     * @param str
     * @param privateKey
     * @return
     */
    public static String getSecretAesStr(String str, String privateKey) throws Exception{

        Algorithm alg = new Algorithm();
        String aesStr = SecretUtil.getRandNum(6) + str + SecretUtil.getRandNum(8);
        str = reverseString(aesStr);
        return  alg.rsaPrivateEncode(str, privateKey);
    }

    /**
     *
     * 对于RSA 公钥的处理
     *da
     * 6字节随机数+真实密钥+8字节随机数->反转
     * @param str
     * @return
     */
    public static String getSecretRSAStr(String str){

        String rsaStr = SecretUtil.getRandNum(6) + str + SecretUtil.getRandNum(8);
        return reverseString(rsaStr);
    }

    private static String reverseString(String str) {
        StringBuffer buffer = new StringBuffer(str);
        return buffer.reverse().toString();
    }



//    public static void main(String args[]) throws Exception{
//
////        System.out.println("开始测试-----------");
//////        System.load("C:\\eclipseWorkSpace\\mongodb-demo\\lib\\crypto.dll");
//////        DllAddUtil.addLibraryDir("C:\\eclipseWorkSpace\\mongodb-demo\\lib\\crypto.dll");
////
////        String dllPath = System.getProperty("user.dir") + "/lib/crypto.dll";
////        System.out.println(System.getProperty("user.dir"));
////
////        DllAddUtil.addLibraryDir("dllPath");
//////        System.out.println(System.getProperty("java.library.path"));
//////        System.loadLibrary("crypto");
////        System.load(dllPath);
////        Algorithm alg = new Algorithm();
////        String txt = "小红帽123";
////        String pubkey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCLtMtk2fLimNPRK5rG0jVBeX8kSO8CP8aMkeu7" +
////                "N94ieqQqhbBArhdA7b5/kd6durfY5dq7NWAPQAK8+/P3XE92qdkshcgX9bYqO9dvE1fSgtLZN1OK" +
////                "Pgm1quNZT5fqxOMQZwancRdqgt53aEjvjANtIoOCNV2QkPsl0a0icGmTXQIDAQAB";
////        String privateKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIu0y2TZ8uKY09ErmsbSNUF5fyRI" +
////                "7wI/xoyR67s33iJ6pCqFsECuF0Dtvn+R3p26t9jl2rs1YA9AArz78/dcT3ap2SyFyBf1tio7128T" +
////                "V9KC0tk3U4o+CbWq41lPl+rE4xBnBqdxF2qC3ndoSO+MA20ig4I1XZCQ+yXRrSJwaZNdAgMBAAEC" +
////                "gYBJ2EGeSDnDHobvzD2elCQEPMVAl7CGHtHDIapZplkrHmpB87JLLfoqnsAPzDnAY56j7jMkuzzQ" +
////                "MsR/wimdeVSimfqlgpTe1KNIdOkZnppAbEijqhngJhUXFNJMmhUyHSaRd1AQ5aYZoIsIyndP13TM" +
////                "RQyX883sbKISIEX7qajxPQJBANZi58PWH9Xg5nrNi6zm0bDvmQe9QIQR03Mv4pWcPmzIcbNCWDeK" +
////                "Nlp9Wqr66jDc7YUkxfAjnDuLsE8g9OzpGBMCQQCm0vORg+BF1QFcLpdG7lilqFZbqjl/hLFzeCAp" +
////                "4L4GrMqSFhYs6h/pWg9FYpPvSY8grZo4qgX97t+QAvHAr/TPAkAPvA6alvqbLfh8ht17gS1euRcY" +
////                "NFGp380PJTKSHHeQcF239GSZHvHCTnT7G5NYpr5gCUTlzRdIX4Y/KIIRaUI1AkARFIwnm+78d2Ca" +
////                "RZLrij+9Rk0BOZ6FddqNTgFNCFusZ0ieSHCgzJ/Tzoq9uegXmdpzqBKgNphCdZCiOAr0hY91AkAt" +
////                "tN1h4g3LA1fhq0MvwgtNb9fVdSDoqCOSELQjKsqQach99kaMApo3uBLpCypAyi2X99i31SDEH/XO" +
////                "ytCzS3IG";
////        String str = alg.rsaPrivateEncode(txt,privateKey);
////        System.out.println("rsa加密后："+str);
////
////        System.out.println("rsa解密后："+alg.rsaPublicDecode(str,pubkey));
////
////
////        System.out.println("结束测试-----------");
////
////        System.out.println("开始测试SHA-----------");
////        String shaStr  =  alg.sha1(txt);
////        System.out.println("sha加密后："+str);
////
////        System.out.println("结束测试SHA-----------");
//
//
////        System.out.println("开始测试aes-----------");
////        txt="123";
////        String aesKey = "5662e7c3d61fd5e0e67acd8bace6d1b0";
////        String aesStr  =  alg.aes256Encode(txt,aesKey);
////        System.out.println("aes加密后："+aesStr);
////        String aesdecodeStr  =  alg.aes256Decode(aesStr,aesKey);
////        System.out.println("aes解密后："+aesdecodeStr);
//
////        System.out.println("结束测试aes-----------");
//
//        String data =  "ajTGaaaaqJ1ymOOGL5UCvY0Q2CKsla==";
//        String aesKey = "5662e7c3d61fd5e0e67acd8bace6d1b0ef9907bd408411d3732fe2959c3e6cc8";
//        String result = SpecialSecret.aesData(data, aesKey);
//        System.out.println("result is "+ result);
//
//
//
//    }

}
