Cipher cipher = Cipher.getInstance(algName); if (algParameters == null) { cipher.init(Cipher.DECRYPT_MODE, decryptKey); } else { cipher.init(Cipher.DECRYPT_MODE, decryptKey, algParameters); byte[] decryptedData = cipher.doFinal(encryptedData); throw new NoSuchAlgorithmException(e.getMessage()); } catch (InvalidAlgorithmParameterException e) { throw new NoSuchAlgorithmException(e.getMessage()); } catch (IllegalStateException e) { throw new InvalidKeyException(e.getMessage()); } catch (IllegalBlockSizeException e) { throw new InvalidKeyException(e.getMessage()); } catch (BadPaddingException e) { throw new InvalidKeyException(e.getMessage());
private static byte[] decrypt(byte[] content, String password) { try { byte[] keyStr = getKey(password); SecretKeySpec key = new SecretKeySpec(keyStr, "AES"); Cipher cipher = Cipher.getInstance(algorithmStr);//algorithmStr cipher.init(Cipher.DECRYPT_MODE, key);// ʼ byte[] result = cipher.doFinal(content); return result; // } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } return null; }
NoSuchAlgorithmException, InvalidKeyException { if (key == null) { throw new InvalidKeyException("key == null"); Cipher cipher = Cipher.getInstance(sealAlg); if ((paramsAlg != null) && (paramsAlg.length() != 0)) { AlgorithmParameters params = AlgorithmParameters.getInstance(paramsAlg); params.init(encodedParams); cipher.init(Cipher.DECRYPT_MODE, key, params); } else { cipher.init(Cipher.DECRYPT_MODE, key); byte[] serialized = cipher.doFinal(encryptedContent); throw new NoSuchAlgorithmException(e.toString()); } catch (InvalidAlgorithmParameterException e) { throw new NoSuchAlgorithmException(e.toString()); } catch (IllegalBlockSizeException e) { throw new NoSuchAlgorithmException(e.toString()); } catch (BadPaddingException e) { throw new NoSuchAlgorithmException(e.toString()); } catch (IllegalStateException e) {
public static String encrypt(String data, byte[] aesKey, byte[] iv) throws BraintreeEncryptionException { SecretKeySpec key = new SecretKeySpec(aesKey, ALGORITHM); Cipher cipher = aesCipher(); try { IvParameterSpec ivParamSpec = new IvParameterSpec(iv); cipher.init(Cipher.ENCRYPT_MODE, key, ivParamSpec); byte[] encryptedBytes = cipher.doFinal(data.getBytes()); byte[] buffer = Arrays.copyOf(iv, iv.length + encryptedBytes.length); System.arraycopy(encryptedBytes, 0, buffer, iv.length, encryptedBytes.length); return new String(Base64.encode(buffer)); } catch (InvalidKeyException e) { throw new BraintreeEncryptionException("Invalid Key: " + e.getMessage()); } catch (BadPaddingException e) { throw new BraintreeEncryptionException("Bad Padding: " + e.getMessage()); } catch (IllegalBlockSizeException e) { throw new BraintreeEncryptionException("Illegal Block Size: " + e.getMessage()); } catch (InvalidAlgorithmParameterException e) { throw new BraintreeEncryptionException("Invalid Algorithm: " + e.getMessage()); } }
public static String decrypt(String value) { try { Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, generateKey()); byte[] decryptedValue64 = Base64.decodeBase64(value); byte[] decryptedByteValue = cipher.doFinal(decryptedValue64); String decryptedValue = new String(decryptedByteValue, "UTF-8"); return decryptedValue; } catch (IllegalBlockSizeException ex) { _logger.warn("Exception: '{}'", ex.getMessage()); _logger.debug("Exception, ", ex); } catch (Exception ex) { _logger.error("Exception, ", ex); } return null; }
public static String encrypt(byte[] data, String publicKeyString) throws BraintreeEncryptionException { Cipher rsa; try { rsa = Cipher.getInstance(TRANSFORMATION); PublicKey publicKey = publicKey(publicKeyString); rsa.init(Cipher.ENCRYPT_MODE, publicKey); byte[] encodedData = Base64.encode(data); byte[] encryptedData = rsa.doFinal(encodedData); return new String(Base64.encode(encryptedData)); } catch (NoSuchAlgorithmException e) { throw new BraintreeEncryptionException("No Such Algorithm: " + e.getMessage()); } catch (NoSuchPaddingException e) { throw new BraintreeEncryptionException("No Such Padding: " + e.getMessage()); } catch (InvalidKeyException e) { throw new BraintreeEncryptionException("Invalid Key: " + e.getMessage()); } catch (IllegalBlockSizeException e) { throw new BraintreeEncryptionException("Illegal Block Size: " + e.getMessage()); } catch (BadPaddingException e) { throw new BraintreeEncryptionException("Bad Padding: " + e.getMessage()); } }
try { if (cipher != null) { result = cipher.doFinal(); if (result != null) { out.write(result); throw new IOException(e.getMessage()); } catch (IllegalBlockSizeException e) { throw new IOException(e.getMessage()); } finally { if (out != null) {
if (provider == null) c = Cipher.getInstance("AES/CCM/NoPadding"); c = Cipher.getInstance("AES/CCM/NoPadding", provider); c.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyBytes, "AES")); byte[] encOut = c.doFinal(storeData.getEncoded()); throw new NoSuchAlgorithmException(e.toString()); throw new IOException(e.toString()); throw new IOException(e.toString()); throw new IOException(e.toString());
private static byte[] encrypt(byte[] content, byte[] keyBytes) { byte[] encryptedText = null; if (!isInited) { init(); } /** *类 SecretKeySpec *可以使用此类来根据一个字节数组构造一个 SecretKey, *而无须通过一个(基于 provider 的)SecretKeyFactory。 *此类仅对能表示为一个字节数组并且没有任何与之相关联的钥参数的原始密钥有用 *构造方法根据给定的字节数组构造一个密钥。 *此构造方法不检查给定的字节数组是否指定了一个算法的密钥。 */ Key key = new SecretKeySpec(keyBytes, "AES"); try { // 用密钥初始化此 cipher。 cipher.init(Cipher.ENCRYPT_MODE, key); } catch (InvalidKeyException e) { e.printStackTrace(); } try { //按单部分操作加密或解密数据,或者结束一个多部分操作。(不知道神马意思) encryptedText = cipher.doFinal(content); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } return encryptedText; }
private byte[] encrypt(byte[] key, byte[] bytes) throws SVNException { Cipher ecipher = getCipher(key); try { byte[] enc = ecipher.doFinal(bytes); return enc; } catch (IllegalBlockSizeException e) { SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.IO_ERROR, "Invalid block size for DES encryption: {0}", e.getLocalizedMessage()); SVNErrorManager.error(err, SVNLogType.NETWORK); } catch (BadPaddingException e) { SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.IO_ERROR, "Data not padded correctly for DES encryption: {0}", e.getLocalizedMessage()); SVNErrorManager.error(err, SVNLogType.NETWORK); } return null; }
byte[] encOut = c.doFinal(storeData.getEncoded()); AlgorithmParameters algorithmParameters = c.getParameters(); byte[] encOut = c.doFinal(storeData.getEncoded()); PBES2Parameters pbeParams = new PBES2Parameters(pbkdAlgId, new EncryptionScheme(NISTObjectIdentifiers.id_aes256_wrap_pad)); throw new NoSuchAlgorithmException(e.toString()); throw new IOException(e.toString()); throw new IOException(e.toString()); throw new IOException(e.toString());
return __verifyNonRepSignature(expectedDigestValue, signatureValue, certificate); } catch (final InvalidKeyException ikex) { LOGGER.warn("invalid key: " + ikex.getMessage(), ikex); return false; } catch (final NoSuchAlgorithmException nsaex) { LOGGER.warn("no such algo: " + nsaex.getMessage(), nsaex); return false; } catch (final NoSuchPaddingException nspex) { LOGGER.warn("no such padding: " + nspex.getMessage(), nspex); return false; } catch (final BadPaddingException bpex) { LOGGER.warn("bad padding: " + bpex.getMessage(), bpex); return false; } catch (final IOException ioex) { return false; } catch (final IllegalBlockSizeException ibex) { LOGGER.warn("illegal block size: " + ibex.getMessage(), ibex); return false;
protected byte[] engineWrap( Key key) throws IllegalBlockSizeException, InvalidKeyException { byte[] encoded = key.getEncoded(); if (encoded == null) { throw new InvalidKeyException("Cannot wrap key, null encoding."); } try { return engineDoFinal(encoded, 0, encoded.length); } catch (BadPaddingException e) { throw new IllegalBlockSizeException(e.getMessage()); } }
@Override public byte[] getSignedData(String alias, byte[] data) throws RemoteException { try { return SimpleSigner.signData(data); } catch (IOException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (InvalidKeySpecException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } // Something failed, return null return null; }
@Override protected byte[] engineWrap(Key key) throws IllegalBlockSizeException, InvalidKeyException { try { byte[] encoded = key.getEncoded(); return engineDoFinal(encoded, 0, encoded.length); } catch (BadPaddingException e) { IllegalBlockSizeException newE = new IllegalBlockSizeException(); newE.initCause(e); throw newE; } }
@Override public String decrypt(final Key key, final byte[] encryptedValue, final byte[] nonce) throws Exception { throw new IllegalBlockSizeException(""); } });
throw new DPKException("Failed to encrypt DPK. Cause: " + e.getLocalizedMessage(), e); } catch (NoSuchPaddingException e) { throw new DPKException("Failed to encrypt DPK. Cause: " + e.getLocalizedMessage(), e); } catch (NoSuchAlgorithmException e) { throw new DPKException("Failed to encrypt DPK. Cause: " + e.getLocalizedMessage(), e); } catch (IllegalBlockSizeException e) { throw new DPKException("Failed to encrypt DPK. Cause: " + e.getLocalizedMessage(), e); } catch (BadPaddingException e) { throw new DPKException("Failed to encrypt DPK. Cause: " + e.getLocalizedMessage(), e); } catch (InvalidAlgorithmParameterException e) { throw new DPKException("Failed to encrypt DPK. Cause: " + e.getLocalizedMessage(), e);
throw PubNubException.builder().errormsg(e.toString()).build(); } catch (NoSuchPaddingException e) { throw PubNubException.builder().errormsg(e.toString()).build();
public byte[] encryptKeyData(byte[] key, byte[] keyData, int keyOff, int keyLen) throws PGPException { try { c = helper.createCipher(PGPUtil.getSymmetricCipherName(this.encAlgorithm) + "/CFB/NoPadding"); c.init(Cipher.ENCRYPT_MODE, JcaJcePGPUtil.makeSymmetricKey(this.encAlgorithm, key), this.random); iv = c.getIV(); return c.doFinal(keyData, keyOff, keyLen); } catch (IllegalBlockSizeException e) { throw new PGPException("illegal block size: " + e.getMessage(), e); } catch (BadPaddingException e) { throw new PGPException("bad padding: " + e.getMessage(), e); } catch (InvalidKeyException e) { throw new PGPException("invalid key: " + e.getMessage(), e); } }
public static String decrypt(String value) { try { Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, generateKey()); byte[] decryptedValue64 = Base64.decodeBase64(value); byte[] decryptedByteValue = cipher.doFinal(decryptedValue64); String decryptedValue = new String(decryptedByteValue, "UTF-8"); return decryptedValue; } catch (IllegalBlockSizeException ex) { _logger.warn("Exception: '{}'", ex.getMessage()); _logger.debug("Exception, ", ex); } catch (Exception ex) { _logger.error("Exception, ", ex); } return null; }