javax.crypto.spec.OAEPParameterSpec Java Examples

The following examples show how to use javax.crypto.spec.OAEPParameterSpec. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RSACipher.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
protected AlgorithmParameters engineGetParameters() {
    if (spec != null && spec instanceof OAEPParameterSpec) {
        try {
            AlgorithmParameters params =
                AlgorithmParameters.getInstance("OAEP",
                    SunJCE.getInstance());
            params.init(spec);
            return params;
        } catch (NoSuchAlgorithmException nsae) {
            // should never happen
            throw new RuntimeException("Cannot find OAEP " +
                " AlgorithmParameters implementation in SunJCE provider");
        } catch (InvalidParameterSpecException ipse) {
            // should never happen
            throw new RuntimeException("OAEPParameterSpec not supported");
        }
    } else {
        return null;
    }
}
 
Example #2
Source File: TestOAEPParameterSpec.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private static boolean compareMGF(OAEPParameterSpec s1,
    OAEPParameterSpec s2) {
    String alg1 = s1.getMGFAlgorithm();
    String alg2 = s2.getMGFAlgorithm();
    if (alg1.equals(alg2)) {
        MGF1ParameterSpec mp1 = (MGF1ParameterSpec)s1.getMGFParameters();
        MGF1ParameterSpec mp2 = (MGF1ParameterSpec)s2.getMGFParameters();
        alg1 = mp1.getDigestAlgorithm();
        alg2 = mp2.getDigestAlgorithm();
        if (alg1.equals(alg2)) {
            return true;
        } else {
            System.out.println("MGF's MD algos: " + alg1 + " vs " + alg2);
            return false;
        }
    } else {
        System.out.println("MGF algos: " + alg1 + " vs " + alg2);
        return false;
    }
}
 
Example #3
Source File: BaseEncryptionManager.java    From samples-android with Apache License 2.0 6 votes vote down vote up
private void initEncodeCipher(String keyAlias, int mode) throws GeneralSecurityException {
    PublicKey key = mKeyStore.getCertificate(keyAlias).getPublicKey();

    // workaround for using public key
    // from https://developer.android.com/reference/android/security/keystore/KeyGenParameterSpec.html#known-issues
    PublicKey unrestricted = KeyFactory.getInstance(key.getAlgorithm())
            .generatePublic(new X509EncodedKeySpec(key.getEncoded()));

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        // from https://code.google.com/p/android/issues/detail?id=197719
        OAEPParameterSpec spec = new OAEPParameterSpec("SHA-256", "MGF1",
                MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT);

        mCipher.init(mode, unrestricted, spec);
    } else {
        mCipher.init(mode, unrestricted);
    }
}
 
Example #4
Source File: TestOAEPParameterSpec.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static boolean compareMGF(OAEPParameterSpec s1,
    OAEPParameterSpec s2) {
    String alg1 = s1.getMGFAlgorithm();
    String alg2 = s2.getMGFAlgorithm();
    if (alg1.equals(alg2)) {
        MGF1ParameterSpec mp1 = (MGF1ParameterSpec)s1.getMGFParameters();
        MGF1ParameterSpec mp2 = (MGF1ParameterSpec)s2.getMGFParameters();
        alg1 = mp1.getDigestAlgorithm();
        alg2 = mp2.getDigestAlgorithm();
        if (alg1.equals(alg2)) {
            return true;
        } else {
            System.out.println("MGF's MD algos: " + alg1 + " vs " + alg2);
            return false;
        }
    } else {
        System.out.println("MGF algos: " + alg1 + " vs " + alg2);
        return false;
    }
}
 
Example #5
Source File: RSACipher.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
protected void engineInit(int opmode, Key key,
        AlgorithmParameters params, SecureRandom random)
        throws InvalidKeyException, InvalidAlgorithmParameterException {
    if (params == null) {
        init(opmode, key, random, null);
    } else {
        try {
            OAEPParameterSpec spec =
                    params.getParameterSpec(OAEPParameterSpec.class);
            init(opmode, key, random, spec);
        } catch (InvalidParameterSpecException ipse) {
            InvalidAlgorithmParameterException iape =
                new InvalidAlgorithmParameterException("Wrong parameter");
            iape.initCause(ipse);
            throw iape;
        }
    }
}
 
