List of usage examples for javax.crypto.spec SecretKeySpec SecretKeySpec
public SecretKeySpec(byte[] key, String algorithm)
From source file:ar.gob.ambiente.servicios.gestionpersonas.entidades.util.CriptPass.java
/** * Mtodo para desencriptar una contrasea encriptada * @param textoEncriptado/*from ww w.ja v a 2 s. c om*/ * @return * @throws Exception */ public static String desencriptar(String textoEncriptado) throws Exception { String secretKey = "zorbazorbas"; //llave para encriptar datos String base64EncryptedString = ""; try { byte[] message = Base64.decodeBase64(textoEncriptado.getBytes("utf-8")); MessageDigest md = MessageDigest.getInstance("MD5"); byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8")); byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); SecretKey key = new SecretKeySpec(keyBytes, "DESede"); Cipher decipher = Cipher.getInstance("DESede"); decipher.init(Cipher.DECRYPT_MODE, key); byte[] plainText = decipher.doFinal(message); base64EncryptedString = new String(plainText, "UTF-8"); } catch (UnsupportedEncodingException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex) { System.out.println(ex.getMessage()); } return base64EncryptedString; }
From source file:illab.nabal.util.SecurityHelper.java
/** * Get HMACSHA1-encoded OAuth 1.0a signature string. * // ww w . j a va2s . c o m * @param secretKey - secret key to encode basestring with * @param baseString - signature base string * @return oauthSignature - HMAC-SHA1 encoded signature string * @throws Exception */ public static String getHmacSha1Signature(String secretKey, String baseString) throws Exception { String oauthSignature = null; // #################### IMPORTANT #################### // the secret key is the concatenated values (each first encoded per Parameter // Encoding) of the Consumer Secret and Token Secret, separated by an '&' character // (ASCII code 38) even if empty. if (StringHelper.isAllFull(secretKey, baseString) == true) { byte[] keyBytes = secretKey.getBytes(HTTP.UTF_8); SecretKey keySpec = new SecretKeySpec(keyBytes, HMAC_SHA1); Mac mac = Mac.getInstance(HMAC_SHA1); mac.init(keySpec); oauthSignature = new String(Base64.encode(mac.doFinal(baseString.getBytes(HTTP.UTF_8)), Base64.DEFAULT), HTTP.UTF_8).trim(); } return oauthSignature; }
From source file:Pusher.java
/** * Returns a HMAC/SHA256 representation of the given string * @param data// w w w . j a v a 2 s .com * @return */ private static String hmacsha256Representation(String data) { try { // Create the HMAC/SHA256 key from application secret final SecretKeySpec signingKey = new SecretKeySpec(pusherApplicationSecret.getBytes(), "HmacSHA256"); // Create the message authentication code (MAC) final Mac mac = Mac.getInstance("HmacSHA256"); mac.init(signingKey); //Process and return data byte[] digest = mac.doFinal(data.getBytes("UTF-8")); digest = mac.doFinal(data.getBytes()); //Convert to string BigInteger bigInteger = new BigInteger(1, digest); return String.format("%0" + (digest.length << 1) + "x", bigInteger); } catch (NoSuchAlgorithmException nsae) { //We should never come here, because GAE has HMac SHA256 throw new RuntimeException("No HMac SHA256 algorithm"); } catch (UnsupportedEncodingException e) { //We should never come here, because UTF-8 should be available throw new RuntimeException("No UTF-8"); } catch (InvalidKeyException e) { throw new RuntimeException("Invalid key exception while converting to HMac SHA256"); } }
From source file:cl.whyem.testsutilityproject.otpgenerator.KeyBase.java
protected SecretKeySpec secret(byte[] key) { return new SecretKeySpec(key, "RAW"); }
From source file:com.vmware.o11n.plugin.crypto.service.CryptoEncryptionService.java
/** * AES Encryption CBC Mode with PKCS5 Padding * * @param dataB64 Data to encrypt Base64 encoded * @param secretB64 Encryption secret Base64 encoded. For AES128 this should be 128 bits (16 bytes) long. For AES256 this should be 256 bits (32 bytes) long. * @param ivB64 Initialization Vector Base64 encoded. 16 bytes long * @return Encrypted data Base64 Encoded * @throws NoSuchAlgorithmException/*from w w w.j ava2 s. c o m*/ * @throws NoSuchPaddingException * @throws InvalidKeyException * @throws InvalidAlgorithmParameterException * @throws IOException * @throws BadPaddingException * @throws IllegalBlockSizeException */ public String aesEncrypt(String dataB64, String secretB64, String ivB64) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { String encryptedB64 = null; final byte[] dataBytes = Base64.decodeBase64(dataB64); final byte[] secretBytes = Base64.decodeBase64(secretB64); final byte[] ivBytes = Base64.decodeBase64(ivB64); final Cipher cipher = Cipher.getInstance(AES_CIPHER); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(secretBytes, "AES"), new IvParameterSpec(ivBytes, 0, cipher.getBlockSize())); encryptedB64 = Base64.encodeBase64String(cipher.doFinal(dataBytes)); return encryptedB64; }
From source file:net.jingx.main.Main.java
public static String computePin(String secret, Long counter) { if (secret == null || secret.length() == 0) { return "Null or empty secret"; }// ww w . ja va 2 s .c o m try { final byte[] keyBytes = Base32String.decode(secret); Mac mac = Mac.getInstance("HMACSHA1"); mac.init(new SecretKeySpec(keyBytes, "")); PasscodeGenerator pcg = new PasscodeGenerator(mac); if (counter == null) { // time-based totp return pcg.generateTimeoutCode(); } else { // counter-based hotp return pcg.generateResponseCode(counter); } } catch (GeneralSecurityException e) { return "General security exception"; } catch (DecodingException e) { return "Decoding exception"; } }
From source file:com.opentok.test.Helpers.java
private static String signData(String data, String key) throws SignatureException, NoSuchAlgorithmException, InvalidKeyException { SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM); Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey);/*w ww. ja v a 2 s . com*/ return toHexString(mac.doFinal(data.getBytes())); }
From source file:edu.kit.dama.util.CryptUtil.java
/** * Hidden constuctor.//from w w w .ja v a 2 s.co m * * @param pSecret The secret used for the SecretKeySpec. The secret must * have a length of 128, 192 or 256 bits. */ private CryptUtil(byte[] pSecret) { try { SecretKeySpec skeySpec = new SecretKeySpec(pSecret, "AES"); deCipher = Cipher.getInstance("AES/CBC/PKCS5Padding", new BouncyCastleProvider()); deCipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[16])); enCipher = Cipher.getInstance("AES/CBC/PKCS5Padding", new BouncyCastleProvider()); enCipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[16])); } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException e) { throw new IllegalStateException("Failed to create cipher instances.", e); } }
From source file:com.wso2telco.gsma.authenticators.cryptosystem.AESencrp.java
/** * Generate key./* ww w. j ava2 s .c om*/ * * @return the key * @throws Exception the exception */ private static Key generateKey() throws Exception { key = configurationService.getDataHolder().getMobileConnectConfig().getSmsConfig().getAesKey(); keyValue = key.getBytes(Charset.forName("UTF-8")); Key key = new SecretKeySpec(keyValue, ALGO); return key; }
From source file:io.pivotal.cla.security.GitHubSignature.java
private byte[] sign(String body, String token) throws NoSuchAlgorithmException, InvalidKeyException { SecretKeySpec signingKey = new SecretKeySpec(token.getBytes(), HMAC_SHA1_ALGORITHM); Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey);/*from w w w . j av a 2 s .co m*/ byte[] expectedBytes = mac.doFinal(body.getBytes()); return expectedBytes; }