By default, OAEP uses SHA-1 for MGF1. Please note that the selected hash does not have a big impact on OAEP security, so in most cases it will be left by default.
We can easily verify this by testing it with "OAEPPadding" and OAEPParameterSpec :
// --- we need a key pair to test encryption/decryption KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); kpg.initialize(1024); // speedy generation, but not secure anymore KeyPair kp = kpg.generateKeyPair(); RSAPublicKey pubkey = (RSAPublicKey) kp.getPublic(); RSAPrivateKey privkey = (RSAPrivateKey) kp.getPrivate(); // --- encrypt given algorithm string Cipher oaepFromAlgo = Cipher.getInstance("RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING"); oaepFromAlgo.init(Cipher.ENCRYPT_MODE, pubkey); byte[] ct = oaepFromAlgo.doFinal("owlstead".getBytes(StandardCharsets.UTF_8)); // --- decrypt given OAEPParameterSpec Cipher oaepFromInit = Cipher.getInstance("RSA/ECB/OAEPPadding"); OAEPParameterSpec oaepParams = new OAEPParameterSpec("SHA-256", "MGF1", new MGF1ParameterSpec("SHA-1"), PSpecified.DEFAULT); oaepFromInit.init(Cipher.DECRYPT_MODE, privkey, oaepParams); byte[] pt = oaepFromInit.doFinal(ct); System.out.println(new String(pt, StandardCharsets.UTF_8));
The code will not be executed with a filling exception if you replace MGF1 as the "SHA-256" parameter.
The reason why an advanced algorithm is needed at all is because it is compatible with other Cipher algorithms. The code written, for example, for "RSA/ECB/PKCS1Padding" , does not use any parameters, not to mention the OAEP parameters. Thus, without a longer string, OAEP cannot function as a replacement.
The "ECB" mode of operation does not mean anything in this context; it should have been "None" or it should have been completely excluded. You can encrypt only one block using the RSRS implementation of the SunRSA provider.
If you want to encrypt more data, create a random (AES) symmetric key and encrypt it using OAEP. Then use the AES key to encrypt your specific data. This is called a hybrid cryptosystem, since both asymmetric and symmetric primitives are used to encrypt data.
Please note that OAEP is not supported in JDK 7 (1.7) or earlier. OAEP is included in the implementation requirements for Java runtimes starting with Java 8:
RSA/ECB/OAEPWithSHA-1AndMGF1Padding (1024, 2048)RSA/ECB/OAEPWithSHA-256AndMGF1Padding (1024, 2048)