/** * Encrypt data of the string using UTF-8 encoding * * @param data to encrypt * @return encrypted data * @throws EncryptionError */ public byte[] encrypt(String data) throws EncryptionError { return encrypt(data.getBytes()); } }
/** * Test {@link RSAOAEPPublicKey#encrypt}. */ @Test public void encrypt() throws Exception { // Test on RSA vectors. AbstractPublicKey rsaPublicKey = oaepSpec.getPublicKey(); ((RSAOAEPPublicKey) rsaPublicKey).resetEncryptor(); assertArrayEquals(rsaPublicKey.encrypt(oaepSpec.M), oaepSpec.C); }
/** * Test {@link RSAOAEPPrivateKey#getPublicKey}. */ @Test public void getPublicKey() throws Exception { AbstractPublicKey randomPublicKey4096 = randomPrivateKey4096.getPublicKey(); assertTrue(randomPublicKey4096 instanceof RSAOAEPPublicKey); AbstractPrivateKey rsaPrivateKey = oaepSpec.getPrivateKey(); AbstractPublicKey rsaPublicKey = rsaPrivateKey.getPublicKey(); ((RSAOAEPPublicKey) rsaPublicKey).resetEncryptor(); assertArrayEquals(rsaPublicKey.encrypt(oaepSpec.M), oaepSpec.C); }
assertArrayEquals(rsaPrivateKey.decrypt(oaepSpec.C), oaepSpec.M); ((RSAOAEPPublicKey) rsaPublicKey).resetEncryptor(); assertArrayEquals(rsaPublicKey.encrypt(oaepSpec.M), oaepSpec.C); rng.nextBytes(message); byte[] encrypted = randomPublicKey.encrypt(message), decrypted = randomPrivateKey.decrypt(encrypted); assertArrayEquals(decrypted, message);
/** * Test {@link RSAOAEPPublicKey#updateFromHash} on success scenarios, * using RSA spec vectors. */ @Test public void updateFromHashGoodRSASpec() throws Exception { Map mapRSA = new HashMap<String, Object>() {{ put("n", oaepSpec.n); put("e", oaepSpec.e); put("hash", "SHA-1"); put("mgf1Hash", "SHA-1"); }}; AbstractPublicKey rsaPublicKey = new RSAOAEPPublicKey(); rsaPublicKey.updateFromHash(mapRSA); // We've read the public key from hash; // but, to test it over the RSA spec vectors, // we need to hack into the state.rng (and substitute it with our custom one), // even though it is declared as final. RSAOAEPPublicKey.State state = ((RSAOAEPPublicKey) rsaPublicKey).state; Field rngField = RSAOAEPPublicKey.State.class.getDeclaredField("rng"); rngField.setAccessible(true); rngField.set(state, oaepSpec.getRandSeed()); // Now we can even test the encryption on the RSA spec vectors. assertTrue(rsaPublicKey.isInitialized()); assertEquals(rsaPublicKey.getBitStrength(), 1024); ((RSAOAEPPublicKey) rsaPublicKey).resetEncryptor(); assertArrayEquals(rsaPublicKey.encrypt(oaepSpec.M), oaepSpec.C); }
assertEquals(rsaPublicKey.getBitStrength(), 1024); ((RSAOAEPPublicKey) rsaPublicKey).resetEncryptor(); assertArrayEquals(rsaPublicKey.encrypt(oaepSpec.M), oaepSpec.C);