Example #6
Source File: RSACipher.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
protected AlgorithmParameters engineGetParameters() {
    if (spec != null && spec instanceof OAEPParameterSpec) {
        try {
            AlgorithmParameters params =
                AlgorithmParameters.getInstance("OAEP",
                    SunJCE.getInstance());
            params.init(spec);
            return params;
        } catch (NoSuchAlgorithmException nsae) {
            // should never happen
            throw new RuntimeException("Cannot find OAEP " +
                " AlgorithmParameters implementation in SunJCE provider");
        } catch (InvalidParameterSpecException ipse) {
            // should never happen
            throw new RuntimeException("OAEPParameterSpec not supported");
        }
    } else {
        return null;
    }
}
 
Example #7
Source File: RSACipher.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
protected void engineInit(int opmode, Key key,
        AlgorithmParameters params, SecureRandom random)
        throws InvalidKeyException, InvalidAlgorithmParameterException {
    if (params == null) {
        init(opmode, key, random, null);
    } else {
        try {
            OAEPParameterSpec spec =
                    params.getParameterSpec(OAEPParameterSpec.class);
            init(opmode, key, random, spec);
        } catch (InvalidParameterSpecException ipse) {
            InvalidAlgorithmParameterException iape =
                new InvalidAlgorithmParameterException("Wrong parameter");
            iape.initCause(ipse);
            throw iape;
        }
    }
}
 
Example #8
Source File: TestOAEPParameterSpec.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static boolean runTest(String mdName, MGF1ParameterSpec mgfSpec,
    byte[] p) throws Exception {
    OAEPParameterSpec spec = new OAEPParameterSpec(mdName, "MGF1",
        mgfSpec, new PSource.PSpecified(p));
    cp = Security.getProvider("SunJCE");
    System.out.println("Testing provider " + cp.getName() + "...");
    AlgorithmParameters ap = AlgorithmParameters.getInstance("OAEP", cp);

    ap.init(spec);
    byte[] encoding = ap.getEncoded();

    AlgorithmParameters ap2 = AlgorithmParameters.getInstance("OAEP", cp);
    ap2.init(encoding);

    OAEPParameterSpec spec2 = (OAEPParameterSpec) ap2.getParameterSpec
            (OAEPParameterSpec.class);
    return compareSpec(spec, spec2);
}
 
Example #9
Source File: RSACipher.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
protected AlgorithmParameters engineGetParameters() {
    if (spec != null && spec instanceof OAEPParameterSpec) {
        try {
            AlgorithmParameters params =
                AlgorithmParameters.getInstance("OAEP",
                    SunJCE.getInstance());
            params.init(spec);
            return params;
        } catch (NoSuchAlgorithmException nsae) {
            // should never happen
            throw new RuntimeException("Cannot find OAEP " +
                " AlgorithmParameters implementation in SunJCE provider");
        } catch (InvalidParameterSpecException ipse) {
            // should never happen
            throw new RuntimeException("OAEPParameterSpec not supported");
        }
    } else {
        return null;
    }
}
 
Example #10
Source File: TestOAEPParameterSpec.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static boolean compareMGF(OAEPParameterSpec s1,
    OAEPParameterSpec s2) {
    String alg1 = s1.getMGFAlgorithm();
    String alg2 = s2.getMGFAlgorithm();
    if (alg1.equals(alg2)) {
        MGF1ParameterSpec mp1 = (MGF1ParameterSpec)s1.getMGFParameters();
        MGF1ParameterSpec mp2 = (MGF1ParameterSpec)s2.getMGFParameters();
        alg1 = mp1.getDigestAlgorithm();
        alg2 = mp2.getDigestAlgorithm();
        if (alg1.equals(alg2)) {
            return true;
        } else {
            System.out.println("MGF's MD algos: " + alg1 + " vs " + alg2);
            return false;
        }
    } else {
        System.out.println("MGF algos: " + alg1 + " vs " + alg2);
        return false;
    }
}
 
