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 Optional<String> validateRsaSha256(SecurityEnvironment env, InboundClientDefinition clientDefinition) { try { Signature signature = Signature.getInstance("SHA256withRSA"); signature.initVerify(clientDefinition.keyConfig() .orElseThrow(() -> new HttpSignatureException("RSA public key configuration is " + "required")) .publicKey() .orElseThrow(() -> new HttpSignatureException( "Public key is required, yet not configured"))); signature.update(getBytesToSign(env, null)); if (!signature.verify(this.signatureBytes)) { return Optional.of("Signature is not valid"); } return Optional.empty(); } catch (NoSuchAlgorithmException e) { LOGGER.log(Level.FINEST, "SHA256withRSA algorithm not found", e); return Optional.of("SHA256withRSA algorithm not found: " + e.getMessage()); } catch (InvalidKeyException e) { LOGGER.log(Level.FINEST, "Invalid RSA key", e); return Optional.of("Invalid RSA key: " + e.getMessage()); } catch (SignatureException e) { LOGGER.log(Level.FINEST, "Signature exception", e); return Optional.of("SignatureException: " + 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; }
@Override public final InvalidKeyException invalidKeyNoSuchMessageDigestAlgorithm(final String algorithm) { final InvalidKeyException result = new InvalidKeyException(String.format(getLoggingLocale(), invalidKeyNoSuchMessageDigestAlgorithm$str(), algorithm)); final StackTraceElement[] st = result.getStackTrace(); result.setStackTrace(Arrays.copyOfRange(st, 1, st.length)); return result; } private static final String invalidKeyCannotVerifyPassword = "ELY08015: Cannot verify password";
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; }
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()); } }
private String generateHash(String ApiKey, String timestamp) { Mac sha256_HMAC = null; String message = ApiKey + timestamp; try { sha256_HMAC = Mac.getInstance(HMAC_SHA_256); SecretKeySpec keySpec = new SecretKeySpec(SecretKey, HMAC_SHA_256); sha256_HMAC.init(keySpec); return Base64.getEncoder().encodeToString(sha256_HMAC.doFinal(message.getBytes())); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } return ""; } }
public void setKey(byte[] key, int keyOff, int keyLen) { try { mac.init(new SecretKeySpec(key, keyOff, keyLen, algorithm)); } catch (InvalidKeyException e) { e.printStackTrace(); } }
@Override public String signString(String stringToSign, String accessKeySecret) { try { Mac mac = Mac.getInstance(ALGORITHM_NAME); mac.init(new SecretKeySpec(accessKeySecret.getBytes(ENCODING), ALGORITHM_NAME)); byte[] signData = mac.doFinal(stringToSign.getBytes(ENCODING)); return DatatypeConverter.printBase64Binary(signData); } catch (NoSuchAlgorithmException e) { throw new IllegalArgumentException(e.toString()); } catch (UnsupportedEncodingException e) { throw new IllegalArgumentException(e.toString()); } catch (InvalidKeyException e) { throw new IllegalArgumentException(e.toString()); } }
@Override protected SecretKey engineTranslateKey(SecretKey secretKey) throws InvalidKeyException { if (secretKey == null) { throw new InvalidKeyException("Null SecretKey"); } return new SecretKeySpec(secretKey.getEncoded(), secretKey.getAlgorithm()); } }
/** * Enumeration of the Elliptic Curve Diffie-Hellman Ephemeral Static * algorithm modes. */ public enum AlgorithmMode { /** * Direct key agreement mode. */ DIRECT, /** * Key wrapping mode. */ KW }
private Cipher getAndInitializeCipher(final int mode, final byte[] nonce) { try { Cipher cipher = CIPHER.get(); cipher.init(mode, key, gcmParameterSpec(nonce)); return cipher; } catch (InvalidKeyException e) { throw OException.wrapException(new OInvalidStorageEncryptionKeyException(e.getMessage()), e); } catch (InvalidAlgorithmParameterException e) { throw new IllegalArgumentException("Invalid or re-used nonce.", e); } }
@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 void engineInit( int opmode, Key key, SecureRandom random) throws InvalidKeyException { try { engineInit(opmode, key, (AlgorithmParameterSpec)null, random); } catch (InvalidAlgorithmParameterException e) { throw new InvalidKeyException(e.getMessage()); } }
@Override boolean verify(char[] guess) throws InvalidKeyException { // The OTP SASL mechanism handles this (this involves updating the stored password) throw new InvalidKeyException(); }
@Override public byte[] sign(byte[] data) { try { return doSign(data); } catch (InvalidKeyException e) { throw new SignatureException("Invalid RSA PrivateKey. " + e.getMessage(), e); } catch (java.security.SignatureException e) { throw new SignatureException("Unable to calculate signature using RSA PrivateKey. " + e.getMessage(), e); } }
public static KeyPair extractEDDSAKeyPair(Buffer buffer, String keyType) throws GeneralSecurityException { if (!KeyPairProvider.SSH_ED25519.equals(keyType)) { throw new InvalidKeyException("Unsupported key type: " + keyType); } if (!isEDDSACurveSupported()) { throw new NoSuchAlgorithmException(EDDSA + " provider not supported"); } throw new GeneralSecurityException("Full SSHD-440 implementation N/A"); }
byte[] updateSalt = updateSpec.getSalt(); if (updateSalt != null && ! Arrays.equals(updateSalt, salt)) { throw new InvalidAlgorithmParameterException(); throw new InvalidAlgorithmParameterException(); addIterations(digest, getMacInstance(algorithm, digest), this.iterationCount, updateIterationCount); } catch (NoSuchAlgorithmException | InvalidKeyException e) { throw new InvalidKeyException(e); int updateIterationCount = updateSpec.getIterationCount(); if (updateIterationCount < this.iterationCount) { throw new InvalidAlgorithmParameterException(); addIterations(digest, getMacInstance(algorithm, digest), this.iterationCount, updateIterationCount); } catch (NoSuchAlgorithmException | InvalidKeyException e) { throw new InvalidKeyException(e);
private Signature createSignature(String encodedHeader, String encodedClaims) throws NoSuchAlgorithmException, SignatureException, RealmUnavailableException { byte[] headerDecoded = Base64.getUrlDecoder().decode(encodedHeader); JsonObject headers = Json.createReader(ByteIterator.ofBytes(headerDecoded).asInputStream()).readObject(); String headerAlg = resolveAlgorithm(headers); Signature signature = Signature.getInstance(headerAlg); try { PublicKey publicKey = resolvePublicKey(headers); if (publicKey == null) { log.debug("Public key could not be resolved."); return null; } signature.initVerify(publicKey); } catch (InvalidKeyException e) { e.printStackTrace(); return null; } signature.update((encodedHeader + "." + encodedClaims).getBytes()); return signature; }