oaeptest.java

来自「J2ME加密算法的代码!里面包括常用的算法」· Java 代码 · 共 466 行 · 第 1/2 页

JAVA
466
字号
        (byte)0x28, (byte)0xfa, (byte)0xe7, (byte)0xaa, (byte)0x04, (byte)0x0a, (byte)0x2d, (byte)0x5b,        (byte)0x25, (byte)0x21, (byte)0x76, (byte)0x45, (byte)0x9d, (byte)0x1f, (byte)0x39, (byte)0x75,        (byte)0x41, (byte)0xba, (byte)0x2a, (byte)0x58, (byte)0xfb, (byte)0x65, (byte)0x99, (byte)0x02,        (byte)0x41, (byte)0x00, (byte)0xc9, (byte)0x7f, (byte)0xb1, (byte)0xf0, (byte)0x27, (byte)0xf4,        (byte)0x53, (byte)0xf6, (byte)0x34, (byte)0x12, (byte)0x33, (byte)0xea, (byte)0xaa, (byte)0xd1,        (byte)0xd9, (byte)0x35, (byte)0x3f, (byte)0x6c, (byte)0x42, (byte)0xd0, (byte)0x88, (byte)0x66,        (byte)0xb1, (byte)0xd0, (byte)0x5a, (byte)0x0f, (byte)0x20, (byte)0x35, (byte)0x02, (byte)0x8b,        (byte)0x9d, (byte)0x86, (byte)0x98, (byte)0x40, (byte)0xb4, (byte)0x16, (byte)0x66, (byte)0xb4,        (byte)0x2e, (byte)0x92, (byte)0xea, (byte)0x0d, (byte)0xa3, (byte)0xb4, (byte)0x32, (byte)0x04,        (byte)0xb5, (byte)0xcf, (byte)0xce, (byte)0x33, (byte)0x52, (byte)0x52, (byte)0x4d, (byte)0x04,        (byte)0x16, (byte)0xa5, (byte)0xa4, (byte)0x41, (byte)0xe7, (byte)0x00, (byte)0xaf, (byte)0x46,        (byte)0x15, (byte)0x03, (byte)0x02, (byte)0x40, (byte)0x54, (byte)0x49, (byte)0x4c, (byte)0xa6,        (byte)0x3e, (byte)0xba, (byte)0x03, (byte)0x37, (byte)0xe4, (byte)0xe2, (byte)0x40, (byte)0x23,        (byte)0xfc, (byte)0xd6, (byte)0x9a, (byte)0x5a, (byte)0xeb, (byte)0x07, (byte)0xdd, (byte)0xdc,        (byte)0x01, (byte)0x83, (byte)0xa4, (byte)0xd0, (byte)0xac, (byte)0x9b, (byte)0x54, (byte)0xb0,        (byte)0x51, (byte)0xf2, (byte)0xb1, (byte)0x3e, (byte)0xd9, (byte)0x49, (byte)0x09, (byte)0x75,        (byte)0xea, (byte)0xb7, (byte)0x74, (byte)0x14, (byte)0xff, (byte)0x59, (byte)0xc1, (byte)0xf7,        (byte)0x69, (byte)0x2e, (byte)0x9a, (byte)0x2e, (byte)0x20, (byte)0x2b, (byte)0x38, (byte)0xfc,        (byte)0x91, (byte)0x0a, (byte)0x47, (byte)0x41, (byte)0x74, (byte)0xad, (byte)0xc9, (byte)0x3c,        (byte)0x1f, (byte)0x67, (byte)0xc9, (byte)0x81, (byte)0x02, (byte)0x40, (byte)0x47, (byte)0x1e,        (byte)0x02, (byte)0x90, (byte)0xff, (byte)0x0a, (byte)0xf0, (byte)0x75, (byte)0x03, (byte)0x51,        (byte)0xb7, (byte)0xf8, (byte)0x78, (byte)0x86, (byte)0x4c, (byte)0xa9, (byte)0x61, (byte)0xad,        (byte)0xbd, (byte)0x3a, (byte)0x8a, (byte)0x7e, (byte)0x99, (byte)0x1c, (byte)0x5c, (byte)0x05,        (byte)0x56, (byte)0xa9, (byte)0x4c, (byte)0x31, (byte)0x46, (byte)0xa7, (byte)0xf9, (byte)0x80,        (byte)0x3f, (byte)0x8f, (byte)0x6f, (byte)0x8a, (byte)0xe3, (byte)0x42, (byte)0xe9, (byte)0x31,        (byte)0xfd, (byte)0x8a, (byte)0xe4, (byte)0x7a, (byte)0x22, (byte)0x0d, (byte)0x1b, (byte)0x99,        (byte)0xa4, (byte)0x95, (byte)0x84, (byte)0x98, (byte)0x07, (byte)0xfe, (byte)0x39, (byte)0xf9,        (byte)0x24, (byte)0x5a, (byte)0x98, (byte)0x36, (byte)0xda, (byte)0x3d, (byte)0x02, (byte)0x41,        (byte)0x00, (byte)0xb0, (byte)0x6c, (byte)0x4f, (byte)0xda, (byte)0xbb, (byte)0x63, (byte)0x01,        (byte)0x19, (byte)0x8d, (byte)0x26, (byte)0x5b, (byte)0xdb, (byte)0xae, (byte)0x94, (byte)0x23,        (byte)0xb3, (byte)0x80, (byte)0xf2, (byte)0x71, (byte)0xf7, (byte)0x34, (byte)0x53, (byte)0x88,        (byte)0x50, (byte)0x93, (byte)0x07, (byte)0x7f, (byte)0xcd, (byte)0x39, (byte)0xe2, (byte)0x11,        (byte)0x9f, (byte)0xc9, (byte)0x86, (byte)0x32, (byte)0x15, (byte)0x4f, (byte)0x58, (byte)0x83,        (byte)0xb1, (byte)0x67, (byte)0xa9, (byte)0x67, (byte)0xbf, (byte)0x40, (byte)0x2b, (byte)0x4e,        (byte)0x9e, (byte)0x2e, (byte)0x0f, (byte)0x96, (byte)0x56, (byte)0xe6, (byte)0x98, (byte)0xea,        (byte)0x36, (byte)0x66, (byte)0xed, (byte)0xfb, (byte)0x25, (byte)0x79, (byte)0x80, (byte)0x39,        (byte)0xf7    };    static byte[] output3 = Hex.decode(        "b8246b56a6ed5881aeb585d9a25b2ad790c417e080681bf1ac2bc3deb69d8bce"      + "f0c4366fec400af052a72e9b0effb5b3f2f192dbeaca03c12740057113bf1f06"      + "69ac22e9f3a7852e3c15d913cab0b8863a95c99294ce8674214954610346f4d4"      + "74b26f7c48b42ee68e1f572a1fc4026ac456b4f59f7b621ea1b9d88f64202fb1");    private class Rand extends SecureRandom        {            byte[]  seed = {                (byte)0xaa, (byte)0xfd, (byte)0x12, (byte)0xf6, (byte)0x59,                (byte)0xca, (byte)0xe6, (byte)0x34, (byte)0x89, (byte)0xb4,                (byte)0x79, (byte)0xe5, (byte)0x07, (byte)0x6d, (byte)0xde,                (byte)0xc2, (byte)0xf0, (byte)0x6c, (byte)0xb5, (byte)0x8f        };        public void nextBytes(            byte[]  bytes)        {            System.arraycopy(seed, 0, bytes, 0, bytes.length);        }    }    private class OAEPTester        implements Test    {        private int     id;        private byte[]  pubKeyEnc;        private byte[]  privKeyEnc;        private byte[]  output;            OAEPTester(            int     id,            byte[]  pubKeyEnc,            byte[]  privKeyEnc,            byte[]  output)        {            this.id = id;            this.pubKeyEnc = pubKeyEnc;            this.privKeyEnc = privKeyEnc;            this.output = output;        }            public String getName()        {            return "OAEPTest " + id;        }            public TestResult perform()        {            ByteArrayInputStream    bIn = new ByteArrayInputStream(pubKeyEnc);            ASN1InputStream         dIn = new ASN1InputStream(bIn);                //            // extract the public key info.            //            RSAPublicKeyStructure   pubStruct;                try            {                pubStruct = new RSAPublicKeyStructure((ASN1Sequence)new SubjectPublicKeyInfo((ASN1Sequence)dIn.readObject()).getPublicKey());            }            catch (Exception e)            {                return new SimpleTestResult(false, getName() + ": exception - " + e.toString());            }                bIn = new ByteArrayInputStream(privKeyEnc);            dIn = new ASN1InputStream(bIn);                //            // extract the private key info.            //            RSAPrivateKeyStructure privStruct;                try            {                privStruct = new RSAPrivateKeyStructure((ASN1Sequence)(new PrivateKeyInfo((ASN1Sequence)dIn.readObject()).getPrivateKey()));            }            catch (Exception e)            {                return new SimpleTestResult(false, getName() + ": exception - " + e.toString());            }                RSAKeyParameters    pubParameters = new RSAKeyParameters(                                                        false,                                                        pubStruct.getModulus(),                                                        pubStruct.getPublicExponent());                RSAKeyParameters    privParameters = new RSAPrivateCrtKeyParameters(                                                        privStruct.getModulus(),                                                        privStruct.getPublicExponent(),                                                        privStruct.getPrivateExponent(),                                                        privStruct.getPrime1(),                                                        privStruct.getPrime2(),                                                        privStruct.getExponent1(),                                                        privStruct.getExponent2(),                                                        privStruct.getCoefficient());                AsymmetricBlockCipher   cipher = new OAEPEncoding(new RSAEngine());                cipher.init(true, new ParametersWithRandom(pubParameters, new Rand()));                byte[]  out;            byte[]  input = new byte[]                { (byte)0x54, (byte)0x85, (byte)0x9b, (byte)0x34, (byte)0x2c, (byte)0x49, (byte)0xea, (byte)0x2a };                try            {                out = cipher.processBlock(input, 0, input.length);            }            catch (Exception e)            {                return new SimpleTestResult(false, getName() + ": exception - " + e.toString());            }                    for (int i = 0; i != output.length; i++)            {                if (out[i] != output[i])                {                    return new SimpleTestResult(false, getName() + ": failed encryption");                }            }                cipher.init(false, privParameters);            try            {                out = cipher.processBlock(output, 0, output.length);            }            catch (Exception e)            {                return new SimpleTestResult(false, getName() + ": exception - " + e.toString());            }                for (int i = 0; i != input.length; i++)            {                if (out[i] != input[i])                {                    return new SimpleTestResult(false, getName() + ": failed decoding");                }            }                return new SimpleTestResult(true, getName() + ": Okay");        }    }    public OAEPTest()    {    }    public String getName()    {        return "OAEP";    }        public TestResult perform()    {        Test        test = new OAEPTester(1, pubKeyEnc1, privKeyEnc1, output1);        TestResult  result = test.perform();            if (!result.isSuccessful())        {            return result;        }            test = new OAEPTester(2, pubKeyEnc2, privKeyEnc2, output2);        result = test.perform();            if (!result.isSuccessful())        {            return result;        }            test = new OAEPTester(3, pubKeyEnc3, privKeyEnc3, output3);        result = test.perform();        if (!result.isSuccessful())        {            return result;        }            return new SimpleTestResult(true, "OAEP: Okay");    }    public static void main(        String[]    args)    {        OAEPTest        test = new OAEPTest();        TestResult      result = test.perform();        System.out.println(result);    }}

⌨️ 快捷键说明

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