栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

android非对称加密算法DSA

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

android非对称加密算法DSA

step1: D:workspaceDsaDemoappsrcmainjavacommondordsademoMainActivity.java

package com.mondor.dsademo;

import androidx.appcompat.app.AppCompatActivity;


import java.math.BigInteger;

import android.os.Bundle;
import android.util.Base64;
import android.util.Log;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String message = "Hi I think I have an A";
        String message2 = "Hi I think I have an A";
        Session session = Session.getInstance(true);
        Pair privateKeys = session.getPrivateKey();
        Pair sign = DSA.sign(true, message,
                session.getGlobalKeyG(), session.getGlobalKeyP(),
                session.getGlobalKeyQ(), privateKeys.getFirst());
        boolean isPass = DSA.verify(true, message2, sign.getFirst(),
                sign.getSecond(), session.getGlobalKeyG(),
                session.getGlobalKeyP(), session.getGlobalKeyQ(),
                privateKeys.getSecond());
        session.destroy();
    }
}

step2: D:workspaceDsaDemoappsrcmainjavacommondordsademoDSA.java

package com.mondor.dsademo;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.SecureRandom;



public class DSA {

    
    private static boolean debug;

    
    public static Pair sign(boolean deb,
                                                    String message, BigInteger publcG, BigInteger publicP,
                                                    BigInteger publicQ, BigInteger privateX) {
        debug = deb;
        debugMode("=== CREATING SIGNATURE ===", true);

        // K
        debugMode("Creating auxiliar variable K .......... ", false);
        BigInteger k = new BigInteger(publicQ.bitLength(), new SecureRandom());
        while (k.compareTo(publicQ) != -1 && k.compareTo(BigInteger.ZERO) != 1) {
            k = new BigInteger(publicQ.bitLength(), new SecureRandom());
        }
        debugMode("[OK]", true);

        // R
        debugMode("Creating R .......... ", false);
        BigInteger r = publcG.modPow(k, publicP).mod(publicQ);
        debugMode("[OK]", true);

        // S
        debugMode("Creating S .......... ", false);
        MessageDigest md = null;
        BigInteger s = BigInteger.ONE;
        try {
            md = MessageDigest.getInstance("SHA-1");
            md.update(message.getBytes());
            BigInteger hash = new BigInteger(md.digest());
            s = (k.modInverse(publicQ).multiply(hash.add(privateX.multiply(r))))
                    .mod(publicQ);
        } catch (Exception e) {
            e.printStackTrace();
        }
        debugMode("[OK]", true);
        Pair result = new Pair(
                r, s);
        return result;
    }

    
    public static Boolean verify(boolean deb, String message, BigInteger r,
                                 BigInteger s, BigInteger publcG, BigInteger publicP,
                                 BigInteger publicQ, BigInteger privateY) {
        debugMode("=== VERIFYING SIGNATURE ===", true);
        debug = deb;
        MessageDigest md;
        BigInteger v = BigInteger.ZERO;
        try {
            md = MessageDigest.getInstance("SHA-1");
            md.update(message.getBytes());
            BigInteger messagehash = new BigInteger(md.digest());
            debugMode("Creating W .......... ", false);
            BigInteger w = s.modInverse(publicQ);
            debugMode("[OK]", true);
            debugMode("Creating U1 .......... ", false);
            BigInteger u1 = messagehash.multiply(w).mod(publicQ);
            debugMode("[OK]", true);
            debugMode("Creating U2 .......... ", false);
            BigInteger u2 = r.multiply(w).mod(publicQ);
            debugMode("[OK]", true);
            debugMode("Creating V .......... ", false);
            v = ((publcG.modPow(u1, publicP).multiply(privateY.modPow(u2,
                    publicP))).mod(publicP)).mod(publicQ);
            debugMode("[OK]", true);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

        return v.compareTo(r) == 0;
    }

    
    private static void debugMode(String s, boolean ln) {
        if (debug) {
            if (ln) {
                System.out.println(s);
            } else {
                System.out.print(s);
            }
        }
    }

}

step3: D:workspaceDsaDemoappsrcmainjavacommondordsademoPair.java

package com.mondor.dsademo;


public class Pair {
    private F first; // first member of pair
    private S second; // second member of pair