Example #11
Source File: PFSecurityUtils.java    From PFLockScreen-Android with Apache License 2.0 6 votes vote down vote up
private void initEncodeCipher(Cipher cipher, String alias, KeyStore keyStore)
        throws PFSecurityException {
    try {
        final PublicKey key = keyStore.getCertificate(alias).getPublicKey();
        final PublicKey unrestricted = KeyFactory.getInstance(key.getAlgorithm()).generatePublic(
                new X509EncodedKeySpec(key.getEncoded()));
        final OAEPParameterSpec spec = new OAEPParameterSpec("SHA-256", "MGF1",
                MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT);
        cipher.init(Cipher.ENCRYPT_MODE, unrestricted, spec);
    } catch (KeyStoreException | InvalidKeySpecException |
            NoSuchAlgorithmException | InvalidKeyException |
            InvalidAlgorithmParameterException e) {
        throw new PFSecurityException(
                "Can not initialize Encode Cipher:" + e.getMessage(),
                PFSecurityUtilsErrorCodes.ERROR_INIT_ENDECODE_CIPHER
        );
    }
}
 
Example #12
Source File: RSACipher.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
protected void engineInit(int opmode, Key key,
        AlgorithmParameters params, SecureRandom random)
        throws InvalidKeyException, InvalidAlgorithmParameterException {
    if (params == null) {
        init(opmode, key, random, null);
    } else {
        try {
            OAEPParameterSpec spec =
                    params.getParameterSpec(OAEPParameterSpec.class);
            init(opmode, key, random, spec);
        } catch (InvalidParameterSpecException ipse) {
            InvalidAlgorithmParameterException iape =
                new InvalidAlgorithmParameterException("Wrong parameter");
            iape.initCause(ipse);
            throw iape;
        }
    }
}
 
Example #13
Source File: TestOAEPParameterSpec.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private static boolean compareMGF(OAEPParameterSpec s1,
    OAEPParameterSpec s2) {
    String alg1 = s1.getMGFAlgorithm();
    String alg2 = s2.getMGFAlgorithm();
    if (alg1.equals(alg2)) {
        MGF1ParameterSpec mp1 = (MGF1ParameterSpec)s1.getMGFParameters();
        MGF1ParameterSpec mp2 = (MGF1ParameterSpec)s2.getMGFParameters();
        alg1 = mp1.getDigestAlgorithm();
        alg2 = mp2.getDigestAlgorithm();
        if (alg1.equals(alg2)) {
            return true;
        } else {
            System.out.println("MGF's MD algos: " + alg1 + " vs " + alg2);
            return false;
        }
    } else {
        System.out.println("MGF algos: " + alg1 + " vs " + alg2);
        return false;
    }
}
 
Example #14
Source File: TestOAEPParameterSpec.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private static boolean runTest(String mdName, MGF1ParameterSpec mgfSpec,
    byte[] p) throws Exception {
    OAEPParameterSpec spec = new OAEPParameterSpec(mdName, "MGF1",
        mgfSpec, new PSource.PSpecified(p));
    cp = Security.getProvider("SunJCE");
    System.out.println("Testing provider " + cp.getName() + "...");
    AlgorithmParameters ap = AlgorithmParameters.getInstance("OAEP", cp);

    ap.init(spec);
    byte[] encoding = ap.getEncoded();

    AlgorithmParameters ap2 = AlgorithmParameters.getInstance("OAEP", cp);
    ap2.init(encoding);

    OAEPParameterSpec spec2 = (OAEPParameterSpec) ap2.getParameterSpec
            (OAEPParameterSpec.class);
    return compareSpec(spec, spec2);
}
 
Example #15
Source File: RSACipher.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
protected AlgorithmParameters engineGetParameters() {
    if (spec != null && spec instanceof OAEPParameterSpec) {
        try {
            AlgorithmParameters params =
                AlgorithmParameters.getInstance("OAEP",
                    SunJCE.getInstance());
            params.init(spec);
            return params;
        } catch (NoSuchAlgorithmException nsae) {
            // should never happen
            throw new RuntimeException("Cannot find OAEP " +
                " AlgorithmParameters implementation in SunJCE provider");
        } catch (InvalidParameterSpecException ipse) {
            // should never happen
            throw new RuntimeException("OAEPParameterSpec not supported");
        }
    } else {
        return null;
    }
}
 
Example #16
Source File: TestOAEPParameterSpec.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private static boolean runTest(String mdName, MGF1ParameterSpec mgfSpec,
    byte[] p) throws Exception {
    OAEPParameterSpec spec = new OAEPParameterSpec(mdName, "MGF1",
        mgfSpec, new PSource.PSpecified(p));
    cp = Security.getProvider("SunJCE");
    System.out.println("Testing provider " + cp.getName() + "...");
    AlgorithmParameters ap = AlgorithmParameters.getInstance("OAEP", cp);

    ap.init(spec);
    byte[] encoding = ap.getEncoded();

    AlgorithmParameters ap2 = AlgorithmParameters.getInstance("OAEP", cp);
    ap2.init(encoding);

    OAEPParameterSpec spec2 = (OAEPParameterSpec) ap2.getParameterSpec
            (OAEPParameterSpec.class);
    return compareSpec(spec, spec2);
}
 
Example #17
Source File: TestOAEPParameterSpec.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static boolean runTest(String mdName, MGF1ParameterSpec mgfSpec,
    byte[] p) throws Exception {
    OAEPParameterSpec spec = new OAEPParameterSpec(mdName, "MGF1",
        mgfSpec, new PSource.PSpecified(p));
    cp = Security.getProvider("SunJCE");
    System.out.println("Testing provider " + cp.getName() + "...");
    AlgorithmParameters ap = AlgorithmParameters.getInstance("OAEP", cp);

    ap.init(spec);
    byte[] encoding = ap.getEncoded();

    AlgorithmParameters ap2 = AlgorithmParameters.getInstance("OAEP", cp);
    ap2.init(encoding);

    OAEPParameterSpec spec2 = (OAEPParameterSpec) ap2.getParameterSpec
            (OAEPParameterSpec.class);
    return compareSpec(spec, spec2);
}
 
Example #18
Source File: TestOAEPParameterSpec.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static boolean compareMGF(OAEPParameterSpec s1,
    OAEPParameterSpec s2) {
    String alg1 = s1.getMGFAlgorithm();
    String alg2 = s2.getMGFAlgorithm();
    if (alg1.equals(alg2)) {
        MGF1ParameterSpec mp1 = (MGF1ParameterSpec)s1.getMGFParameters();
        MGF1ParameterSpec mp2 = (MGF1ParameterSpec)s2.getMGFParameters();
        alg1 = mp1.getDigestAlgorithm();
        alg2 = mp2.getDigestAlgorithm();
        if (alg1.equals(alg2)) {
            return true;
        } else {
            System.out.println("MGF's MD algos: " + alg1 + " vs " + alg2);
            return false;
        }
    } else {
        System.out.println("MGF algos: " + alg1 + " vs " + alg2);
        return false;
    }
}
 
Example #19
Source File: RSACipher.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
protected void engineInit(int opmode, Key key,
        AlgorithmParameters params, SecureRandom random)
        throws InvalidKeyException, InvalidAlgorithmParameterException {
    if (params == null) {
        init(opmode, key, random, null);
    } else {
        try {
            OAEPParameterSpec spec =
                    params.getParameterSpec(OAEPParameterSpec.class);
            init(opmode, key, random, spec);
        } catch (InvalidParameterSpecException ipse) {
            InvalidAlgorithmParameterException iape =
                new InvalidAlgorithmParameterException("Wrong parameter");
            iape.initCause(ipse);
            throw iape;
        }
    }
}
 
Example #20
Source File: TestOAEPParameterSpec.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private static boolean compareMGF(OAEPParameterSpec s1,
    OAEPParameterSpec s2) {
    String alg1 = s1.getMGFAlgorithm();
    String alg2 = s2.getMGFAlgorithm();
    if (alg1.equals(alg2)) {
        MGF1ParameterSpec mp1 = (MGF1ParameterSpec)s1.getMGFParameters();
        MGF1ParameterSpec mp2 = (MGF1ParameterSpec)s2.getMGFParameters();
        alg1 = mp1.getDigestAlgorithm();
        alg2 = mp2.getDigestAlgorithm();
        if (alg1.equals(alg2)) {
            return true;
        } else {
            System.out.println("MGF's MD algos: " + alg1 + " vs " + alg2);
            return false;
        }
    } else {
        System.out.println("MGF algos: " + alg1 + " vs " + alg2);
        return false;
    }
}
 
Example #21
Source File: RSACipher.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
protected void engineInit(int opmode, Key key,
        AlgorithmParameters params, SecureRandom random)
        throws InvalidKeyException, InvalidAlgorithmParameterException {
    if (params == null) {
        init(opmode, key, random, null);
    } else {
        try {
            OAEPParameterSpec spec =
                    params.getParameterSpec(OAEPParameterSpec.class);
            init(opmode, key, random, spec);
        } catch (InvalidParameterSpecException ipse) {
            InvalidAlgorithmParameterException iape =
                new InvalidAlgorithmParameterException("Wrong parameter");
            iape.initCause(ipse);
            throw iape;
        }
    }
}
 
Example #22
Source File: RSACipher.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
protected AlgorithmParameters engineGetParameters() {
    if (spec != null && spec instanceof OAEPParameterSpec) {
        try {
            AlgorithmParameters params =
                AlgorithmParameters.getInstance("OAEP",
                    SunJCE.getInstance());
            params.init(spec);
            return params;
        } catch (NoSuchAlgorithmException nsae) {
            // should never happen
            throw new RuntimeException("Cannot find OAEP " +
                " AlgorithmParameters implementation in SunJCE provider");
        } catch (InvalidParameterSpecException ipse) {
            // should never happen
            throw new RuntimeException("OAEPParameterSpec not supported");
        }
    } else {
        return null;
    }
}
 
Example #23
Source File: TestOAEPParameterSpec.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static boolean runTest(String mdName, MGF1ParameterSpec mgfSpec,
    byte[] p) throws Exception {
    OAEPParameterSpec spec = new OAEPParameterSpec(mdName, "MGF1",
        mgfSpec, new PSource.PSpecified(p));
    cp = Security.getProvider("SunJCE");
    System.out.println("Testing provider " + cp.getName() + "...");
    AlgorithmParameters ap = AlgorithmParameters.getInstance("OAEP", cp);

    ap.init(spec);
    byte[] encoding = ap.getEncoded();

    AlgorithmParameters ap2 = AlgorithmParameters.getInstance("OAEP", cp);
    ap2.init(encoding);

    OAEPParameterSpec spec2 = (OAEPParameterSpec) ap2.getParameterSpec
            (OAEPParameterSpec.class);
    return compareSpec(spec, spec2);
}
 
Example #24
Source File: TestOAEPParameterSpec.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static boolean comparePSource(OAEPParameterSpec s1,
    OAEPParameterSpec s2) {
    PSource src1 = s1.getPSource();
    PSource src2 = s2.getPSource();
    String alg1 = src1.getAlgorithm();
    String alg2 = src2.getAlgorithm();
    if (alg1.equals(alg2)) {
        // assumes they are PSource.PSpecified
        return Arrays.equals(((PSource.PSpecified) src1).getValue(),
            ((PSource.PSpecified) src2).getValue());
    } else {
        System.out.println("PSource algos: " + alg1 + " vs " + alg2);
        return false;
    }
}
 
Example #25
Source File: RsaOaepTest.java    From wycheproof with Apache License 2.0 5 votes vote down vote up
protected static OAEPParameterSpec getOaepParameters(JsonObject group,
                                                     JsonObject test) throws Exception {
  String sha = getString(group, "sha");
  String mgf = getString(group, "mgf");
  String mgfSha = getString(group, "mgfSha");
  PSource p = PSource.PSpecified.DEFAULT;
  if (test.has("label")) {
    p = new PSource.PSpecified(getBytes(test, "label"));
  }
  return new OAEPParameterSpec(sha, mgf, new MGF1ParameterSpec(mgfSha), p); 
}
 
Example #26
Source File: TestOAEPParameterSpec.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private static boolean compareMD(OAEPParameterSpec s1,
    OAEPParameterSpec s2) {
    boolean result = false;
    String alg1 = s1.getDigestAlgorithm().toUpperCase().trim();
    String alg2 = s2.getDigestAlgorithm().toUpperCase().trim();
    alg1 = alg1.replaceAll("\\-", "");
    alg2 = alg2.replaceAll("\\-", "");
    if (alg1.equals("SHA") || alg1.equals("SHA1")) {
        result = (alg2.equals("SHA") || alg2.equals("SHA1"));
    } else {
        result = (alg1.equals(alg2));
    }
    return result;
}
 
