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 invalidKey(); return new PKCS8EncodedKeySpec(decryptedData); } catch (NoSuchPaddingException e) { 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; }
private byte[] pkcs7UnPad(byte[] buffer) throws BadPaddingException { byte last = buffer[buffer.length - 1]; int i = buffer.length - 2; while (buffer[i] == last) { i--; } if (i + 1 + last != buffer.length) { throw new BadPaddingException(); } return Arrays.copyOfRange(buffer, 0, i + 1); }
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(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()); } }
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; }
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) {
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()); } }
oos.writeObject(object); oos.flush(); AlgorithmParameters ap = c.getParameters(); this.encodedParams = (ap == null) ? null : ap.getEncoded(); this.paramsAlg = (ap == null) ? null : ap.getAlgorithm(); this.sealAlg = c.getAlgorithm(); this.encryptedContent = c.doFinal(bos.toByteArray()); } catch (BadPaddingException e) { throw new IOException(e.toString());
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[] decryptedData = cipher.doFinal(encryptedData); try { ASN1PrivateKeyInfo.verify(decryptedData); throw new InvalidKeySpecException("Decrypted data does not represent valid PKCS#8 PrivateKeyInfo"); return new PKCS8EncodedKeySpec(decryptedData); } catch (IllegalStateException e) { throw new InvalidKeySpecException(e.getMessage()); } catch (IllegalBlockSizeException e) { throw new InvalidKeySpecException(e.getMessage()); } catch (BadPaddingException e) { throw new InvalidKeySpecException(e.getMessage());
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());
@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; }
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()); } }
protected int engineDoFinal( byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws IllegalBlockSizeException, BadPaddingException { int len = 0; if (inputLen != 0) { len = cipher.processBytes(input, inputOffset, inputLen, output, outputOffset); } try { return (len + cipher.doFinal(output, outputOffset + len)); } catch (DataLengthException e) { throw new IllegalBlockSizeException(e.getMessage()); } catch (InvalidCipherTextException e) { throw new BadPaddingException(e.getMessage()); } }
public static InputStream decrypt(InputStream input, byte[] password, byte[] salt) { try { Cipher c = init(password, salt, Cipher.DECRYPT_MODE); return new CipherInputStream(input, c); } catch (BadPaddingException e) { throw new SecurityException("Error decrypting a secret: " + e.getMessage() + ". Invalid input data and/or a password."); } catch (GeneralSecurityException e) { throw new SecurityException("Error decrypting a secret: " + e.getMessage()); } }
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);
Cipher cipher = Cipher.getInstance(sealAlg, provider); 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) {
@Override public byte[] getSignature() { try { return cipher.doFinal(); } catch (IllegalBlockSizeException ex) { throw new IllegalStateException("IllegalBlockSizeException: " + ex.getMessage()); } catch (BadPaddingException ex) { throw new IllegalStateException("BadPaddingException: " + ex.getMessage()); } }