📄 rsatest.java
字号:
out = c.doFinal(out); if (!areEqual(out, input)) { fail("OAEP SHA-256 test failed on decrypt expected " + new String(Hex.encode(input)) + " got " + new String(Hex.encode(out))); } oaepP = c.getParameters(); if (!areEqual(oaepP.getEncoded(), new RSAESOAEPparams( new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha256, new DERNull()), new AlgorithmIdentifier(PKCSObjectIdentifiers.id_mgf1, new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha256, new DERNull())), new AlgorithmIdentifier(PKCSObjectIdentifiers.id_pSpecified, new DEROctetString(new byte[0]))).getEncoded())) { fail("OAEP test failed default sha-256 parameters"); } // // OAEP - SHA 384 // c = Cipher.getInstance("RSA/NONE/OAEPWithSHA384AndMGF1Padding", "BC"); c.init(Cipher.ENCRYPT_MODE, pub2048Key, rand); out = c.doFinal(input); if (!areEqual(out, output[5])) { fail("OAEP SHA-384 test failed on encrypt expected " + new String(Hex.encode(output[2])) + " got " + new String(Hex.encode(out))); } c.init(Cipher.DECRYPT_MODE, priv2048Key); out = c.doFinal(out); if (!areEqual(out, input)) { fail("OAEP SHA-384 test failed on decrypt expected " + new String(Hex.encode(input)) + " got " + new String(Hex.encode(out))); } oaepP = c.getParameters(); if (!areEqual(oaepP.getEncoded(), new RSAESOAEPparams( new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha384, new DERNull()), new AlgorithmIdentifier(PKCSObjectIdentifiers.id_mgf1, new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha384, new DERNull())), new AlgorithmIdentifier(PKCSObjectIdentifiers.id_pSpecified, new DEROctetString(new byte[0]))).getEncoded())) { fail("OAEP test failed default sha-384 parameters"); } // // OAEP - MD5 // c = Cipher.getInstance("RSA/NONE/OAEPWithMD5AndMGF1Padding", "BC"); c.init(Cipher.ENCRYPT_MODE, pubKey, rand); out = c.doFinal(input); if (!areEqual(out, output[6])) { fail("OAEP MD5 test failed on encrypt expected " + new String(Hex.encode(output[2])) + " got " + new String(Hex.encode(out))); } c.init(Cipher.DECRYPT_MODE, privKey); out = c.doFinal(out); if (!areEqual(out, input)) { fail("OAEP MD5 test failed on decrypt expected " + new String(Hex.encode(input)) + " got " + new String(Hex.encode(out))); } oaepP = c.getParameters(); if (!areEqual(oaepP.getEncoded(), new RSAESOAEPparams( new AlgorithmIdentifier(PKCSObjectIdentifiers.md5, new DERNull()), new AlgorithmIdentifier(PKCSObjectIdentifiers.id_mgf1, new AlgorithmIdentifier(PKCSObjectIdentifiers.md5, new DERNull())), new AlgorithmIdentifier(PKCSObjectIdentifiers.id_pSpecified, new DEROctetString(new byte[0]))).getEncoded())) { fail("OAEP test failed default md5 parameters"); } // // OAEP - SHA1 with default parameters // c = Cipher.getInstance("RSA/NONE/OAEPPadding", "BC"); c.init(Cipher.ENCRYPT_MODE, pubKey, OAEPParameterSpec.DEFAULT, rand); out = c.doFinal(input); if (!areEqual(out, output[2])) { fail("OAEP test failed on encrypt expected " + new String(Hex.encode(output[2])) + " got " + new String(Hex.encode(out))); } c = Cipher.getInstance("RSA/NONE/OAEPWithSHA1AndMGF1Padding", "BC"); c.init(Cipher.DECRYPT_MODE, privKey); out = c.doFinal(out); if (!areEqual(out, input)) { fail("OAEP test failed on decrypt expected " + new String(Hex.encode(input)) + " got " + new String(Hex.encode(out))); } oaepP = c.getParameters(); if (!areEqual(oaepP.getEncoded(), new byte[] { 0x30, 0x00 })) { fail("OAEP test failed default parameters"); } // // OAEP - SHA1 with specified string // c = Cipher.getInstance("RSA/NONE/OAEPPadding", "BC"); c.init(Cipher.ENCRYPT_MODE, pubKey, new OAEPParameterSpec("SHA1", "MGF1", new MGF1ParameterSpec("SHA1"), new PSource.PSpecified(new byte[] { 1, 2, 3, 4, 5 })), rand); out = c.doFinal(input); oaepP = c.getParameters(); if (!areEqual(oaepP.getEncoded(), new RSAESOAEPparams( new AlgorithmIdentifier(OIWObjectIdentifiers.idSHA1, new DERNull()), new AlgorithmIdentifier(PKCSObjectIdentifiers.id_mgf1, new AlgorithmIdentifier(OIWObjectIdentifiers.idSHA1, new DERNull())), new AlgorithmIdentifier(PKCSObjectIdentifiers.id_pSpecified, new DEROctetString(new byte[] { 1, 2, 3, 4, 5 }))).getEncoded())) { fail("OAEP test failed changed sha-1 parameters"); } if (!areEqual(out, output[7])) { fail("OAEP test failed on encrypt expected " + new String(Hex.encode(output[2])) + " got " + new String(Hex.encode(out))); } c = Cipher.getInstance("RSA/NONE/OAEPWithSHA1AndMGF1Padding", "BC"); c.init(Cipher.DECRYPT_MODE, privKey, oaepP); out = c.doFinal(out); if (!areEqual(out, input)) { fail("OAEP test failed on decrypt expected " + new String(Hex.encode(input)) + " got " + new String(Hex.encode(out))); } // // ISO9796-1 // byte[] isoInput = Hex.decode("fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210"); PrivateKey isoPrivKey = fact.generatePrivate(isoPrivKeySpec); PublicKey isoPubKey = fact.generatePublic(isoPubKeySpec); c = Cipher.getInstance("RSA/NONE/ISO9796-1Padding", "BC"); c.init(Cipher.ENCRYPT_MODE, isoPrivKey); out = c.doFinal(isoInput); if (!areEqual(out, output[8])) { fail("ISO9796-1 test failed on encrypt expected " + new String(Hex.encode(output[3])) + " got " + new String(Hex.encode(out))); } c.init(Cipher.DECRYPT_MODE, isoPubKey); out = c.doFinal(out); if (!areEqual(out, isoInput)) { fail("ISO9796-1 test failed on decrypt expected " + new String(Hex.encode(input)) + " got " + new String(Hex.encode(out))); } // // // generation with parameters test. // KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA", "BC"); // // 768 bit RSA with e = 2^16-1 // keyPairGen.initialize( new RSAKeyGenParameterSpec(768, BigInteger.valueOf(65537)), new SecureRandom()); KeyPair kp = keyPairGen.generateKeyPair(); pubKey = kp.getPublic(); privKey = kp.getPrivate(); c.init(Cipher.ENCRYPT_MODE, pubKey, rand); out = c.doFinal(input); c.init(Cipher.DECRYPT_MODE, privKey); out = c.doFinal(out); if (!areEqual(out, input)) { fail("key generation test failed on decrypt expected " + new String(Hex.encode(input)) + " got " + new String(Hex.encode(out))); } // // comparison check // KeyFactory keyFact = KeyFactory.getInstance("RSA", "BC"); RSAPrivateCrtKey crtKey = (RSAPrivateCrtKey)keyFact.translateKey(privKey); if (!privKey.equals(crtKey)) { fail("private key equality check failed"); } RSAPublicKey copyKey = (RSAPublicKey)keyFact.translateKey(pubKey); if (!pubKey.equals(copyKey)) { fail("public key equality check failed"); } oaepCompatibilityTest("SHA-1", priv2048Key, pub2048Key); oaepCompatibilityTest("SHA-224", priv2048Key, pub2048Key); oaepCompatibilityTest("SHA-256", priv2048Key, pub2048Key); oaepCompatibilityTest("SHA-384", priv2048Key, pub2048Key); oaepCompatibilityTest("SHA-512", priv2048Key, pub2048Key); } private void oaepCompatibilityTest(String digest, PrivateKey privKey, PublicKey pubKey) throws Exception { if (Security.getProvider("SunJCE") == null || Security.getProvider("SunRsaSign") == null) { System.out.println("return"); return; } KeyFactory fact = KeyFactory.getInstance("RSA", "SunRsaSign"); PrivateKey priv2048Key = fact.generatePrivate(priv2048KeySpec); PublicKey pub2048Key = fact.generatePublic(pub2048KeySpec); byte[] data = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; Cipher sCipher; try { sCipher = Cipher.getInstance("RSA/ECB/OAEPWith" + digest + "AndMGF1Padding", "SunJCE"); } catch (NoSuchAlgorithmException e) { return; } catch (NoSuchPaddingException e) { return; } sCipher.init(Cipher.ENCRYPT_MODE, pub2048Key); byte[] enctext = sCipher.doFinal(data); Cipher bcCipher = Cipher.getInstance("RSA/ECB/OAEPWith" + digest + "AndMGF1Padding", "BC"); bcCipher.init(Cipher.DECRYPT_MODE, privKey, new OAEPParameterSpec(digest, "MGF1", MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT)); byte[] plaintext = bcCipher.doFinal(enctext); if (!Arrays.areEqual(plaintext, data)) { fail("data did not decrypt first time"); } bcCipher.init(Cipher.ENCRYPT_MODE, pubKey, new OAEPParameterSpec(digest, "MGF1", MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT)); enctext = bcCipher.doFinal(data); sCipher.init(Cipher.DECRYPT_MODE, priv2048Key); plaintext = sCipher.doFinal(enctext); if (!Arrays.areEqual(plaintext, data)) { fail("data did not decrypt second time"); } } public String getName() { return "RSATest"; } public static void main( String[] args) { Security.addProvider(new BouncyCastleProvider()); runTest(new RSATest()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -