< prev index next >

src/java.base/share/classes/javax/crypto/Cipher.java

Print this page




 476      * Provider that supports the specified algorithm is returned.
 477      *
 478      * <p> Note that the list of registered providers may be retrieved via
 479      * the {@link Security#getProviders() Security.getProviders()} method.
 480      *
 481      * @implNote
 482      * The JDK Reference Implementation additionally uses the
 483      * {@code jdk.security.provider.preferred}
 484      * {@link Security#getProperty(String) Security} property to determine
 485      * the preferred provider order for the specified algorithm. This
 486      * may be different than the order of providers returned by
 487      * {@link Security#getProviders() Security.getProviders()}.
 488      *
 489      * @param transformation the name of the transformation, e.g.,
 490      * <i>DES/CBC/PKCS5Padding</i>.
 491      * See the Cipher section in the <a href=
 492      *   "{@docRoot}/../technotes/guides/security/StandardNames.html#Cipher">
 493      * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
 494      * for information about standard transformation names.
 495      *
 496      * @return a cipher that implements the requested transformation.
 497      *
 498      * @exception NoSuchAlgorithmException if {@code transformation}
 499      *          is null, empty, in an invalid format,
 500      *          or if no Provider supports a CipherSpi implementation for the
 501      *          specified algorithm.
 502      *
 503      * @exception NoSuchPaddingException if {@code transformation}
 504      *          contains a padding scheme that is not available.
 505      *
 506      * @see java.security.Provider
 507      */
 508     public static final Cipher getInstance(String transformation)
 509             throws NoSuchAlgorithmException, NoSuchPaddingException
 510     {



 511         List<Transform> transforms = getTransforms(transformation);
 512         List<ServiceId> cipherServices = new ArrayList<>(transforms.size());
 513         for (Transform transform : transforms) {
 514             cipherServices.add(new ServiceId("Cipher", transform.transform));
 515         }
 516         List<Service> services = GetInstance.getServices(cipherServices);
 517         // make sure there is at least one service from a signed provider
 518         // and that it can use the specified mode and padding
 519         Iterator<Service> t = services.iterator();
 520         Exception failure = null;
 521         while (t.hasNext()) {
 522             Service s = t.next();
 523             if (JceSecurity.canUseProvider(s.getProvider()) == false) {
 524                 continue;
 525             }
 526             Transform tr = getTransform(s, transforms);
 527             if (tr == null) {
 528                 // should never happen
 529                 continue;
 530             }


 553      * Returns a {@code Cipher} object that implements the specified
 554      * transformation.
 555      *
 556      * <p> A new Cipher object encapsulating the
 557      * CipherSpi implementation from the specified provider
 558      * is returned.  The specified provider must be registered
 559      * in the security provider list.
 560      *
 561      * <p> Note that the list of registered providers may be retrieved via
 562      * the {@link Security#getProviders() Security.getProviders()} method.
 563      *
 564      * @param transformation the name of the transformation,
 565      * e.g., <i>DES/CBC/PKCS5Padding</i>.
 566      * See the Cipher section in the <a href=
 567      *   "{@docRoot}/../technotes/guides/security/StandardNames.html#Cipher">
 568      * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
 569      * for information about standard transformation names.
 570      *
 571      * @param provider the name of the provider.
 572      *
 573      * @return a cipher that implements the requested transformation.
 574      *
 575      * @exception NoSuchAlgorithmException if {@code transformation}
 576      *          is null, empty, in an invalid format,
 577      *          or if a CipherSpi implementation for the specified algorithm
 578      *          is not available from the specified provider.
 579      *
 580      * @exception NoSuchProviderException if the specified provider is not
 581      *          registered in the security provider list.



 582      *
 583      * @exception NoSuchPaddingException if {@code transformation}
 584      *          contains a padding scheme that is not available.
 585      *
 586      * @exception IllegalArgumentException if the {@code provider}
 587      *          is null or empty.
 588      *
 589      * @see java.security.Provider
 590      */
 591     public static final Cipher getInstance(String transformation,
 592                                            String provider)
 593             throws NoSuchAlgorithmException, NoSuchProviderException,
 594             NoSuchPaddingException
 595     {



 596         if ((provider == null) || (provider.length() == 0)) {
 597             throw new IllegalArgumentException("Missing provider");
 598         }
 599         Provider p = Security.getProvider(provider);
 600         if (p == null) {
 601             throw new NoSuchProviderException("No such provider: " +
 602                                               provider);
 603         }
 604         return getInstance(transformation, p);
 605     }
 606 
 607     /**
 608      * Returns a {@code Cipher} object that implements the specified
 609      * transformation.
 610      *
 611      * <p> A new Cipher object encapsulating the
 612      * CipherSpi implementation from the specified Provider
 613      * object is returned.  Note that the specified Provider object
 614      * does not have to be registered in the provider list.
 615      *
 616      * @param transformation the name of the transformation,
 617      * e.g., <i>DES/CBC/PKCS5Padding</i>.
 618      * See the Cipher section in the <a href=
 619      *   "{@docRoot}/../technotes/guides/security/StandardNames.html#Cipher">
 620      * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
 621      * for information about standard transformation names.
 622      *
 623      * @param provider the provider.
 624      *
 625      * @return a cipher that implements the requested transformation.
 626      *
 627      * @exception NoSuchAlgorithmException if {@code transformation}
 628      *          is null, empty, in an invalid format,
 629      *          or if a CipherSpi implementation for the specified algorithm
 630      *          is not available from the specified Provider object.
 631      *
 632      * @exception NoSuchPaddingException if {@code transformation}
 633      *          contains a padding scheme that is not available.



 634      *
 635      * @exception IllegalArgumentException if the {@code provider}
 636      *          is null.
 637      *
 638      * @see java.security.Provider
 639      */
 640     public static final Cipher getInstance(String transformation,
 641                                            Provider provider)
 642             throws NoSuchAlgorithmException, NoSuchPaddingException
 643     {



 644         if (provider == null) {
 645             throw new IllegalArgumentException("Missing provider");
 646         }
 647         Exception failure = null;
 648         List<Transform> transforms = getTransforms(transformation);
 649         boolean providerChecked = false;
 650         String paddingError = null;
 651         for (Transform tr : transforms) {
 652             Service s = provider.getService("Cipher", tr.transform);
 653             if (s == null) {
 654                 continue;
 655             }
 656             if (providerChecked == false) {
 657                 // for compatibility, first do the lookup and then verify
 658                 // the provider. this makes the difference between a NSAE
 659                 // and a SecurityException if the
 660                 // provider does not support the algorithm.
 661                 Exception ve = JceSecurity.getVerificationResult(provider);
 662                 if (ve != null) {
 663                     String msg = "JCE cannot authenticate the provider "




 476      * Provider that supports the specified algorithm is returned.
 477      *
 478      * <p> Note that the list of registered providers may be retrieved via
 479      * the {@link Security#getProviders() Security.getProviders()} method.
 480      *
 481      * @implNote
 482      * The JDK Reference Implementation additionally uses the
 483      * {@code jdk.security.provider.preferred}
 484      * {@link Security#getProperty(String) Security} property to determine
 485      * the preferred provider order for the specified algorithm. This
 486      * may be different than the order of providers returned by
 487      * {@link Security#getProviders() Security.getProviders()}.
 488      *
 489      * @param transformation the name of the transformation, e.g.,
 490      * <i>DES/CBC/PKCS5Padding</i>.
 491      * See the Cipher section in the <a href=
 492      *   "{@docRoot}/../technotes/guides/security/StandardNames.html#Cipher">
 493      * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
 494      * for information about standard transformation names.
 495      *
 496      * @return a cipher that implements the requested transformation
 497      *
 498      * @throws NoSuchAlgorithmException if {@code transformation}
 499      *         is {@code null}, empty, in an invalid format,
 500      *         or if no {@code Provider} supports a {@code CipherSpi}
 501      *         implementation for the specified algorithm
 502      *
 503      * @throws NoSuchPaddingException if {@code transformation}
 504      *         contains a padding scheme that is not available
 505      *
 506      * @see java.security.Provider
 507      */
 508     public static final Cipher getInstance(String transformation)
 509             throws NoSuchAlgorithmException, NoSuchPaddingException
 510     {
 511         if ((transformation == null) || transformation.equals("")) {
 512             throw new NoSuchAlgorithmException("Null or empty transformation");
 513         }
 514         List<Transform> transforms = getTransforms(transformation);
 515         List<ServiceId> cipherServices = new ArrayList<>(transforms.size());
 516         for (Transform transform : transforms) {
 517             cipherServices.add(new ServiceId("Cipher", transform.transform));
 518         }
 519         List<Service> services = GetInstance.getServices(cipherServices);
 520         // make sure there is at least one service from a signed provider
 521         // and that it can use the specified mode and padding
 522         Iterator<Service> t = services.iterator();
 523         Exception failure = null;
 524         while (t.hasNext()) {
 525             Service s = t.next();
 526             if (JceSecurity.canUseProvider(s.getProvider()) == false) {
 527                 continue;
 528             }
 529             Transform tr = getTransform(s, transforms);
 530             if (tr == null) {
 531                 // should never happen
 532                 continue;
 533             }


 556      * Returns a {@code Cipher} object that implements the specified
 557      * transformation.
 558      *
 559      * <p> A new Cipher object encapsulating the
 560      * CipherSpi implementation from the specified provider
 561      * is returned.  The specified provider must be registered
 562      * in the security provider list.
 563      *
 564      * <p> Note that the list of registered providers may be retrieved via
 565      * the {@link Security#getProviders() Security.getProviders()} method.
 566      *
 567      * @param transformation the name of the transformation,
 568      * e.g., <i>DES/CBC/PKCS5Padding</i>.
 569      * See the Cipher section in the <a href=
 570      *   "{@docRoot}/../technotes/guides/security/StandardNames.html#Cipher">
 571      * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
 572      * for information about standard transformation names.
 573      *
 574      * @param provider the name of the provider.
 575      *
 576      * @return a cipher that implements the requested transformation
 577      *
 578      * @throws IllegalArgumentException if the {@code provider}
 579      *         is {@code null} or empty


 580      *
 581      * @throws NoSuchAlgorithmException if {@code transformation}
 582      *         is {@code null}, empty, in an invalid format,
 583      *         or if a {@code CipherSpi} implementation for the
 584      *         specified algorithm is not available from the specified
 585      *         provider
 586      *
 587      * @throws NoSuchPaddingException if {@code transformation}
 588      *         contains a padding scheme that is not available
 589      *
 590      * @throws NoSuchProviderException if the specified provider is not
 591      *         registered in the security provider list
 592      *
 593      * @see java.security.Provider
 594      */
 595     public static final Cipher getInstance(String transformation,
 596                                            String provider)
 597             throws NoSuchAlgorithmException, NoSuchProviderException,
 598             NoSuchPaddingException
 599     {
 600         if ((transformation == null) || transformation.equals("")) {
 601             throw new NoSuchAlgorithmException("Null or empty transformation");
 602         }
 603         if ((provider == null) || (provider.length() == 0)) {
 604             throw new IllegalArgumentException("Missing provider");
 605         }
 606         Provider p = Security.getProvider(provider);
 607         if (p == null) {
 608             throw new NoSuchProviderException("No such provider: " +
 609                                               provider);
 610         }
 611         return getInstance(transformation, p);
 612     }
 613 
 614     /**
 615      * Returns a {@code Cipher} object that implements the specified
 616      * transformation.
 617      *
 618      * <p> A new Cipher object encapsulating the
 619      * CipherSpi implementation from the specified Provider
 620      * object is returned.  Note that the specified Provider object
 621      * does not have to be registered in the provider list.
 622      *
 623      * @param transformation the name of the transformation,
 624      * e.g., <i>DES/CBC/PKCS5Padding</i>.
 625      * See the Cipher section in the <a href=
 626      *   "{@docRoot}/../technotes/guides/security/StandardNames.html#Cipher">
 627      * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
 628      * for information about standard transformation names.
 629      *
 630      * @param provider the provider.
 631      *
 632      * @return a cipher that implements the requested transformation
 633      *
 634      * @throws IllegalArgumentException if the {@code provider}
 635      *         is {@code null}


 636      *
 637      * @throws NoSuchAlgorithmException if {@code transformation}
 638      *         is {@code null}, empty, in an invalid format,
 639      *         or if a {@code CipherSpi} implementation for the
 640      *         specified algorithm is not available from the specified
 641      *         {@code Provider} object
 642      *
 643      * @throws NoSuchPaddingException if {@code transformation}
 644      *         contains a padding scheme that is not available
 645      *
 646      * @see java.security.Provider
 647      */
 648     public static final Cipher getInstance(String transformation,
 649                                            Provider provider)
 650             throws NoSuchAlgorithmException, NoSuchPaddingException
 651     {
 652         if ((transformation == null) || transformation.equals("")) {
 653             throw new NoSuchAlgorithmException("Null or empty transformation");
 654         }
 655         if (provider == null) {
 656             throw new IllegalArgumentException("Missing provider");
 657         }
 658         Exception failure = null;
 659         List<Transform> transforms = getTransforms(transformation);
 660         boolean providerChecked = false;
 661         String paddingError = null;
 662         for (Transform tr : transforms) {
 663             Service s = provider.getService("Cipher", tr.transform);
 664             if (s == null) {
 665                 continue;
 666             }
 667             if (providerChecked == false) {
 668                 // for compatibility, first do the lookup and then verify
 669                 // the provider. this makes the difference between a NSAE
 670                 // and a SecurityException if the
 671                 // provider does not support the algorithm.
 672                 Exception ve = JceSecurity.getVerificationResult(provider);
 673                 if (ve != null) {
 674                     String msg = "JCE cannot authenticate the provider "


< prev index next >