elgamalsign.java

来自「ElGamal method is an aymetric method to 」· Java 代码 · 共 40 行

JAVA
40
字号
package elgamal;import java.math.BigInteger;import java.util.Random;public class ElGamalSign {    private BigInteger r;    private BigInteger s;    private static final BigInteger UN = BigInteger.ONE;    public ElGamalSign(Parameter params,PrivateKey x,byte[]M){                sha256 sh=new sha256(M);        int[]sha=sh.getHash();        byte[]hash=new byte[32];        int i=0;        for(int j=0;j<8;j++){            hash[i++]=(byte)(sha[j] & 0xFF);            hash[i++]=(byte)((sha[j]>>>8) & 0xFF);            hash[i++]=(byte)((sha[j]>>>16) & 0xFF);            hash[i++]=(byte)((sha[j]>>>24) & 0xFF);        }        BigInteger m=new BigInteger(hash);        BigInteger k=new BigInteger(params.getP().bitLength()-1,new Random());        BigInteger p1=params.getP().subtract(UN);        while(!k.gcd(p1).equals(UN)){            k=new BigInteger(params.getP().bitLength()-1,new Random());        }        r=params.getG().modPow(k,params.getP());        k=k.modInverse(p1);        s=k.multiply(m.subtract(r.multiply(x.getX()))).mod(p1);    }        public BigInteger getR(){        return r;    }    public BigInteger getS(){        return s;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?