    public Pair(F first, S second) {
        this.first = first;
        this.second = second;
    }

    public void setFirst(F first) {
        this.first = first;
    }

    public void setSecond(S second) {
        this.second = second;
    }

    public F getFirst() {
        return first;
    }

    public S getSecond() {
        return second;
    }
}

step4: D:workspaceDsaDemoappsrcmainjavacommondordsademoSession.java

package com.mondor.dsademo;


        import java.math.BigInteger;
        import java.security.SecureRandom;
        import java.util.Random;


public class Session {

    private static Session session;
    private static boolean debug;
    private BigInteger globalKeyP;
    private BigInteger globalKeyQ;
    private BigInteger globalKeyG;
    private static int L = 512;

    
    public static Session getInstance(boolean debugMode) {
        if (session == null)
            session = new Session(debugMode);

        return session;

    }

    
    private Session(boolean b) {
        if (b) {
            debug = true;
        } else {
            debug = false;
        }
        debugMode("=== CREATION OF GLOBAL KEYS ===", true);

        // ==Q==
        debugMode("Creating global key Q .......... ", false);

        globalKeyQ = new BigInteger(160, 20, new SecureRandom());
        debugMode("[OK]", true);

        // ==P==
        debugMode("Creating global key P (be patient).......... ", false);

        BigInteger tempP;
        BigInteger tempP2;
        SecureRandom rand = new SecureRandom();
        do {
            tempP = new BigInteger(L, 20, rand);
            tempP2 = tempP.subtract(BigInteger.ONE);
            tempP = tempP.subtract(tempP2.remainder(globalKeyQ));
        } while (!tempP.isProbablePrime(20) || tempP.bitLength() != L);

        BigInteger p = tempP;

        globalKeyP = p;
        debugMode("[OK]", true);

        // ==G==
        debugMode("Creating global key G .......... ", false);
        BigInteger p1 = globalKeyP.subtract(BigInteger.ONE);
        BigInteger exp = p1.divide(globalKeyQ);

        BigInteger tempg;
        Random random = new Random();
        do {
            tempg = new BigInteger(p1.bitLength(), random);
        } while (tempg.compareTo(p1) != -1
                && tempg.compareTo(BigInteger.ONE) != 1);
        globalKeyG = tempg.modPow(exp, p);
        debugMode("[OK]", true);
        System.out.println("");
        System.out.println("Q: " + globalKeyQ);
        System.out.println("P: " + globalKeyP);
        System.out.println("G: " + globalKeyG);
    }

    
    public BigInteger getGlobalKeyP() {
        return globalKeyP;
    }

    
    public BigInteger getGlobalKeyQ() {
        return globalKeyQ;
    }

    
    public BigInteger getGlobalKeyG() {
        return globalKeyG;
    }

    
    private void debugMode(String s, boolean ln) {
        if (debug) {
            if (ln) {
                System.out.println(s);
            } else {
                System.out.print(s);
            }
        }
    }

    
    public Pair getPrivateKey() {

        debugMode("=== CREATION OF PRIVATE KEY ===", true);
        // Private key
        debugMode("Creating private key X .......... ", false);
        BigInteger privK = new BigInteger(getGlobalKeyQ().bitLength(),
                new SecureRandom());
        while (privK.compareTo(globalKeyQ) != -1) {
            privK = new BigInteger(getGlobalKeyQ().bitLength(),
                    new SecureRandom());
        }
        debugMode("[OK]", true);
        debugMode("Creating private key Y .......... ", false);
        // Public key:
        BigInteger pubK = getGlobalKeyG().modPow(privK, getGlobalKeyP());

        Pair result = new Pair(
                privK, pubK);
        debugMode("[OK]", true);

        return result;
    }

    
    public void destroy() {
        debugMode("=== DESTROYING SESSION ===", true);
        globalKeyP = null;
        globalKeyQ = null;
        globalKeyG = null;
        session = null;
        debugMode("Session destroyed", true);
    }

}

end

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/877887.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号