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");
}
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;
}
}
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();
}
}
}
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];
}
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;
}
Aggregations