⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gost3410parametersgenerator.java

📁 kmlnjlkj nlkjlkjkljl okopokipoipo oipipipo i
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.bouncycastle.crypto.generators;import org.bouncycastle.crypto.params.GOST3410Parameters;import org.bouncycastle.crypto.params.GOST3410ValidationParameters;import java.math.BigInteger;import java.security.SecureRandom;/** * generate suitable parameters for GOST3410. */public class GOST3410ParametersGenerator{    private int             size;    private int             typeproc;    private SecureRandom    init_random;    private static final BigInteger ONE = BigInteger.valueOf(1);    private static final BigInteger TWO = BigInteger.valueOf(2);    /**     * initialise the key generator.     *     * @param size size of the key     * @param typeproc type procedure A,B = 1;  A',B' - else     * @param random random byte source.     */    public void init(        int             size,        int             typeproc,        SecureRandom    random)    {        this.size = size;        this.typeproc = typeproc;        this.init_random = random;    }    //Procedure A    private int procedure_A(int x0, int c,  BigInteger[] pq, int size)    {        //Verify and perform condition: 0<x<2^16; 0<c<2^16; c - odd.        while(x0<0 || x0>65536)        {            x0 = init_random.nextInt()/32768;        }        while((c<0 || c>65536) || (c/2==0))        {            c = init_random.nextInt()/32768 + 1;        }        BigInteger C = new BigInteger(Integer.toString(c));        BigInteger constA16 = new BigInteger("19381");        //step1        BigInteger[] y = new BigInteger[1]; // begin length = 1        y[0] = new BigInteger(Integer.toString(x0));        //step 2        int[] t = new int[1]; // t - orders; begin length = 1        t[0] = size;        int s = 0;        for (int i=0; t[i]>=17; i++)        {            // extension array t            int tmp_t[] = new int[t.length + 1];             ///////////////            System.arraycopy(t,0,tmp_t,0,t.length);          //  extension            t = new int[tmp_t.length];                       //  array t            System.arraycopy(tmp_t, 0, t, 0, tmp_t.length);  ///////////////            t[i+1] = t[i]/2;            s = i+1;        }        //step3        BigInteger p[] = new BigInteger[s+1];        p[s] = new BigInteger("8003",16); //set min prime number length 16 bit        int m = s-1;  //step4        for (int i=0; i<s; i++)        {            int rm = t[m]/16;  //step5     step6: for(;;)            {                //step 6                BigInteger tmp_y[] = new BigInteger[y.length];  ////////////////                System.arraycopy(y,0,tmp_y,0,y.length);         //  extension                y = new BigInteger[rm+1];                       //  array y                System.arraycopy(tmp_y,0,y,0,tmp_y.length);     ////////////////                for (int j=0; j<rm; j++)                {                    y[j+1] = (y[j].multiply(constA16).add(C)).mod(TWO.pow(16));                }                //step 7                BigInteger Ym = new BigInteger("0");                for (int j=0; j<rm; j++)                {                    Ym = Ym.add(y[j].multiply(TWO.pow(16*j)));                }                y[0] = y[rm]; //step 8                //step 9                BigInteger N = TWO.pow(t[m]-1).divide(p[m+1]).                                   add((TWO.pow(t[m]-1).multiply(Ym)).                                       divide(p[m+1].multiply(TWO.pow(16*rm))));                if (N.mod(TWO).compareTo(ONE)==0)                 {                    N = N.add(ONE);                }                int k = 0; //step 10        step11: for(;;)                {                    //step 11                    p[m] = p[m+1].multiply(N.add(BigInteger.valueOf(k))).add(ONE);                    if (p[m].compareTo(TWO.pow(t[m]))==1)                    {                        continue step6; //step 12                    }                    //step13                    if ((TWO.modPow(p[m+1].multiply(N.add(BigInteger.valueOf(k))),p[m]).compareTo(ONE)==0) &&                        (TWO.modPow(N.add(BigInteger.valueOf(k)),p[m]).compareTo(ONE)!=0))                    {                        m -= 1;                        break;                    }                    else                    {                        k += 2;                        continue step11;                    }                }                if (m>=0)                 {                    break; //step 14                }                else                {                    pq[0] = p[0];                    pq[1] = p[1];                    return y[0].intValue(); //return for procedure B step 2                }            }        }        return y[0].intValue();    }    //Procedure A'    private long procedure_Aa(long x0, long c, BigInteger[] pq, int size)    {        //Verify and perform condition: 0<x<2^32; 0<c<2^32; c - odd.        while(x0<0 || x0>4294967296L)        {            x0 = init_random.nextInt()*2;        }        while((c<0 || c>4294967296L) || (c/2==0))        {            c = init_random.nextInt()*2+1;        }        BigInteger C = new BigInteger(Long.toString(c));        BigInteger constA32 = new BigInteger("97781173");        //step1        BigInteger[] y = new BigInteger[1]; // begin length = 1        y[0] = new BigInteger(Long.toString(x0));        //step 2        int[] t = new int[1]; // t - orders; begin length = 1        t[0] = size;        int s = 0;        for (int i=0; t[i]>=33; i++)        {            // extension array t            int tmp_t[] = new int[t.length + 1];             ///////////////            System.arraycopy(t,0,tmp_t,0,t.length);          //  extension            t = new int[tmp_t.length];                       //  array t            System.arraycopy(tmp_t, 0, t, 0, tmp_t.length);  ///////////////            t[i+1] = t[i]/2;            s = i+1;        }        //step3        BigInteger p[] = new BigInteger[s+1];        p[s] = new BigInteger("8000000B",16); //set min prime number length 32 bit        int m = s-1;  //step4        for (int i=0; i<s; i++)        {            int rm = t[m]/32;  //step5     step6: for(;;)            {                //step 6                BigInteger tmp_y[] = new BigInteger[y.length];  ////////////////                System.arraycopy(y,0,tmp_y,0,y.length);         //  extension                y = new BigInteger[rm+1];                       //  array y                System.arraycopy(tmp_y,0,y,0,tmp_y.length);     ////////////////                for (int j=0; j<rm; j++)                {                    y[j+1] = (y[j].multiply(constA32).add(C)).mod(TWO.pow(32));                }                //step 7                BigInteger Ym = new BigInteger("0");                for (int j=0; j<rm; j++)                {                    Ym = Ym.add(y[j].multiply(TWO.pow(32*j)));                }                y[0] = y[rm]; //step 8                //step 9                BigInteger N = TWO.pow(t[m]-1).divide(p[m+1]).                                   add((TWO.pow(t[m]-1).multiply(Ym)).                                       divide(p[m+1].multiply(TWO.pow(32*rm))));                if (N.mod(TWO).compareTo(ONE)==0)                 {                    N = N.add(ONE);                }                int k = 0; //step 10        step11: for(;;)                {                    //step 11                    p[m] = p[m+1].multiply(N.add(BigInteger.valueOf(k))).add(ONE);                    if (p[m].compareTo(TWO.pow(t[m]))==1)                    {                        continue step6; //step 12                    }                    //step13                    if ((TWO.modPow(p[m+1].multiply(N.add(BigInteger.valueOf(k))),p[m]).compareTo(ONE)==0) &&                        (TWO.modPow(N.add(BigInteger.valueOf(k)),p[m]).compareTo(ONE)!=0))                    {                        m -= 1;                        break;                    }                    else                    {                        k += 2;                        continue step11;                    }                }                if (m>=0)                {                    break; //step 14                }                else                {                    pq[0] = p[0];                    pq[1] = p[1];                    return y[0].longValue(); //return for procedure B' step 2

⌨️ 快捷键说明

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