欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

利用JDBC密码学库实现ECC椭圆曲线加密算法

程序员文章站 2022-05-11 20:29:30
...

最近实验室有需求写一个ECC的实现代码。今天刚写好一个简陋的实现!!先贴核心代码,等有时间了再来写思考过程!
网上可循的代码太少了!欢迎大家一起在评论区讨论!

ECC加解密算法:
利用JDBC密码学库实现ECC椭圆曲线加密算法

//系统初始化
public static Pairing initPairing(String parameter) {
        System.out.println("系统正在导入椭圆曲线的相关参数……");
        //读入参数
        Pairing pairing = PairingFactory.getPairing(parameter);

        System.out.println("系统已经导入完毕");
        return pairing;
    }
    public static Field initG_1(Pairing pairing) {
        System.out.println("系统正在产生椭圆曲线……");
        //读入参数
        Field G1 = pairing.getG1();

        System.out.println("系统已经产生椭圆曲线");
        return G1;
    }
    public static Element initG(Field G1) {
        System.out.println("系统正在挑选生成元点G……");
        //挑选生成元点G
        Element G = G1.newRandomElement().getImmutable();
        System.out.println("系统已经挑选好生成元点G");
        return G;
    }

    public static Element initP_t(Field G1) {
        System.out.println("系统正在挑选随机点P_t……");
        //挑选随机点P_t
        Element P_t = G1.newRandomElement().getImmutable();
        System.out.println("系统已经挑选好随机点P_t");
        return P_t;
    }
//利用**生成算法生成公私钥对
    public static void KeyGenerator(Element G,Field G_1) throws Exception {
        //随机选择一个整数n_b,作为私钥
        Random random = new Random();
        BigInteger n_b = new BigInteger(160, 160,random);//大整数作为私钥1
        //构造公钥P_b
        Element P_b = G.duplicate().mul(n_b);
        byte[] b = P_b.toCanonicalRepresentation();
        System.out.println("\n");
        //把公钥和私钥分别存储在publicKey.key和privateKey.key文件里
        String path = new File("").getCanonicalPath();
        out(path + "\\privateKey.key", n_b.toString());
        out(path + "\\publicKey.key", Base64.getEncoder().encodeToString(P_b.toCanonicalRepresentation()) + ",,,,,," + Base64.getEncoder().encodeToString(G.toCanonicalRepresentation()));
        System.out.println("你的公钥存放在:" + path + "\\publicKey.key");
        System.out.println("你的私钥存放在:" + path + "\\privateKey.key");
    }
//实现加密过程
public static String encrypt(Element P_b, String data, BigInteger k, Element P_t, Element G){
        try {
            byte[] datasource=data.getBytes("utf8");
            String CArray = "A";
            //计算P_1
            Element P_1 = G.duplicate().getImmutable().mul(k);
            System.out.println("加密过程中计算出的P_1:"+ P_1);
            //计算P_2
            Element P_2 = P_b.duplicate().getImmutable().mul(k);
            System.out.println("加密过程中计算出的P_2:"+ P_2);
            //计算P_end
            Element P_end = P_t.add(P_2);
            System.out.println("加密过程中计算出的P_end:"+ P_end);
            //计算密文C
            String[] p_txy = P_t.toString().split(",");
            BigInteger p_tx = new BigInteger(p_txy[0]);
            BigInteger p_ty = new BigInteger(p_txy[1]);
            for(int i=0;i<datasource.length;i++)
            {
                BigInteger M = new BigInteger(datasource[i]+"");
                BigInteger C_mid = M.multiply(p_tx).add(p_ty);

                CArray = CArray +","+C_mid.toString();
            }
            CArray = CArray + ",,"+Base64.getEncoder().encodeToString(P_1.toCanonicalRepresentation())+",,"+ Base64.getEncoder().encodeToString(P_end.toCanonicalRepresentation());
            return Base64.getEncoder().encodeToString(CArray.getBytes());
        }
 public static String decrypt(BigInteger Privatekey, String data, Field G_1,Element G) {
        try {
            String ciphertext= new String(Base64.getDecoder().decode(data),"utf8");
            //分解密文
            String[] CS=ciphertext.split(",,");
            String m = "";
            //取出P_t+kP_b
            Element P_end = G_1.newElementFromBytes(Base64.getDecoder().decode(CS[2]));
            //取出P_1
            Element P_1 = G_1.newElementFromBytes(Base64.getDecoder().decode(CS[1]));
            //计算P_t
            Element P_t = P_end.getImmutable().sub(P_1.getImmutable().mul(Privatekey));
            System.out.println("解密过程中计算出的P_t:"+ P_t);
            //计算明文M
            String[] p_txy = P_t.toString().split(",");
            BigInteger p_tx = new BigInteger(p_txy[0]);
            BigInteger p_ty = new BigInteger(p_txy[1]);
            //取出密文c
            String[] Plaintext = CS[0].split(",");
            for(int i=1;i<Plaintext.length;i++){
                BigInteger C = new BigInteger(Plaintext[i]);
                BigInteger M_mid = C.subtract(p_ty).divide(p_tx);
                m = m+new String(M_mid.toByteArray(),"GBK");;
            }
            return m;
        }

实验截图:
利用JDBC密码学库实现ECC椭圆曲线加密算法

相关标签: 密码学 算法