Search in sources :

Example 26 with MessageDigest

use of java.security.MessageDigest in project UltimateAndroid by cymcsg.

the class TripleDES method decrypt.

/**
     * Decrypt the message with TripleDES
     *
     * @param message
     * @return
     * @throws Exception
     */
public static String decrypt(String message) throws Exception {
    if (message == null || message == "")
        return "";
    byte[] values = Base64decoding(message, 0);
    final MessageDigest md = MessageDigest.getInstance("SHA-1");
    final byte[] digestOfPassword = md.digest(token.getBytes("utf-8"));
    final byte[] keyBytes = copyOf(digestOfPassword, 24);
    for (int j = 0, k = 16; j < 8; ) {
        keyBytes[k++] = keyBytes[j++];
    }
    final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
    String s1 = "12345678";
    byte[] bytes = s1.getBytes();
    final IvParameterSpec iv = new IvParameterSpec(bytes);
    final Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
    decipher.init(Cipher.DECRYPT_MODE, key, iv);
    final byte[] plainText = decipher.doFinal(values);
    return new String(plainText, "UTF-8");
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MessageDigest(java.security.MessageDigest)

Example 27 with MessageDigest

use of java.security.MessageDigest in project UltimateAndroid by cymcsg.

the class CryptographyUtils method getCryptography.

public static String getCryptography(String content, String encryptType) throws NoSuchAlgorithmException {
    try {
        MessageDigest messageDigest = MessageDigest.getInstance(encryptType);
        byte[] inputByteArray = content.getBytes();
        messageDigest.update(inputByteArray);
        byte[] resultByteArray = messageDigest.digest();
        return byteArrayToHex(resultByteArray);
    } catch (NoSuchAlgorithmException e) {
        throw e;
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 28 with MessageDigest

use of java.security.MessageDigest in project UltimateAndroid by cymcsg.

the class CryptographyUtils method getMd5FromFile.

/**
     * Get the MD5 of the file
     *
     * @param filePath
     * @return
     * @throws IOException
     * @throws NoSuchAlgorithmException
     */
public static String getMd5FromFile(String filePath) throws IOException, NoSuchAlgorithmException {
    int bufferSize = 256 * 1024;
    FileInputStream fileInputStream = null;
    DigestInputStream digestInputStream = null;
    try {
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        fileInputStream = new FileInputStream(filePath);
        digestInputStream = new DigestInputStream(fileInputStream, messageDigest);
        byte[] buffer = new byte[bufferSize];
        while (digestInputStream.read(buffer) > 0) ;
        messageDigest = digestInputStream.getMessageDigest();
        byte[] resultByteArray = messageDigest.digest();
        return byteArrayToHex(resultByteArray);
    } catch (NoSuchAlgorithmException e) {
        throw e;
    } finally {
        if (digestInputStream != null) {
            digestInputStream.close();
        }
        if (fileInputStream != null) {
            fileInputStream.close();
        }
    }
}
Also used : DigestInputStream(java.security.DigestInputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 29 with MessageDigest

use of java.security.MessageDigest in project UltimateAndroid by cymcsg.

the class CryptographyUtils method encryptAlgorithm.

public static byte[] encryptAlgorithm(byte[] data, String algorithm) {
    try {
        MessageDigest md = MessageDigest.getInstance(algorithm);
        md.update(data);
        return md.digest();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return new byte[0];
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 30 with MessageDigest

use of java.security.MessageDigest in project hackpad by dropbox.

the class Main method getDigest.

private static byte[] getDigest(Object source) {
    byte[] bytes, digest = null;
    if (source != null) {
        if (source instanceof String) {
            try {
                bytes = ((String) source).getBytes("UTF-8");
            } catch (UnsupportedEncodingException ue) {
                bytes = ((String) source).getBytes();
            }
        } else {
            bytes = (byte[]) source;
        }
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            digest = md.digest(bytes);
        } catch (NoSuchAlgorithmException nsa) {
            // Should not happen
            throw new RuntimeException(nsa);
        }
    }
    return digest;
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Aggregations

MessageDigest (java.security.MessageDigest)1122 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)562 IOException (java.io.IOException)161 UnsupportedEncodingException (java.io.UnsupportedEncodingException)93 BigInteger (java.math.BigInteger)93 FileInputStream (java.io.FileInputStream)68 InputStream (java.io.InputStream)66 File (java.io.File)58 DigestInputStream (java.security.DigestInputStream)56 Test (org.junit.Test)55 ByteArrayOutputStream (java.io.ByteArrayOutputStream)46 DigestOutputStream (java.security.DigestOutputStream)43 ArrayList (java.util.ArrayList)32 ByteArrayInputStream (java.io.ByteArrayInputStream)29 OutputStream (java.io.OutputStream)27 X509Certificate (java.security.cert.X509Certificate)27 GeneralSecurityException (java.security.GeneralSecurityException)25 Cipher (javax.crypto.Cipher)23 Provider (java.security.Provider)22 SecretKeySpec (javax.crypto.spec.SecretKeySpec)22