MD5加密(不能解密)
[java]
view plain
copy
- public
class Encrypter { - //
default algorithm is MD5 -
public static String encrypt(String message) throws Exception { -
return encrypt(message, "MD5"); -
} -
-
// algorithm: MD5 or SHA-1 -
// return string length: 32 if algorithm = MD5, or 40 if algorithm = SHA-1 -
public static String encrypt(String message, String algorithm) -
throws Exception { -
if (message == null) { - //
throw new Exception("message is null."); -
message = ""; -
} -
if (!"MD5".equals(algorithm) && !"SHA-1".equals(algorithm)) { -
throw new Exception("algorithm must be MD5 or SHA-1."); -
} -
byte[] buffer = message.getBytes(); -
-
// The SHA algorithm results in a 20-byte digest, while MD5 is 16 bytes -
// long. -
MessageDigest md = MessageDigest.getInstance(algorithm); -
-
// Ensure the digest's buffer is empty. This isn't necessary the first -
// time used. -
// However, it is good practice to always empty the buffer out in case -
// you later reuse it. -
md.reset(); -
-
// Fill the digest's buffer with data to compute a message digest from. -
md.update(buffer); -
-
// Generate the digest. This does any necessary padding required by the -
// algorithm. -
byte[] digest = md.digest(); -
-
// Save or print digest bytes. Integer.toHexString() doesn't print -
// leading zeros. -
StringBuffer hexString = new StringBuffer(); -
String sHexBit = null; -
for (int i = 0; i < digest.length; i++) { -
sHexBit = Integer.toHexString(0xFF & digest[i]); -
if (sHexBit.length() == 1) { -
sHexBit = "0" + sHexBit; -
} -
hexString.append(sHexBit); -
} -
return hexString.toString(); -
} -
-
public static void main(String[] args) throws Exception { -
System.out.println(Encrypter.encrypt("123456")); -
-
}
DES加密
[java]
view plain
copy
- public
class CryptUtil { -
-
private static final String PASSWORD_CRYPT_KEY = "__jDlog_"; -
-
private static final String DES = "DES"; -
-
public byte[] encrypt(byte[] src, byte[] key) throws Exception { -
SecureRandom sr = new SecureRandom(); -
DESKeySpec dks = new DESKeySpec(key); -
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); -
SecretKey securekey = keyFactory.generateSecret(dks); -
Cipher cipher = Cipher.getInstance(DES); -
cipher.init(Cipher.ENCRYPT_MODE, securekey, sr); -
return cipher.doFinal(src); -
} -
-
public byte[] decrypt(byte[] src, byte[] key) throws Exception { -
SecureRandom sr = new SecureRandom(); -
DESKeySpec dks = new DESKeySpec(key); -
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); -
SecretKey securekey = keyFactory.generateSecret(dks); -
Cipher cipher = Cipher.getInstance(DES); -
cipher.init(Cipher.DECRYPT_MODE, securekey, sr); -
return cipher.doFinal(src); -
} -
-
-
public final String decrypt(String data) { -
try { -
return new String(decrypt(hex2byte(data.getBytes()), -
PASSWORD_CRYPT_KEY.getBytes())); -
} catch (Exception e) { -
} -
return null; -
} -
-
-
public final String encrypt(String password) { -
try { -
return byte2hex(encrypt(password.getBytes(), PASSWORD_CRYPT_KEY -
.getBytes())); -
} catch (Exception e) { -
} -
return null; -
} -
-
public String byte2hex(byte[] b) { -
StringBuffer hs = new StringBuffer(); -
String stmp = ""; -
for (int n = 0; n < b.length; n++) { -
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF)); -
if (stmp.length() == 1) -
hs.append("0").append(stmp); -
else -
hs.append(stmp); -
} -
return hs.toString().toUpperCase(); -
} -
-
public byte[] hex2byte(byte[] b) { -
if ((b.length % 2) != 0) -
throw new IllegalArgumentException ("The length is not an even."); -
byte[] b2 = new byte[b.length / 2]; -
for (int n = 0; n < b.length; n += 2) { -
String item = new String(b, n, 2); -
b2[n / 2] = (byte) Integer.parseInt(item, 16); -
} -
return b2; -
} -
public static void main(String[] args) { -
String pwd="123456"; -
CryptUtil u=new CryptUtil(); -
System.out.println(u.encrypt(pwd)); -
System.out.println(u.decrypt("619034920555A7F3")); -
} - }