Search in sources :

Example 76 with BadPaddingException

use of javax.crypto.BadPaddingException in project OpenAM by OpenRock.

the class ReplayPasswd method onLoginSuccess.

/**
     * Post processing on successful authentication.
     * @param requestParamsMap contains HttpServletRequest parameters
     * @param request HttpServlet  request
     * @param response HttpServlet response
     * @param ssoToken user's session
     * @throws AuthenticationException if there is an error while setting
     * the session password property
     */
public void onLoginSuccess(Map requestParamsMap, HttpServletRequest request, HttpServletResponse response, SSOToken ssoToken) throws AuthenticationException {
    if (request == null) {
        debug.message("ReplayPasswd.onLoginSuccess: request is not available, password is not saved.");
        return;
    }
    if (debug.messageEnabled()) {
        debug.message("ReplayPasswd.onLoginSuccess called: Req:" + request.getRequestURL());
    }
    try {
        if (requestParamsMap == null) {
            debug.warning("ReplayPasswd: unable to get user password as requestParamsMap is null");
        } else {
            String userpasswd = (String) requestParamsMap.get(ISAuthConstants.SHARED_STATE_PASSWORD);
            if (StringUtils.isNotEmpty(userpasswd)) {
                String encryptedPassword = encryptPassword(userpasswd);
                if (StringUtils.isNotBlank(encryptedPassword)) {
                    ssoToken.setProperty(SUN_IDENTITY_USER_PASSWORD, encryptedPassword);
                } else if (debug.warningEnabled()) {
                    debug.warning("ReplayPasswd: unable to set encrypted Password as encrypted value is empty");
                }
            } else if (debug.warningEnabled()) {
                debug.warning("ReplayPasswd: unable to get user password to encrypt");
            }
        }
        String iisOwaEnabled = SystemProperties.get(IIS_OWA_ENABLED);
        String strAttributeName = SystemProperties.get(SHAREPOINT_LOGIN_ATTR_NAME);
        if (Boolean.parseBoolean(iisOwaEnabled)) {
            // Set OWA Auth Cookie
            Cookie owaAuthCookie;
            for (String domain : AuthUtils.getCookieDomainsForRequest(request)) {
                owaAuthCookie = CookieUtils.newCookie(OWA_AUTH_COOKIE, OWA_AUTH_COOKIE_VALUE, "/", domain);
                CookieUtils.addCookieToResponse(response, owaAuthCookie);
            }
        }
        if (strAttributeName != null && !strAttributeName.trim().equals("")) {
            AMIdentity amIdentityUser = IdUtils.getIdentity(ssoToken);
            Map attrMap = amIdentityUser.getAttributes();
            String strAttributeValue = Misc.getMapAttr(attrMap, strAttributeName, null);
            if (strAttributeValue != null) {
                ssoToken.setProperty(SHAREPOINT_LOGIN_ATTR_VALUE, strAttributeValue);
            }
            if (debug.messageEnabled()) {
                debug.message("ReplayPasswd.onLoginSuccess: " + strAttributeName + "=" + strAttributeValue);
            }
        }
        if (debug.messageEnabled()) {
            debug.message("ReplayPasswd.onLoginSuccess: Replay password concluded successfully");
        }
    } catch (IdRepoException ire) {
        debug.error("ReplayPasswd.onLoginSuccess: IOException while fetching user attributes: " + ire);
    } catch (NoSuchAlgorithmException noe) {
        debug.error("ReplayPasswd.onLoginSuccess: NoSuchAlgorithmException" + " while setting session password property: " + noe);
    } catch (InvalidKeyException ike) {
        debug.error("ReplayPasswd.onLoginSuccess: InvalidKeyException " + "while setting session password property: " + ike);
    } catch (IllegalBlockSizeException ibe) {
        debug.error("ReplayPasswd.onLoginSuccess:IllegalBlockSizeException" + " while setting session password property: " + ibe);
    } catch (NoSuchPaddingException npe) {
        debug.error("ReplayPasswd.onLoginSuccess: NoSuchPaddingException " + "while setting session password property: " + npe);
    } catch (BadPaddingException bpe) {
        debug.error("ReplayPasswd.onLoginSuccess: BadPaddingException " + "while setting session password property: " + bpe);
    } catch (SSOException sse) {
        debug.error("ReplayPasswd.onLoginSuccess: SSOException while setting session password property: " + sse);
    }
}
Also used : Cookie(javax.servlet.http.Cookie) AMIdentity(com.sun.identity.idm.AMIdentity) IdRepoException(com.sun.identity.idm.IdRepoException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) SSOException(com.iplanet.sso.SSOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) Map(java.util.Map)

Example 77 with BadPaddingException

use of javax.crypto.BadPaddingException in project Gradle-demo by Arisono.

the class RSAUtils method RSAEncode.

/**
	 * 加密,三步走。
	 * 
	 * @param key
	 * @param plainText
	 * @return
	 */
public static byte[] RSAEncode(byte[] key, byte[] plainText) {
    try {
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, restorePublicKey(key));
        return cipher.doFinal(plainText);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) Cipher(javax.crypto.Cipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException)

Example 78 with BadPaddingException

use of javax.crypto.BadPaddingException in project Gradle-demo by Arisono.

the class RSAUtils method RSADecode.

/**
	 * 解密,三步走。
	 * 
	 * @param key
	 * @param encodedText
	 * @return
	 */
public static String RSADecode(byte[] key, byte[] encodedText) {
    try {
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, restorePrivateKey(key));
        return new String(cipher.doFinal(encodedText));
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) Cipher(javax.crypto.Cipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException)

Example 79 with BadPaddingException

use of javax.crypto.BadPaddingException in project nhin-d by DirectProject.

the class SplitDirectRecipientInformation method getContentStream.

/**
	 * {@inheritDoc}
	 */
@Override
public CMSTypedStream getContentStream(Key key, /*private key*/
String prov) throws /*ignored, use class variables instead*/
CMSException, NoSuchProviderException {
    // this is the symmetric key
    final byte[] encryptedKey = info.getEncryptedKey().getOctets();
    // this is the algorithm that protects the symmetric key
    final String keyExchangeAlgorithm = getExchangeEncryptionAlgorithmName(_keyEncAlg.getObjectId());
    // this is the algorithm of the symmetric key to actually decrypt the content
    final String alg = EncryptionAlgorithm.fromOID(_encAlg.getObjectId().getId(), EncryptionAlgorithm.AES128_CBC).getAlgName();
    try {
        Cipher keyCipher = Cipher.getInstance(keyExchangeAlgorithm, keyEncProvider);
        Key sKey;
        try {
            // the original BC libraries attempted to do an UNWRAP assuming that the 
            // same provider was used for secret key decryption and message decryption
            // when these two operations are split into separate providers, using an unwrap method
            // may result in a secret key handle that may not be usable by the another provider
            // for that reason, this class will do a straight up decrypt of the message's internal
            // secret key and hand that key off to the "encProvider" provider
            keyCipher.init(Cipher.DECRYPT_MODE, key);
            sKey = new SecretKeySpec(keyCipher.doFinal(encryptedKey), alg);
        } catch (GeneralSecurityException e) {
            keyCipher.init(Cipher.DECRYPT_MODE, key);
            sKey = new SecretKeySpec(keyCipher.doFinal(encryptedKey), alg);
        } catch (IllegalStateException e) {
            keyCipher.init(Cipher.DECRYPT_MODE, key);
            sKey = new SecretKeySpec(keyCipher.doFinal(encryptedKey), alg);
        } catch (UnsupportedOperationException e) {
            keyCipher.init(Cipher.DECRYPT_MODE, key);
            sKey = new SecretKeySpec(keyCipher.doFinal(encryptedKey), alg);
        } catch (ProviderException e) {
            keyCipher.init(Cipher.DECRYPT_MODE, key);
            sKey = new SecretKeySpec(keyCipher.doFinal(encryptedKey), alg);
        }
        return getContentFromSessionKey(sKey, encProvider);
    } catch (NoSuchAlgorithmException e) {
        throw new CMSException("can't find algorithm.", e);
    } catch (InvalidKeyException e) {
        throw new CMSException("key invalid in message.", e);
    } catch (NoSuchPaddingException e) {
        throw new CMSException("required padding not supported.", e);
    } catch (IllegalBlockSizeException e) {
        throw new CMSException("illegal blocksize in message.", e);
    } catch (BadPaddingException e) {
        throw new CMSException("bad padding in message.", e);
    }
}
Also used : ProviderException(java.security.ProviderException) NoSuchProviderException(java.security.NoSuchProviderException) GeneralSecurityException(java.security.GeneralSecurityException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher) Key(java.security.Key) CMSException(org.bouncycastle.cms.CMSException)

Example 80 with BadPaddingException

use of javax.crypto.BadPaddingException in project android_frameworks_base by DirtyUnicorns.

the class AndroidKeyStoreCipherSpiBase method engineDoFinal.

@Override
protected final byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen) throws IllegalBlockSizeException, BadPaddingException {
    if (mCachedException != null) {
        throw (IllegalBlockSizeException) new IllegalBlockSizeException().initCause(mCachedException);
    }
    try {
        ensureKeystoreOperationInitialized();
    } catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
        throw (IllegalBlockSizeException) new IllegalBlockSizeException().initCause(e);
    }
    byte[] output;
    try {
        flushAAD();
        byte[] additionalEntropy = KeyStoreCryptoOperationUtils.getRandomBytesToMixIntoKeystoreRng(mRng, getAdditionalEntropyAmountForFinish());
        output = mMainDataStreamer.doFinal(input, inputOffset, inputLen, // no signature involved
        null, additionalEntropy);
    } catch (KeyStoreException e) {
        switch(e.getErrorCode()) {
            case KeymasterDefs.KM_ERROR_INVALID_INPUT_LENGTH:
                throw (IllegalBlockSizeException) new IllegalBlockSizeException().initCause(e);
            case KeymasterDefs.KM_ERROR_INVALID_ARGUMENT:
                throw (BadPaddingException) new BadPaddingException().initCause(e);
            case KeymasterDefs.KM_ERROR_VERIFICATION_FAILED:
                throw (AEADBadTagException) new AEADBadTagException().initCause(e);
            default:
                throw (IllegalBlockSizeException) new IllegalBlockSizeException().initCause(e);
        }
    }
    resetWhilePreservingInitState();
    return output;
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) KeyStoreException(android.security.KeyStoreException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) AEADBadTagException(javax.crypto.AEADBadTagException)

Aggregations

BadPaddingException (javax.crypto.BadPaddingException)120 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)103 InvalidKeyException (java.security.InvalidKeyException)80 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)70 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)69 Cipher (javax.crypto.Cipher)53 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)45 IOException (java.io.IOException)39 KeyStoreException (java.security.KeyStoreException)25 UnrecoverableKeyException (java.security.UnrecoverableKeyException)25 CertificateException (java.security.cert.CertificateException)25 SecretKey (javax.crypto.SecretKey)25 IvParameterSpec (javax.crypto.spec.IvParameterSpec)25 SecretKeySpec (javax.crypto.spec.SecretKeySpec)23 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)17 RemoteException (android.os.RemoteException)15 ShortBufferException (javax.crypto.ShortBufferException)14 KeyGenerator (javax.crypto.KeyGenerator)13 UnsupportedEncodingException (java.io.UnsupportedEncodingException)12 FileNotFoundException (java.io.FileNotFoundException)11