```java
import org.apache.commons.codec.digest.DigestUtils;
public class MD5 {
/**
* MD5方法
*
* @param text 明文
* @param key 密钥
* @return 密文
* @throws Exception
*/
public static String md5(String text, String key) {
//加密后的字符串
String encodeStr=DigestUtils.md5Hex(text + key);
// System.out.println("MD5加密后的字符串为:encodeStr="+encodeStr);
return encodeStr;
}
/**
* MD5验证方法
*
* @param text 明文
* @param key 密钥
* @param md5 密文
* @return true/false
* @throws Exception
*/
public static boolean verify(String text, String key, String md5) {
//根据传入的密钥进行验证
String md5Text = md5(text, key);
if(md5Text.equalsIgnoreCase(md5))
{
// System.out.println("MD5验证通过");
return true;
}
return false;
}
}
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class DES3EncryptionUtil {
public static byte[] hex(String key){
String f = DigestUtils.md5Hex(key);
byte[] bkeys = new String(f).getBytes();
byte[] enk = new byte[24];
for (int i=0;i<24;i++){
enk[i] = bkeys[i];
}
return enk;
}
public static String encode3Des(String key,String srcStr){
byte[] keybyte = hex(key);
byte[] src = srcStr.getBytes();
try {
SecretKey deskey = new SecretKeySpec(keybyte, "DESede");
Cipher c1 = Cipher.getInstance("DESede");
c1.init(Cipher.ENCRYPT_MODE, deskey);
String pwd = Base64.encodeBase64String(c1.doFinal(src));
return pwd;
} catch (java.security.NoSuchAlgorithmException e1) {
e1.printStackTrace();
}catch(javax.crypto.NoSuchPaddingException e2){
e2.printStackTrace();
}catch(java.lang.Exception e3){
e3.printStackTrace();
}
return null;
}
public static String decode3Des(String key, String desStr){
Base64 base64 = new Base64();
byte[] keybyte = hex(key);
byte[] src = base64.decode(desStr);
try {
SecretKey deskey = new SecretKeySpec(keybyte, "DESede");
Cipher c1 = Cipher.getInstance("DESede");
c1.init(Cipher.DECRYPT_MODE, deskey);
String pwd = new String(c1.doFinal(src));
return pwd;
} catch (java.security.NoSuchAlgorithmException e1) {
e1.printStackTrace();
}catch(javax.crypto.NoSuchPaddingException e2){
e2.printStackTrace();
}catch(java.lang.Exception e3){
e3.printStackTrace();
}
return null;
}
}
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
import java.security.Security;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
public class CryptologyUtils {
public static final String ALGORITHM = "AES/ECB/PKCS7Padding";
static {
Security.addProvider(new BouncyCastleProvider());
}
public static String calculateSha1Checksum(Map<String, String> input) {
String contextString = reformDictionaryToSortedString(input);
byte[] hashBytes = new byte[0];
MessageDigest digest = null;
try {
digest = MessageDigest.getInstance("SHA-1");
digest.update(contextString.getBytes("UTF-8"));
hashBytes = digest.digest();
} catch (Exception e) {
e.printStackTrace();
}
return byteArrayToHexString(hashBytes);
}
public static String encryptDataWithAes(String str, String key) {
byte[] result = null;
byte[] keyInBytes = hexStringToByteArray(key);
int byteLength = keyInBytes.length;
if (byteLength != 16 && byteLength != 24 && byteLength != 32) {
throw new RuntimeException("AES key length not 128/192/256 bits.");
}
try {
Cipher cipher = Cipher.getInstance(ALGORITHM, "BC");
SecretKeySpec keySpec = new SecretKeySpec(keyInBytes, "AES");
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
result = cipher.doFinal(str.getBytes("UTF-8"));
} catch (Exception e) {
e.printStackTrace();
}
return byteArrayToHexString(result);
}
public static String decryptDataWithAes(String toDecrypt, String key) {
String result = null;
byte[] toDecryptInBytes = hexStringToByteArray(toDecrypt);
byte[] keyInBytes = hexStringToByteArray(key);
int byteLength = keyInBytes.length;
if (byteLength != 16 && byteLength != 24 && byteLength != 32) {
throw new RuntimeException("AES key length not 128/192/256 bits.");
}
try {
Cipher cipher = Cipher.getInstance(ALGORITHM, "BC");
SecretKeySpec keySpec = new SecretKeySpec(keyInBytes, "AES");
cipher.init(Cipher.DECRYPT_MODE, keySpec);
byte[] decoded = cipher.doFinal(toDecryptInBytes);
result = new String(decoded, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static String reformDictionaryToSortedString(Map<String, String> input) {
StringBuilder sb = new StringBuilder();
List<String> list = new ArrayList<String>(input.keySet());
Collections.sort(list, new Comparator<String>() {
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
for (String key : list) {
sb.append(key).append(input.get(key));
}
return sb.toString();
}
public static String byteArrayToHexString(byte[] bytes) {
StringBuilder hexString = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
String shaHex = Integer.toHexString(bytes[i] & 0xFF);
if (shaHex.length() < 2) {
hexString.append(0);
}
hexString.append(shaHex);
}
return hexString.toString().replace("-", "").toUpperCase();
}
private static byte[] hexStringToByteArray(String hexString) {
int len = (hexString.length() / 2);
byte[] result = new byte[len];
char[] achar = hexString.toCharArray();
for (int i = 0; i < len; i++) {
int pos = i * 2;
result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]) & 0xff);
}
return result;
}
private static byte toByte(char c) {
byte b = (byte) "0123456789ABCDEF".indexOf(c);
return b;
}
}