Example #27
Source File: OAEPParameters.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
protected <T extends AlgorithmParameterSpec>
    T engineGetParameterSpec(Class<T> paramSpec)
    throws InvalidParameterSpecException {
    if (OAEPParameterSpec.class.isAssignableFrom(paramSpec)) {
        return paramSpec.cast(
            new OAEPParameterSpec(mdName, "MGF1", mgfSpec,
                                  new PSource.PSpecified(p)));
    } else {
        throw new InvalidParameterSpecException
            ("Inappropriate parameter specification");
    }
}
 
Example #28
Source File: OAEPParameters.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
protected void engineInit(AlgorithmParameterSpec paramSpec)
    throws InvalidParameterSpecException {
    if (!(paramSpec instanceof OAEPParameterSpec)) {
        throw new InvalidParameterSpecException
            ("Inappropriate parameter specification");
    }
    OAEPParameterSpec spec = (OAEPParameterSpec) paramSpec;
    mdName = spec.getDigestAlgorithm();
    String mgfName = spec.getMGFAlgorithm();
    if (!mgfName.equalsIgnoreCase("MGF1")) {
        throw new InvalidParameterSpecException("Unsupported mgf " +
            mgfName + "; MGF1 only");
    }
    AlgorithmParameterSpec mgfSpec = spec.getMGFParameters();
    if (!(mgfSpec instanceof MGF1ParameterSpec)) {
        throw new InvalidParameterSpecException("Inappropriate mgf " +
            "parameters; non-null MGF1ParameterSpec only");
    }
    this.mgfSpec = (MGF1ParameterSpec) mgfSpec;
    PSource pSrc = spec.getPSource();
    if (pSrc.getAlgorithm().equals("PSpecified")) {
        p = ((PSource.PSpecified) pSrc).getValue();
    } else {
        throw new InvalidParameterSpecException("Unsupported pSource " +
            pSrc.getAlgorithm() + "; PSpecified only");
    }
}
 
Example #29
Source File: TestOAEPPadding.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static void testEncryptDecrypt(OAEPParameterSpec spec,
        int dataLength) throws Exception {

    System.out.print("Testing OAEP with hash ");
    if (spec != null) {
        System.out.print(spec.getDigestAlgorithm() + " and MGF " +
            ((MGF1ParameterSpec)spec.getMGFParameters()).
                getDigestAlgorithm());
    } else {
        System.out.print("Default");
    }
    System.out.println(", " + dataLength + " bytes");

    Cipher c = Cipher.getInstance("RSA/ECB/OAEPPadding", cp);
    if (spec != null) {
        c.init(Cipher.ENCRYPT_MODE, publicKey, spec);
    } else {
        c.init(Cipher.ENCRYPT_MODE, publicKey);
    }

    byte[] data = new byte[dataLength];
    byte[] enc = c.doFinal(data);
    if (spec != null) {
        c.init(Cipher.DECRYPT_MODE, privateKey, spec);
    } else {
        c.init(Cipher.DECRYPT_MODE, privateKey);
    }
    byte[] dec = c.doFinal(enc);
    if (Arrays.equals(data, dec) == false) {
        throw new Exception("Data does not match");
    }
}
 
Example #30
Source File: OAEPParameters.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
protected void engineInit(AlgorithmParameterSpec paramSpec)
    throws InvalidParameterSpecException {
    if (!(paramSpec instanceof OAEPParameterSpec)) {
        throw new InvalidParameterSpecException
            ("Inappropriate parameter specification");
    }
    OAEPParameterSpec spec = (OAEPParameterSpec) paramSpec;
    mdName = spec.getDigestAlgorithm();
    String mgfName = spec.getMGFAlgorithm();
    if (!mgfName.equalsIgnoreCase("MGF1")) {
        throw new InvalidParameterSpecException("Unsupported mgf " +
            mgfName + "; MGF1 only");
    }
    AlgorithmParameterSpec mgfSpec = spec.getMGFParameters();
    if (!(mgfSpec instanceof MGF1ParameterSpec)) {
        throw new InvalidParameterSpecException("Inappropriate mgf " +
            "parameters; non-null MGF1ParameterSpec only");
    }
    this.mgfSpec = (MGF1ParameterSpec) mgfSpec;
    PSource pSrc = spec.getPSource();
    if (pSrc.getAlgorithm().equals("PSpecified")) {
        p = ((PSource.PSpecified) pSrc).getValue();
    } else {
        throw new InvalidParameterSpecException("Unsupported pSource " +
            pSrc.getAlgorithm() + "; PSpecified only");
    }
}