📄 rsatest.java
字号:
c.init(Cipher.DECRYPT_MODE, privKey); out = c.doFinal(out); if (!areEqual(out, input)) { fail("PKCS1 test failed on decrypt expected " + new String(Hex.encode(input)) + " got " + new String(Hex.encode(out))); } // // OAEP - SHA1 // c = Cipher.getInstance("RSA/NONE/OAEPPadding", "BC"); c.init(Cipher.ENCRYPT_MODE, pubKey, 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))); } // // OAEP - SHA224 // c = Cipher.getInstance("RSA/NONE/OAEPWithSHA224AndMGF1Padding", "BC"); c.init(Cipher.ENCRYPT_MODE, pub2048Key, rand); out = c.doFinal(input); if (!areEqual(out, output[3])) { fail("OAEP SHA-224 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-224 test failed on decrypt expected " + new String(Hex.encode(input)) + " got " + new String(Hex.encode(out))); } // // OAEP - SHA 256 // c = Cipher.getInstance("RSA/NONE/OAEPWithSHA256AndMGF1Padding", "BC"); c.init(Cipher.ENCRYPT_MODE, pub2048Key, rand); out = c.doFinal(input); if (!areEqual(out, output[4])) { fail("OAEP SHA-256 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-256 test failed on decrypt expected " + new String(Hex.encode(input)) + " got " + new String(Hex.encode(out))); } // // 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))); } // // 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))); } // // 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[7])) { 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))); } } 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 + -