public final void init ( int opmode , Certificate certificate , SecureRandom random ) throws InvalidKeyException { initialized = false ; checkOpmode ( opmode ) ; // Check key usage if the certificate is of // type X.509. if ( certificate instanceof java . security . cert . X509Certificate ) { // Check whether the cert has a key usage extension // marked as a critical extension. X509Certificate cert = ( X509Certificate ) certificate ; Set critSet = cert . getCriticalExtensionOIDs ( ) ; if ( critSet != null && ! critSet . isEmpty ( ) && critSet . contains ( KEY_USAGE_EXTENSION_OID ) ) { boolean [ ] keyUsageInfo = cert . getKeyUsage ( ) ; // keyUsageInfo[2] is for keyEncipherment; // keyUsageInfo[3] is for dataEncipherment. if ( ( keyUsageInfo != null ) && ( ( ( opmode == Cipher . ENCRYPT_MODE ) && ( keyUsageInfo . length > 3 ) && ( keyUsageInfo [ 3 ] == false ) ) || ( ( opmode == Cipher . WRAP_MODE ) && ( keyUsageInfo . length > 2 ) && ( keyUsageInfo [ 2 ] == false ) ) ) ) { throw new InvalidKeyException ( "Wrong key usage" ) ; } } } PublicKey publicKey = ( certificate == null ? null : certificate . getPublicKey ( ) ) ; try { chooseProvider ( InitType . KEY , opmode , ( Key ) publicKey , null , null , random ) ; } catch ( InvalidAlgorithmParameterException e ) { // should never occur throw new InvalidKeyException ( e ) ; } initialized = true ; this . opmode = opmode ; }
Initializes this cipher with the public key from the given certificate and a source of randomness .