@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 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()); } }
/** * 计算数据的Hmac值 * * @param binaryData 二进制数据 * @param key 秘钥 * @return 加密后的hmacsha1值 */ public static byte[] HmacSha1(byte[] binaryData, String key) throws Exception { try { Mac mac = Mac.getInstance(HMAC_SHA1); SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1); mac.init(secretKey); byte[] HmacSha1Digest = mac.doFinal(binaryData); return HmacSha1Digest; } catch (NoSuchAlgorithmException e) { LOG.error("mac not find algorithm {}", HMAC_SHA1); throw e; } catch (InvalidKeyException e) { LOG.error("mac init key {} occur a error {}", key, e.toString()); throw e; } catch (IllegalStateException e) { LOG.error("mac.doFinal occur a error {}", e.toString()); throw e; } }
@Override public String signString(String stringToSign, String accessKeySecret) { try { Signature rsaSign = Signature.getInstance("SHA256withRSA"); KeyFactory kf = KeyFactory.getInstance("RSA"); byte[] keySpec = DatatypeConverter.parseBase64Binary(accessKeySecret); PrivateKey privateKey = kf.generatePrivate(new PKCS8EncodedKeySpec(keySpec)); rsaSign.initSign(privateKey); rsaSign.update(stringToSign.getBytes(ENCODING)); byte[] sign = rsaSign.sign(); String signature = new String(DatatypeConverter.printBase64Binary(sign)); return signature; } catch (NoSuchAlgorithmException e) { throw new IllegalArgumentException(e.toString()); } catch (InvalidKeySpecException e) { throw new IllegalArgumentException(e.toString()); } catch (InvalidKeyException e) { throw new IllegalArgumentException(e.toString()); } catch (SignatureException e) { throw new IllegalArgumentException(e.toString()); } catch (UnsupportedEncodingException e) { throw new IllegalArgumentException(e.toString()); } }
/** * computes a HMAC-SHA1 Signature * * @param data the data * @param key the key * @return the signature * @throws IOException if an error occurs */ public static byte[] computeRFC2104HMAC(byte[] data, byte[] key) throws IOException { try { SecretKeySpec signingKey = new SecretKeySpec(key, "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(signingKey); byte[] rawHmac = mac.doFinal(data); return encodeBase64(rawHmac); } catch (NoSuchAlgorithmException nsae) { throw new IOException("required algorithm HmacSHA1 is not supported by environment: " + nsae.toString()); } catch (InvalidKeyException ke) { throw new IOException("error occured by initializing mac: " + ke.toString()); } }
@Override public String signString(String stringToSign, String accessKeySecret) { try { Signature rsaSign = Signature.getInstance("SHA256withRSA"); KeyFactory kf = KeyFactory.getInstance("RSA"); byte[] keySpec = DatatypeConverter.parseBase64Binary(accessKeySecret); PrivateKey privateKey = kf.generatePrivate(new PKCS8EncodedKeySpec(keySpec)); rsaSign.initSign(privateKey); rsaSign.update(stringToSign.getBytes(ENCODING)); byte[] sign = rsaSign.sign(); String signature = new String(DatatypeConverter.printBase64Binary(sign)); return signature; } catch (NoSuchAlgorithmException e) { throw new IllegalArgumentException(e.toString()); } catch (InvalidKeySpecException e) { throw new IllegalArgumentException(e.toString()); } catch (InvalidKeyException e) { throw new IllegalArgumentException(e.toString()); } catch (SignatureException e) { throw new IllegalArgumentException(e.toString()); } catch (UnsupportedEncodingException e) { throw new IllegalArgumentException(e.toString()); } }
private IMode getMode(byte[] key, byte[] iv, int state) { IBlockCipher cipher = CipherFactory.getInstance(properties.get("cipher")); if (cipher == null) { throw new IllegalArgumentException("no such cipher: " + properties.get("cipher")); } int blockSize = cipher.defaultBlockSize(); if (properties.containsKey("block-size")) { try { blockSize = Integer.parseInt(properties.get("block-size")); } catch (NumberFormatException nfe) { throw new IllegalArgumentException("bad block size: " + nfe.getMessage()); } } IMode mode = ModeFactory.getInstance(properties.get("mode"), cipher, blockSize); if (mode == null) { throw new IllegalArgumentException("no such mode: " + properties.get("mode")); } HashMap modeAttr = new HashMap(); modeAttr.put(IMode.KEY_MATERIAL, key); modeAttr.put(IMode.STATE, new Integer(state)); modeAttr.put(IMode.IV, iv); try { mode.init(modeAttr); } catch (InvalidKeyException ike) { throw new IllegalArgumentException(ike.toString()); } return mode; } }
throw new AuthTokenException(e.toString()); } catch (InvalidKeyException e) { throw new AuthTokenException(e.toString());
throw new AuthTokenException(e.toString()); } catch (InvalidKeyException e) { throw new AuthTokenException(e.toString());
mode.init(modeAttr); } catch (InvalidKeyException ike) { throw new IllegalArgumentException(ike.toString());
mac.init(macAttr); } catch (InvalidKeyException shouldNotHappen) { throw new Error(shouldNotHappen.toString());
throw new IOException(e.toString());
throw new IOException(e.toString());
public String encrypt(String input) throws PubNubException { try { initCiphers(); AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes); SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = null; cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec); return new String(Base64.encode(cipher.doFinal(input.getBytes("UTF-8")), 0), Charset.forName("UTF-8")); } catch (NoSuchAlgorithmException e) { throw PubNubException.builder().errormsg(e.toString()).build(); } catch (NoSuchPaddingException e) { throw PubNubException.builder().errormsg(e.toString()).build(); } catch (InvalidKeyException e) { throw PubNubException.builder().errormsg(e.toString()).build(); } catch (InvalidAlgorithmParameterException e) { throw PubNubException.builder().errormsg(e.toString()).build(); } catch (UnsupportedEncodingException e) { throw PubNubException.builder().errormsg(e.toString()).build(); } catch (IllegalBlockSizeException e) { throw PubNubException.builder().errormsg(e.toString()).build(); } catch (BadPaddingException e) { throw PubNubException.builder().errormsg(e.toString()).build(); } }
public String encrypt(String input) throws PubNubException { try { initCiphers(); AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes); SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = null; cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec); return new String(Base64.encode(cipher.doFinal(input.getBytes("UTF-8")), 0), Charset.forName("UTF-8")); } catch (NoSuchAlgorithmException e) { throw PubNubException.builder().errormsg(e.toString()).build(); } catch (NoSuchPaddingException e) { throw PubNubException.builder().errormsg(e.toString()).build(); } catch (InvalidKeyException e) { throw PubNubException.builder().errormsg(e.toString()).build(); } catch (InvalidAlgorithmParameterException e) { throw PubNubException.builder().errormsg(e.toString()).build(); } catch (UnsupportedEncodingException e) { throw PubNubException.builder().errormsg(e.toString()).build(); } catch (IllegalBlockSizeException e) { throw PubNubException.builder().errormsg(e.toString()).build(); } catch (BadPaddingException e) { throw PubNubException.builder().errormsg(e.toString()).build(); } }