List of usage examples for javax.crypto.spec SecretKeySpec SecretKeySpec
public SecretKeySpec(byte[] key, String algorithm)
From source file:net.arccotangent.pacchat.crypto.MsgCrypto.java
public static PacchatMessage decryptAndVerifyMessage(String cryptedMsg, PrivateKey privateKey, PublicKey publicKey) {//from w ww . ja v a 2s.c o m mc_log.i("Decrypting and verifying message."); String[] messageComponents = cryptedMsg.split("\n"); String cryptedKeyB64 = messageComponents[0]; String cryptedTextB64 = messageComponents[1]; String signatureB64 = messageComponents[2]; boolean verified = RSA.verifyBytes(Base64.decodeBase64(cryptedKeyB64), Base64.decodeBase64(signatureB64), publicKey); if (verified) mc_log.i("Message authenticity verified!"); else { mc_log.w("**********************************************"); mc_log.w("Message authenticity NOT VERIFIED! Will continue decryption anyway."); mc_log.w( "Someone may be tampering with your connection! This is an unlikely, but not impossible scenario!"); mc_log.w( "If you are sure the connection was not tampered with, consider asking the sender to send out a key update."); mc_log.w("**********************************************"); } DecryptStatus rsa = RSA.decryptBytes(Base64.decodeBase64(cryptedKeyB64), privateKey); byte[] aesKey = rsa.getMessage(); SecretKey aes = new SecretKeySpec(aesKey, "AES"); DecryptStatus message = AES.decryptBytes(Base64.decodeBase64(cryptedTextB64), aes); byte[] msg = message.getMessage(); boolean decrypted = message.isDecryptedSuccessfully(); return new PacchatMessage(new String(msg), verified, decrypted); }
From source file:org.matrix.security.crypto.encrypt.AesBytesEncryptor.java
public AesBytesEncryptor(String password, CharSequence salt, BytesKeyGenerator ivGenerator) { PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray(), Hex.decode(salt), 1024, 256); SecretKey secretKey = newSecretKey("PBKDF2WithHmacSHA1", keySpec); this.secretKey = new SecretKeySpec(secretKey.getEncoded(), "AES"); encryptor = newCipher(AES_ALGORITHM); decryptor = newCipher(AES_ALGORITHM); this.ivGenerator = ivGenerator != null ? ivGenerator : NULL_IV_GENERATOR; }
From source file:de.scrubstudios.srvmon.agent.classes.Crypt.java
/** * This function is used to decrypt incoming data. * @param key Encryption key//from w w w . j a va 2 s . c o m * @param data Data string which should be decrypted. * @return The decrypted data string. */ public static String decrypt(String key, String data) { byte[] decryptedData = null; SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES"); try { Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, keySpec); decryptedData = cipher.doFinal(Base64.decodeBase64(data)); return new String(decryptedData); } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex) { Logger.getLogger(Crypt.class.getName()).log(Level.SEVERE, null, ex); } return null; }
From source file:com.microsoft.azure.batch.auth.BatchCredentialsInterceptor.java
private String sign(String accessKey, String stringToSign) { try {/* w w w . j av a 2s .c o m*/ // Encoding the Signature // Signature=Base64(HMAC-SHA256(UTF8(StringToSign))) Mac hmac = Mac.getInstance("hmacSHA256"); hmac.init(new SecretKeySpec(Base64.decodeBase64(accessKey), "hmacSHA256")); byte[] digest = hmac.doFinal(stringToSign.getBytes("UTF-8")); return Base64.encodeBase64String(digest); } catch (Exception e) { throw new IllegalArgumentException("accessKey", e); } }
From source file:fi.vm.kapa.identification.service.PhaseIdService.java
public PhaseIdService(String sharedSecret, int timeInterval, String hmacAlgorithm) throws Exception { this.timeInterval = timeInterval; hmacCalc = Mac.getInstance(hmacAlgorithm); SecretKeySpec keySpec = new SecretKeySpec(sharedSecret.getBytes(), hmacAlgorithm); hmacCalc.init(keySpec);//from www. j av a2s . com }
From source file:edu.cmu.sei.ams.cloudlet.impl.AESEncrypter.java
public AESEncrypter(String password) { this.password = password; try {/*w w w.j av a 2 s. c om*/ MessageDigest digest = MessageDigest.getInstance("SHA-256"); digest.reset(); this.skeySpec = new SecretKeySpec(digest.digest(this.password.getBytes("UTF-8")), "AES"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } }
From source file:net.fenyo.mail4hotspot.web.GMailOAuthStep1Servlet.java
private String hmac(final String key, final String message) throws InvalidKeyException, NoSuchAlgorithmException, UnsupportedEncodingException { final Mac mac = Mac.getInstance("HmacSHA1"); mac.init(new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA1")); return org.apache.commons.codec.binary.Base64.encodeBase64String(mac.doFinal(message.getBytes("UTF-8"))); }
From source file:com.cloud.consoleproxy.ConsoleProxyPasswordBasedEncryptor.java
public String encryptText(String text) { if (text == null || text.isEmpty()) return text; assert (password != null); assert (!password.isEmpty()); try {/* w ww. j a v a2s .com*/ Cipher cipher = Cipher.getInstance("DES"); int maxKeySize = 8; SecretKeySpec keySpec = new SecretKeySpec(normalizeKey(password.getBytes(), maxKeySize), "DES"); cipher.init(Cipher.ENCRYPT_MODE, keySpec); byte[] encryptedBytes = cipher.doFinal(text.getBytes()); return Base64.encodeBase64URLSafeString(encryptedBytes); } catch (NoSuchAlgorithmException e) { s_logger.error("Unexpected exception ", e); return null; } catch (NoSuchPaddingException e) { s_logger.error("Unexpected exception ", e); return null; } catch (IllegalBlockSizeException e) { s_logger.error("Unexpected exception ", e); return null; } catch (BadPaddingException e) { s_logger.error("Unexpected exception ", e); return null; } catch (InvalidKeyException e) { s_logger.error("Unexpected exception ", e); return null; } }
From source file:model.Encryption.java
public void generateSecretKey() { try {/*from w ww .j a v a2 s .com*/ String specificCryptKey = "zertfghzbenrfzer65z+er56365zr45ze4rz4er534z65rzrz53er4135a456r4az4er34z56er42df13z4er5646z5r4zer4"; MessageDigest shahash = MessageDigest.getInstance("SHA-1"); byte[] key = shahash.digest(specificCryptKey.getBytes("UTF-8")); key = Arrays.copyOf(key, 16); secret = new SecretKeySpec(key, "AES"); } catch (NoSuchAlgorithmException ex) { Logger.getLogger(Encryption.class.getName()).log(Level.SEVERE, null, ex); } catch (UnsupportedEncodingException ex) { Logger.getLogger(Encryption.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.wso2telco.cryptosystem.AESencrp.java
/** * Generate key./*from w w w . jav a2 s . c o m*/ * * @return the key * @throws Exception the exception */ private static Key generateKey() throws Exception { Key key = new SecretKeySpec(keyValue, ALGO); return key; }