javaå å¯å符串å¯ä»¥ä½¿ç¨deså å¯ç®æ³ï¼å®ä¾å¦ä¸ï¼
package test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.*;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
/**
* å å¯è§£å¯
*
* @author shy.qiu
* @since
http://blog.csdn.net/qiushyfm */
public class CryptTest {
/**
* è¿è¡MD5å å¯
*
* @param info
* è¦å å¯çä¿¡æ¯
* @return String å å¯åçå符串
*/
public String encryptToMD5(String info) {
byte[] digesta = null;
try {
// å¾å°ä¸ä¸ªmd5çæ¶æ¯æè¦
MessageDigest alga = MessageDigest.getInstance("MD5");
// æ·»å è¦è¿è¡è®¡ç®æè¦çä¿¡æ¯
alga.update(info.getBytes());
// å¾å°è¯¥æè¦
digesta = alga.digest();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
// å°æè¦è½¬ä¸ºå符串
String rs = byte2hex(digesta);
return rs;
}
/**
* è¿è¡SHAå å¯
*
* @param info
* è¦å å¯çä¿¡æ¯
* @return String å å¯åçå符串
*/
public String encryptToSHA(String info) {
byte[] digesta = null;
try {
// å¾å°ä¸ä¸ªSHA-1çæ¶æ¯æè¦
MessageDigest alga = MessageDigest.getInstance("SHA-1");
// æ·»å è¦è¿è¡è®¡ç®æè¦çä¿¡æ¯
alga.update(info.getBytes());
// å¾å°è¯¥æè¦
digesta = alga.digest();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
// å°æè¦è½¬ä¸ºå符串
String rs = byte2hex(digesta);
return rs;
}
// //////////////////////////////////////////////////////////////////////////
/**
* å建å¯å
*
* @param algorithm
* å å¯ç®æ³,å¯ç¨ DES,DESede,Blowfish
* @return SecretKey ç§å¯ï¼å¯¹ç§°ï¼å¯é¥
*/
public SecretKey createSecretKey(String algorithm) {
// 声æKeyGenerator对象
KeyGenerator keygen;
// 声æ å¯é¥å¯¹è±¡
SecretKey deskey = null;
try {
// è¿åçææå®ç®æ³çç§å¯å¯é¥ç KeyGenerator 对象
keygen = KeyGenerator.getInstance(algorithm);
// çæä¸ä¸ªå¯é¥
deskey = keygen.generateKey();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
// è¿åå¯å
return deskey;
}
/**
* æ ¹æ®å¯åè¿è¡DESå å¯
*
* @param key
* å¯å
* @param info
* è¦å å¯çä¿¡æ¯
* @return String å å¯åçä¿¡æ¯
*/
public String encryptToDES(SecretKey key, String info) {
// å®ä¹ å å¯ç®æ³,å¯ç¨ DES,DESede,Blowfish
String Algorithm = "DES";
// å å¯éæºæ°çæå¨ (RNG),(å¯ä»¥ä¸å)
SecureRandom sr = new SecureRandom();
// å®ä¹è¦çæçå¯æ
byte[] cipherByte = null;
try {
// å¾å°å å¯/解å¯å¨
Cipher c1 = Cipher.getInstance(Algorithm);
// ç¨æå®çå¯é¥å模å¼åå§åCipher对象
// åæ°:(ENCRYPT_MODE, DECRYPT_MODE, WRAP_MODE,UNWRAP_MODE)
c1.init(Cipher.ENCRYPT_MODE, key, sr);
// 对è¦å å¯çå
容è¿è¡ç¼ç å¤ç,
cipherByte = c1.doFinal(info.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
// è¿åå¯æçåå
è¿å¶å½¢å¼
return byte2hex(cipherByte);
}
/**
* æ ¹æ®å¯åè¿è¡DES解å¯
*
* @param key
* å¯å
* @param sInfo
* è¦è§£å¯çå¯æ
* @return String è¿å解å¯åä¿¡æ¯
*/
public String decryptByDES(SecretKey key, String sInfo) {
// å®ä¹ å å¯ç®æ³,
String Algorithm = "DES";
// å å¯éæºæ°çæå¨ (RNG)
SecureRandom sr = new SecureRandom();
byte[] cipherByte = null;
try {
// å¾å°å å¯/解å¯å¨
Cipher c1 = Cipher.getInstance(Algorithm);
// ç¨æå®çå¯é¥å模å¼åå§åCipher对象
c1.init(Cipher.DECRYPT_MODE, key, sr);
// 对è¦è§£å¯çå
容è¿è¡ç¼ç å¤ç
cipherByte = c1.doFinal(hex2byte(sInfo));
} catch (Exception e) {
e.printStackTrace();
}
// return byte2hex(cipherByte);
return new String(cipherByte);
}
// /////////////////////////////////////////////////////////////////////////////
/**
* å建å¯åç»ï¼å¹¶å°å
¬åï¼ç§åæ¾å
¥å°æå®æ件ä¸
*
* é»è®¤æ¾å
¥mykeys.batæ件ä¸
*/
public void createPairKey() {
try {
// æ ¹æ®ç¹å®çç®æ³ä¸ä¸ªå¯é¥å¯¹çæå¨
KeyPairGenerator keygen = KeyPairGenerator.getInstance("DSA");
// å å¯éæºæ°çæå¨ (RNG)
SecureRandom random = new SecureRandom();
// éæ°è®¾ç½®æ¤éæºå¯¹è±¡çç§å
random.setSeed(1000);
// 使ç¨ç»å®çéæºæºï¼åé»è®¤çåæ°éåï¼åå§åç¡®å®å¯é¥å¤§å°çå¯é¥å¯¹çæå¨
keygen.initialize(512, random);// keygen.initialize(512);
// çæå¯é¥ç»
KeyPair keys = keygen.generateKeyPair();
// å¾å°å
Œ
PublicKey pubkey = keys.getPublic();
// å¾å°ç§å
PrivateKey prikey = keys.getPrivate();
// å°å
¬åç§ååå
¥å°æ件å½ä¸
doObjToFile("mykeys.bat", new Object[] { prikey, pubkey });
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
/**
* å©ç¨ç§å对信æ¯è¿è¡ç¾å æç¾ååçä¿¡æ¯æ¾å
¥å°æå®çæ件ä¸
*
* @param info
* è¦ç¾åçä¿¡æ¯
* @param signfile
* åå
¥çæ件
*/
public void signToInfo(String info, String signfile) {
// ä»æ件å½ä¸è¯»åç§å
PrivateKey myprikey = (PrivateKey) getObjFromFile("mykeys.bat", 1);
// ä»æ件ä¸è¯»åå
Œ
PublicKey mypubkey = (PublicKey) getObjFromFile("mykeys.bat", 2);
try {
// Signature 对象å¯ç¨æ¥çæåéªè¯æ°åç¾å
Signature signet = Signature.getInstance("DSA");
// åå§åç¾ç½²ç¾åçç§é¥
signet.initSign(myprikey);
// æ´æ°è¦ç±åèç¾åæéªè¯çæ°æ®
signet.update(info.getBytes());
// ç¾ç½²æéªè¯æææ´æ°åèçç¾åï¼è¿åç¾å
byte[] signed = signet.sign();
// å°æ°åç¾å,å
¬å,ä¿¡æ¯æ¾å
¥æ件ä¸
doObjToFile(signfile, new Object[] { signed, mypubkey, info });
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 读åæ°åç¾åæ件 æ ¹æ®å
¬åï¼ç¾åï¼ä¿¡æ¯éªè¯ä¿¡æ¯çåæ³æ§
*
* @return true éªè¯æå false éªè¯å¤±è´¥
*/
public boolean validateSign(String signfile) {
// 读åå
Œ
PublicKey mypubkey = (PublicKey) getObjFromFile(signfile, 2);
// 读åç¾å
byte[] signed = (byte[]) getObjFromFile(signfile, 1);
// 读åä¿¡æ¯
String info = (String) getObjFromFile(signfile, 3);
try {
// åå§ä¸ä¸ªSignature对象,并ç¨å
¬é¥åç¾åè¿è¡éªè¯
Signature signetcheck = Signature.getInstance("DSA");
// åå§åéªè¯ç¾åçå
¬é¥
signetcheck.initVerify(mypubkey);
// 使ç¨æå®ç byte æ°ç»æ´æ°è¦ç¾åæéªè¯çæ°æ®
signetcheck.update(info.getBytes());
System.out.println(info);
// éªè¯ä¼ å
¥çç¾å
return signetcheck.verify(signed);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* å°äºè¿å¶è½¬å为16è¿å¶å符串
*
* @param b
* äºè¿å¶åèæ°ç»
* @return String
*/
public String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1) {
hs = hs + "0" + stmp;
} else {
hs = hs + stmp;
}
}
return hs.toUpperCase();
}
/**
* åå
è¿å¶å符串转å为2è¿å¶
*
* @param hex
* @return
*/
public byte[] hex2byte(String hex) {
byte[] ret = new byte[8];
byte[] tmp = hex.getBytes();
for (int i = 0; i < 8; i++) {
ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
}
return ret;
}
/**
* å°ä¸¤ä¸ªASCIIå符åæä¸ä¸ªåèï¼ å¦ï¼"EF"--> 0xEF
*
* @param src0
* byte
* @param src1
* byte
* @return byte
*/
public static byte uniteBytes(byte src0, byte src1) {
byte _b0 = Byte.decode("0x" + new String(new byte[] { src0 }))
.byteValue();
_b0 = (byte) (_b0 << 4);
byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 }))
.byteValue();
byte ret = (byte) (_b0 ^ _b1);
return ret;
}
/**
* å°æå®ç对象åå
¥æå®çæ件
*
* @param file
* æå®åå
¥çæ件
* @param objs
* è¦åå
¥ç对象
*/
public void doObjToFile(String file, Object[] objs) {
ObjectOutputStream oos = null;
try {
FileOutputStream fos = new FileOutputStream(file);
oos = new ObjectOutputStream(fos);
for (int i = 0; i < objs.length; i++) {
oos.writeObject(objs[i]);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* è¿åå¨æ件ä¸æå®ä½ç½®ç对象
*
* @param file
* æå®çæ件
* @param i
* ä»1å¼å§
* @return
*/
public Object getObjFromFile(String file, int i) {
ObjectInputStream ois = null;
Object obj = null;
try {
FileInputStream fis = new FileInputStream(file);
ois = new ObjectInputStream(fis);
for (int j = 0; j < i; j++) {
obj = ois.readObject();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return obj;
}
/**
* æµè¯
*
* @param args
*/
public static void main(String[] args) {
CryptTest jiami = new CryptTest();
// æ§è¡MD5å å¯"Hello world!"
System.out.println("Helloç»è¿MD5:" + jiami.encryptToMD5("Hello"));
// çæä¸ä¸ªDESç®æ³çå¯å
SecretKey key = jiami.createSecretKey("DES");
// ç¨å¯åå å¯ä¿¡æ¯"Hello world!"
String str1 = jiami.encryptToDES(key, "Hello");
System.out.println("使ç¨deså å¯ä¿¡æ¯Hello为:" + str1);
// 使ç¨è¿ä¸ªå¯å解å¯
String str2 = jiami.decryptByDES(key, str1);
System.out.println("解å¯å为ï¼" + str2);
// å建å
¬ååç§å
jiami.createPairKey();
// 对Hello world!使ç¨ç§åè¿è¡ç¾å
jiami.signToInfo("Hello", "mysign.bat");
// å©ç¨å
¬å对ç¾åè¿è¡éªè¯ã
if (jiami.validateSign("mysign.bat")) {
System.out.println("Success!");
} else {
System.out.println("Fail!");
}
}
}