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

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

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;
        }

实验截图:
在这里插入图片描述

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,我可以回答这个问题。首先,我们需要使用JDBC连接到数据库并创建一个用户表。以下是一个简单的示例: ```java // 加载JDBC驱动 Class.forName("com.mysql.jdbc.Driver"); // 连接到数据库 Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password"); // 创建用户表 Statement statement = connection.createStatement(); String sql = "CREATE TABLE users (id INT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(50), password VARCHAR(100))"; statement.executeUpdate(sql); ``` 接下来,我们需要实现用户注册和登录的方法,并且在存储密码时使用加密算法。以下是一个简单的示例: ```java // 用户注册 public void register(String username, String password) throws SQLException, NoSuchAlgorithmException { // 对密码进行加密 String hashedPassword = hashPassword(password); // 存储用户信息 String sql = "INSERT INTO users (username, password) VALUES (?, ?)"; PreparedStatement statement = connection.prepareStatement(sql); statement.setString(1, username); statement.setString(2, hashedPassword); statement.executeUpdate(); } // 用户登录 public boolean login(String username, String password) throws SQLException, NoSuchAlgorithmException { // 对密码进行加密 String hashedPassword = hashPassword(password); // 查询用户信息 String sql = "SELECT * FROM users WHERE username = ? AND password = ?"; PreparedStatement statement = connection.prepareStatement(sql); statement.setString(1, username); statement.setString(2, hashedPassword); ResultSet resultSet = statement.executeQuery(); // 如果用户存在,返回true,否则返回false return resultSet.next(); } // 密码加密 private String hashPassword(String password) throws NoSuchAlgorithmException { MessageDigest md = MessageDigest.getInstance("SHA-256"); md.update(password.getBytes()); byte[] bytes = md.digest(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < bytes.length; i++) { sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1)); } return sb.toString(); } ``` 以上示例是一个简单的实现,你可以根据实际需要进行修改和优化。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值