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

📄 gost3410test.java

📁 J2ME加密算法的代码!里面包括常用的算法
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
                String x0 = "0x3DFC46F1";                String c =  "0xD";                if (firstLong)                {                    firstLong = false;                    return NumberParsing.decodeLongFromHex(x0);                }                return NumberParsing.decodeLongFromHex(c);            }            public void nextBytes(byte[] bytes)            {                byte[] d = Hex.decode("02");                System.arraycopy(d, 0, bytes, bytes.length-d.length, d.length);            }        };        SecureRandom    random = new SecureRandom()        {            public void nextBytes(byte[] bytes)            {                byte[] k = Hex.decode("90F3A564439242F5186EBB224C8E223811B7105C64E4F5390807E6362DF4C72A");                int i;                for (i = 0; i < (bytes.length - k.length); i += k.length)                {                    System.arraycopy(k, 0, bytes, i, k.length);                }                if (i > bytes.length)                {                    System.arraycopy(k, 0, bytes, i - k.length, bytes.length - (i - k.length));                }                else                {                    System.arraycopy(k, 0, bytes, i, bytes.length - i);                }            }        };        SecureRandom    keyRandom = new SecureRandom()        {            public void nextBytes(byte[] bytes)            {                byte[] x = Hex.decode("3036314538303830343630454235324435324234314132373832433138443046");                int i;                for (i = 0; i < (bytes.length - x.length); i += x.length)                {                    System.arraycopy(x, 0, bytes, i, x.length);                }                if (i > bytes.length)                {                    System.arraycopy(x, 0, bytes, i - x.length, bytes.length - (i - x.length));                }                else                {                    System.arraycopy(x, 0, bytes, i, bytes.length - i);                }            }        };        BigInteger  pValue = new BigInteger("e2c4191c4b5f222f9ac2732562f6d9b4f18e7fb67a290ea1e03d750f0b9806755fc730d975bf3faa606d05c218b35a6c3706919aab92e0c58b1de4531c8fa8e7af43c2bff016251e21b2870897f6a27ac4450bca235a5b748ad386e4a0e4dfcb09152435abcfe48bd0b126a8122c7382f285a9864615c66decddf6afd355dfb7", 16);        BigInteger  qValue = new BigInteger("931a58fb6f0dcdf2fe7549bc3f19f4724b56898f7f921a076601edb18c93dc75", 16);        public TestResult perform()        {            BigInteger              r = new BigInteger("81d69a192e9c7ac21fc07da41bd07e230ba6a94eb9f3c1fd104c7bd976733ca5",16);            BigInteger              s = new BigInteger("315c879c8414f35feb4deb15e7cc0278c48e6ca1596325d6959338d860b0c47a",16);            GOST3410ParametersGenerator  pGen = new GOST3410ParametersGenerator();            pGen.init(1024, 2, init_random);            GOST3410Parameters           params = pGen.generateParameters();            if (!pValue.equals(params.getP()) || !qValue.equals(params.getQ()))            {                return new SimpleTestResult(false, getName() + ": p or q wrong");            }            GOST3410KeyPairGenerator         GOST3410KeyGen = new GOST3410KeyPairGenerator();            GOST3410KeyGenerationParameters  genParam = new GOST3410KeyGenerationParameters(keyRandom, params);            GOST3410KeyGen.init(genParam);            AsymmetricCipherKeyPair  pair = GOST3410KeyGen.generateKeyPair();            ParametersWithRandom param = new ParametersWithRandom(pair.getPrivate(), random);            GOST3410Signer GOST3410 = new GOST3410Signer();            GOST3410.init(true, param);                       BigInteger[] sig = GOST3410.generateSignature(hashmessage);            if (!r.equals(sig[0]))            {                return new SimpleTestResult(false, getName()                    + ": r component wrong." + System.getProperty("line.separator")                    + " expecting: " + r.toString(16) + System.getProperty("line.separator")                    + " got      : " + sig[0].toString(16));            }            if (!s.equals(sig[1]))            {                return new SimpleTestResult(false, getName()                    + ": s component wrong." + System.getProperty("line.separator")                    + " expecting: " + s.toString(16) + System.getProperty("line.separator")                    + " got      : " + sig[1].toString(16));            }            GOST3410.init(false, pair.getPublic());            if (GOST3410.verifySignature(hashmessage, sig[0], sig[1]))            {                return new SimpleTestResult(true, getName() + ": Okay");            }            else            {                return new SimpleTestResult(false, getName() + ": verification fails");            }        }    }    private class GOST3410_AParam        implements Test    {        public String getName()        {            return "GOST3410-AParam";        }        SecureRandom    init_random = new SecureRandom()        {            boolean firstLong = true;            public long nextLong()            {                String x0 = "0x520874F5";                String c =  "0xEE39ADB3";                if (firstLong)                {                    firstLong = false;                    return NumberParsing.decodeLongFromHex(x0);                }                return NumberParsing.decodeLongFromHex(c);            }            public void nextBytes(byte[] bytes)            {                byte[] d = Hex.decode("02");                System.arraycopy(d, 0, bytes, bytes.length-d.length, d.length);            }        };        SecureRandom    random = new SecureRandom()        {            public void nextBytes(byte[] bytes)            {                byte[] k = Hex.decode("90F3A564439242F5186EBB224C8E223811B7105C64E4F5390807E6362DF4C72A");                int i;                for (i = 0; i < (bytes.length - k.length); i += k.length)                {                    System.arraycopy(k, 0, bytes, i, k.length);                }                if (i > bytes.length)                {                    System.arraycopy(k, 0, bytes, i - k.length, bytes.length - (i - k.length));                }                else                {                    System.arraycopy(k, 0, bytes, i, bytes.length - i);                }            }        };        SecureRandom    keyRandom = new SecureRandom()        {            public void nextBytes(byte[] bytes)            {                byte[] x = Hex.decode("3036314538303830343630454235324435324234314132373832433138443046");                int i;                for (i = 0; i < (bytes.length - x.length); i += x.length)                {                    System.arraycopy(x, 0, bytes, i, x.length);                }                if (i > bytes.length)                {                    System.arraycopy(x, 0, bytes, i - x.length, bytes.length - (i - x.length));                }                else                {                    System.arraycopy(x, 0, bytes, i, bytes.length - i);                }            }        };        BigInteger  pValue = new BigInteger("b4e25efb018e3c8b87505e2a67553c5edc56c2914b7e4f89d23f03f03377e70a2903489dd60e78418d3d851edb5317c4871e40b04228c3b7902963c4b7d85d52b9aa88f2afdbeb28da8869d6df846a1d98924e925561bd69300b9ddd05d247b5922d967cbb02671881c57d10e5ef72d3e6dad4223dc82aa1f7d0294651a480df", 16);        BigInteger  qValue = new BigInteger("972432a437178b30bd96195b773789ab2fff15594b176dd175b63256ee5af2cf", 16);        public TestResult perform()        {            BigInteger              r = new BigInteger("64a8856628e5669d85f62cd763dd4a99bc56d33dc0e1859122855d141e9e4774",16);            BigInteger              s = new BigInteger("319ebac97092b288d469a4b988248794f60c865bc97858d9a3135c6d1a1bf2dd",16);            GOST3410ParametersGenerator  pGen = new GOST3410ParametersGenerator();            pGen.init(1024, 2, init_random);            GOST3410Parameters           params = pGen.generateParameters();            if (!pValue.equals(params.getP()) || !qValue.equals(params.getQ()))            {                return new SimpleTestResult(false, getName() + ": p or q wrong");            }            GOST3410KeyPairGenerator         GOST3410KeyGen = new GOST3410KeyPairGenerator();            GOST3410KeyGenerationParameters  genParam = new GOST3410KeyGenerationParameters(keyRandom, params);            GOST3410KeyGen.init(genParam);            AsymmetricCipherKeyPair  pair = GOST3410KeyGen.generateKeyPair();            ParametersWithRandom param = new ParametersWithRandom(pair.getPrivate(), random);            GOST3410Signer GOST3410 = new GOST3410Signer();            GOST3410.init(true, param);            BigInteger[] sig = GOST3410.generateSignature(hashmessage);            if (!r.equals(sig[0]))            {                return new SimpleTestResult(false, getName()                    + ": r component wrong." + System.getProperty("line.separator")                    + " expecting: " + r.toString(16) + System.getProperty("line.separator")                    + " got      : " + sig[0].toString(16));            }            if (!s.equals(sig[1]))            {                return new SimpleTestResult(false, getName()                    + ": s component wrong." + System.getProperty("line.separator")                    + " expecting: " + s.toString(16) + System.getProperty("line.separator")                    + " got      : " + sig[1].toString(16));            }            GOST3410.init(false, pair.getPublic());            if (GOST3410.verifySignature(hashmessage, sig[0], sig[1]))            {                return new SimpleTestResult(true, getName() + ": Okay");            }            else            {                return new SimpleTestResult(false, getName() + ": verification fails");            }        }    }    private class GOST3410_BParam        implements Test    {        public String getName()        {            return "GOST3410-BParam";        }        SecureRandom    init_random = new SecureRandom()        {            boolean firstLong = true;            public long nextLong()            {                String x0 = "0x5B977CDB";                String c =  "0x6E9692DD";                if (firstLong)                {                    firstLong = false;                    return NumberParsing.decodeLongFromHex(x0);                }                return NumberParsing.decodeLongFromHex(c);            }            public void nextBytes(byte[] bytes)            {                byte[] d = Hex.decode("bc3cbbdb7e6f848286e19ad9a27a8e297e5b71c53dd974cdf60f937356df69cbc97a300ccc71685c553046147f11568c4fddf363d9d886438345a62c3b75963d6546adfabf31b31290d12cae65ecb8309ef66782");                System.arraycopy(d, 0, bytes, bytes.length-d.length, d.length);            }        };        SecureRandom    random = new SecureRandom()        {            public void nextBytes(byte[] bytes)            {                byte[] k = Hex.decode("90F3A564439242F5186EBB224C8E223811B7105C64E4F5390807E6362DF4C72A");                int i;                for (i = 0; i < (bytes.length - k.length); i += k.length)                {                    System.arraycopy(k, 0, bytes, i, k.length);                }                if (i > bytes.length)                {                    System.arraycopy(k, 0, bytes, i - k.length, bytes.length - (i - k.length));                }                else                {                    System.arraycopy(k, 0, bytes, i, bytes.length - i);                }            }        };        SecureRandom    keyRandom = new SecureRandom()        {            public void nextBytes(byte[] bytes)            {                byte[] x = Hex.decode("3036314538303830343630454235324435324234314132373832433138443046");                int i;                for (i = 0; i < (bytes.length - x.length); i += x.length)                {                    System.arraycopy(x, 0, bytes, i, x.length);                }                if (i > bytes.length)                {                    System.arraycopy(x, 0, bytes, i - x.length, bytes.length - (i - x.length));                }                else                {                    System.arraycopy(x, 0, bytes, i, bytes.length - i);                }            }        };        BigInteger  pValue = new BigInteger("c6971fc57524b30c9018c5e621de15499736854f56a6f8aee65a7a404632b3540f09020f67f04dc2e6783b141dceffd21a703035b7d0187c6e12cb4229922bafdb2225b73e6b23a0de36e20047065aea000c1a374283d0ad8dc1981e3995f0bb8c72526041fcb98ae6163e1e71a669d8364e9c4c3188f673c5f8ee6fadb41abf", 16);        BigInteger  qValue = new BigInteger("b09d634c10899cd7d4c3a7657403e05810b07c61a688bab2c37f475e308b0607", 16);        public TestResult perform()        {            BigInteger              r = new BigInteger("860d82c60e9502cd00c0e9e1f6563feafec304801974d745c5e02079946f729e",16);            BigInteger              s = new BigInteger("7ef49264ef022801aaa03033cd97915235fbab4c823ed936b0f360c22114688a",16);            GOST3410ParametersGenerator  pGen = new GOST3410ParametersGenerator();            pGen.init(1024, 2, init_random);            GOST3410Parameters           params = pGen.generateParameters();            if (!pValue.equals(params.getP()) || !qValue.equals(params.getQ()))            {                return new SimpleTestResult(false, getName() + ": p or q wrong");            }            GOST3410KeyPairGenerator         GOST3410KeyGen = new GOST3410KeyPairGenerator();            GOST3410KeyGenerationParameters  genParam = new GOST3410KeyGenerationParameters(keyRandom, params);            GOST3410KeyGen.init(genParam);            AsymmetricCipherKeyPair  pair = GOST3410KeyGen.generateKeyPair();            ParametersWithRandom param = new ParametersWithRandom(pair.getPrivate(), random);            GOST3410Signer GOST3410 = new GOST3410Signer();            GOST3410.init(true, param);            BigInteger[] sig = GOST3410.generateSignature(hashmessage);            if (!r.equals(sig[0]))            {                return new SimpleTestResult(false, getName()                    + ": r component wrong." + System.getProperty("line.separator")                    + " expecting: " + r.toString(16) + System.getProperty("line.separator")

⌨️ 快捷键说明

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