android非对称加密算法DSA

这篇博客详细介绍了在Android环境中实现非对称加密算法DSA的步骤,包括MainActivity.java、DSA.java、Pair.java和Session.java四个关键文件的使用,为Android开发者提供了哈希算法之外的另一种安全通信手段。
摘要由CSDN通过智能技术生成

step1: D:\workspace\DsaDemo\app\src\main\java\com\mondor\dsademo\MainActivity.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<BigInteger, BigInteger> privateKeys = session.getPrivateKey();
        Pair<BigInteger, BigInteger> 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:\workspace\DsaDemo\app\src\main\java\com\mondor\dsademo\DSA.java

package com.mondor.dsademo;

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


/**
 * @author robertomm
 *
 */
public class DSA {
   

    /*
     * Static methods -> (M,r,s) sign(M, claves) Boolean verify(M, r, s)
     */
    private static boolean debug;

    /**
     * Method to create the signature
     *
     * @param deb
     *            , boolean for activate the debug mode
     * @param message
     *            , string to make the signature
     * @param publcG
     *            , the global public g key
     * @param publicP
     *            , the global public p key
     * @param publicQ
     *            , the global public q key
     * @param privateX
     *            , the personal private x key
     * @return Pair<String, Pair<BigInteger, BigInteger>>, Pair of the message
     *         and another Pair with (r,s)
     */
    public static Pair<BigInteger, BigInteger> 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值