Example usage for javax.crypto.spec SecretKeySpec SecretKeySpec

List of usage examples for javax.crypto.spec SecretKeySpec SecretKeySpec

Introduction

In this page you can find the example usage for javax.crypto.spec SecretKeySpec SecretKeySpec.

Prototype

public SecretKeySpec(byte[] key, String algorithm) 

Source Link

Document

Constructs a secret key from the given byte array.

Usage

From source file:Componentes.EncryptionMD5.java

public static String Desencriptar(String encriptado) {
    System.out.println(encriptado);
    String secretKey = "qualityinfosolutions"; //llave para encriptar datos
    String base64EncryptedString = "";

    try {/*www .j ava 2 s .co m*/
        System.out.println("h");
        byte[] message = Base64.decodeBase64(encriptado.getBytes("utf-8"));
        System.out.println("b");
        MessageDigest md = MessageDigest.getInstance("MD5");
        System.out.println("a");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
        System.out.println("c");
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        System.out.println("g");
        SecretKey key = new SecretKeySpec(keyBytes, "DESede");
        System.out.println("w");
        Cipher decipher = Cipher.getInstance("DESede");
        System.out.println("t");
        decipher.init(Cipher.DECRYPT_MODE, key);
        System.out.println("r");
        System.out.println(Arrays.toString(message));
        byte[] plainText = decipher.doFinal(message);
        System.out.println(Arrays.toString(plainText));
        base64EncryptedString = new String(plainText, "UTF-8");

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return base64EncryptedString;
}

From source file:com.javaps.springboot.LicenseController.java

@RequestMapping(value = "/public/license", produces = "text/plain", method = RequestMethod.POST)
public String licenseValidate(HttpServletRequest req, @RequestBody String license) throws Exception {
    String clientIp = req.getHeader("X-Forwarded-For"); //nginx???IP
    if (clientIp == null)
        clientIp = req.getRemoteAddr(); //?????
    //System.out.println("clientIp="+clientIp);
    SecretKeySpec signingKey = new SecretKeySpec(licenseSecretKey.getBytes(), "HmacSHA1");
    Mac mac = Mac.getInstance("HmacSHA1");
    mac.init(signingKey);/*from ww w  . j av a  2  s .com*/

    byte[] rawHmac = mac.doFinal(clientIp.getBytes());
    //System.out.println("license should be:"+Base64.encodeBase64String(rawHmac));
    if (!license.equals(Base64.encodeBase64String(rawHmac)))
        throw new Exception();

    return "OK";
}

From source file:com.tydic.dbp.utils.ThreeDesUtils.java

public static String decryptMode(String srcStr) {
    try {// www. ja v a  2  s  . c o m
        // ?
        SecretKey deskey = new SecretKeySpec(keyBytes, Algorithm);
        // 
        Cipher c1 = Cipher.getInstance(Algorithm);
        c1.init(Cipher.DECRYPT_MODE, deskey);
        return new String(c1.doFinal(Hex.decodeHex(srcStr.toCharArray())));
    } catch (java.security.NoSuchAlgorithmException e1) {
        e1.printStackTrace();
    } catch (javax.crypto.NoSuchPaddingException e2) {
        e2.printStackTrace();
    } catch (java.lang.Exception e3) {
        e3.printStackTrace();
    }
    return null;
}

From source file:com.jsmartframework.web.manager.TagEncrypter.java

static void init() {
    try {/* w w  w  .  j  a  v  a 2  s  .co  m*/
        String customKey = CONFIG.getContent().getTagSecretKey();
        if (StringUtils.isNotBlank(customKey)) {

            if (customKey.length() != CYPHER_KEY_LENGTH) {
                throw new RuntimeException("Custom tag-secret-key must have its value " + "with ["
                        + CYPHER_KEY_LENGTH + "] characters");
            }
            secretKey = new SecretKeySpec(customKey.getBytes("UTF8"), "AES");
        } else {
            secretKey = new SecretKeySpec(DEFAULT_KEY.getBytes("UTF8"), "AES");
        }
    } catch (UnsupportedEncodingException ex) {
        LOGGER.log(Level.INFO, "Failed to generate key secret for tag: " + ex.getMessage());
    }
}

From source file:de.hybris.platform.acceleratorservices.payment.utils.impl.DefaultAcceleratorDigestUtils.java

@Override
public String getPublicDigest(final String customValues, final String key)
        throws NoSuchAlgorithmException, InvalidKeyException {
    final Base64 encoder = new Base64();
    final Mac sha1Mac = Mac.getInstance(getMacAlgorithm());
    final SecretKeySpec publicKeySpec = new SecretKeySpec(key.getBytes(), getMacAlgorithm());
    sha1Mac.init(publicKeySpec);//from w w w.  ja va2s .co  m

    final byte[] publicBytes = sha1Mac.doFinal(customValues.getBytes());
    final String publicDigest = new String(encoder.encode(publicBytes));

    return publicDigest.replaceAll("\n", "");
}

From source file:gov.nih.nci.cadsr.cadsrpasswordchange.core.OracleObfuscation.java

public OracleObfuscation(String secretString) throws GeneralSecurityException {
    key = new SecretKeySpec(secretString.getBytes(), algorithm1);
    cipher = Cipher.getInstance(algorithm2);
}

From source file:D_common.E_ncript.java

public static byte[] decrypt(byte[] data, String keyStr) {
    try {//from ww  w. ja  va2 s.co m
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        final SecretKeySpec secretKey = new SecretKeySpec(makeAESKey(keyStr), "AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        return cipher.doFinal(data);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:adminpassword.Encryption.java

public String encrypt(String word, String idKey) throws Exception {

    byte[] ivBytes;
    String password = idKey; //you can give whatever you want. This is for testing purpose
    SecureRandom random = new SecureRandom();
    byte bytes[] = new byte[20];
    random.nextBytes(bytes);/*from w  w w  .ja  v a 2 s  . c  om*/

    byte[] saltBytes = bytes;

    // Derive the key
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), saltBytes, 65556, 256);
    SecretKey secretKey = factory.generateSecret(spec);
    SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");

    //encrypting the word
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, secret);
    AlgorithmParameters params = cipher.getParameters();
    ivBytes = params.getParameterSpec(IvParameterSpec.class).getIV();
    byte[] encryptedTextBytes = cipher.doFinal(word.getBytes("UTF-8"));

    //prepend salt and vi
    byte[] buffer = new byte[saltBytes.length + ivBytes.length + encryptedTextBytes.length];
    System.arraycopy(saltBytes, 0, buffer, 0, saltBytes.length);
    System.arraycopy(ivBytes, 0, buffer, saltBytes.length, ivBytes.length);
    System.arraycopy(encryptedTextBytes, 0, buffer, saltBytes.length + ivBytes.length,
            encryptedTextBytes.length);
    return new Base64().encodeToString(buffer);
}

From source file:com.kylinolap.rest.security.PasswordPlaceholderConfigurer.java

public static String decrypt(String strToDecrypt) {
    try {//from   w w  w.j a  v a  2  s .  com
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        final String decryptedString = new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt)));
        return decryptedString;
    } catch (Exception e) {
    }
    return null;
}

From source file:de.scrubstudios.srvmon.agent.classes.Crypt.java

/**
 * This function is used to encrypt the outgoing data.
 * @param key Encryption key/*from   ww w .  jav a 2s . co m*/
 * @param data Data which will be encrypted.
 * @return The encrypted data string.
 */
public static String encrypt(String key, String data) {
    byte[] encryptedData = null;
    SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");

    try {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        encryptedData = cipher.doFinal(data.getBytes());

        return new String(Base64.encodeBase64(encryptedData));
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException
            | BadPaddingException ex) {
        Logger.getLogger(Crypt.class.getName()).log(Level.SEVERE, null, ex);
    }

    return null;
}