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 ""; } }
private String generateHash(String key, String data) { Mac sha256_HMAC = null; String result = null; try { byte[] byteKey = key.getBytes("UTF-8"); final String HMAC_SHA256 = "HmacSHA256"; sha256_HMAC = Mac.getInstance(HMAC_SHA256); SecretKeySpec keySpec = new SecretKeySpec(byteKey, HMAC_SHA256); sha256_HMAC.init(keySpec); byte[] mac_data = sha256_HMAC.doFinal(data.getBytes()); return Base64.getEncoder().encodeToString(mac_data); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvalidKeyException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { } return ""; } }
e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace();
e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace();
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; }
e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (InvalidAlgorithmParameterException e) { e.printStackTrace();
cipher.init(Cipher.DECRYPT_MODE, aesKey); } catch (InvalidKeyException e) { e.printStackTrace();
e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace();
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 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; }
e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace();
e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace();
e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace();
@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; }
public void setKey(byte[] key, int keyOff, int keyLen) { try { mac.init(new SecretKeySpec(key, keyOff, keyLen, algorithm)); } catch (InvalidKeyException e) { e.printStackTrace(); } }
public void setKey(byte[] key, int keyOff, int keyLen) { try { hmac.init(new SecretKeySpec(key, keyOff, keyLen, algorithm)); } catch (InvalidKeyException e) { e.printStackTrace(); } }
public void initialize(byte[] key) { byte[] nkey = new byte[keysize >> 3]; System.arraycopy(key, 0, nkey, 0, nkey.length); try { sessionKey = Rijndael_Algorithm.makeKey(nkey); } catch (InvalidKeyException e) { //can't happen? e.printStackTrace(); } } }
private static Mac initialize(final byte[] byteKey) { final Mac hmacSha512 = getInstance(HMAC_SHA512); final SecretKeySpec keySpec = new SecretKeySpec(byteKey, HMAC_SHA512); try { hmacSha512.init(keySpec); } catch (InvalidKeyException e) { e.printStackTrace(); } return hmacSha512; }
public void initialize(byte[] key) { byte[] nkey = new byte[keysize >> 3]; System.arraycopy(key, 0, nkey, 0, nkey.length); try { sessionKey = Rijndael_Algorithm.makeKey(nkey); } catch (InvalidKeyException e) { //can't happen? e.printStackTrace(); } } }
@Override public final void initialize(byte[] key) { try { byte[] nkey=new byte[keysize>>3]; System.arraycopy(key, 0, nkey, 0, nkey.length); sessionKey=Rijndael_Algorithm.makeKey(nkey, blocksize/8); } catch (InvalidKeyException e) { e.printStackTrace(); Logger.error(this,"Invalid key"); } }