설명은 하지 않습니다.
그냥 복제해서 사용하시면 됩니다.
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 {
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 {
MessageDigest alga = MessageDigest.getInstance("SHA-1");
alga.update(info.getBytes());
digesta = alga.digest();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
String rs = byte2hex(digesta);
return rs;
}
// //////////////////////////////////////////////////////////////////////////
/**
* SecretKey생성
*
* @param algorithm
* 암호화 DES,DESede,Blowfish
* @return SecretKey
*/
public SecretKey createSecretKey(String algorithm) {
KeyGenerator keygen;
SecretKey deskey = null;
try {
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";
SecureRandom sr = new SecureRandom();
byte[] cipherByte = null;
try {
Cipher c1 = Cipher.getInstance(Algorithm);
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";
SecureRandom sr = new SecureRandom();
byte[] cipherByte = null;
try {
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.DECRYPT_MODE, key, sr);
cipherByte = c1.doFinal(hex2byte(sInfo));
} catch (Exception e) {
e.printStackTrace();
}
// return byte2hex(cipherByte);
return new String(cipherByte);
}
// /////////////////////////////////////////////////////////////////////////////
/**
* 키를 생성화여파일에 저장한다
*
*
*/
public void createPairKey() {
try {
KeyPairGenerator keygen = KeyPairGenerator.getInstance("DSA");
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 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();
}
}
/**
* key,signe,info의 합법성
*
* @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 signetcheck = Signature.getInstance("DSA");
signetcheck.initVerify(mypubkey);
signetcheck.update(info.getBytes());
System.out.println(info);
return signetcheck.verify(signed);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* byte를 hex로 전환
*
* @param b
* 2bit 데이터
* @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();
}
/**
* hex를 byte로 전환
*
* @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;
} }
사용:
CryptTest jiami = new CryptTest();
//SecretKey key = jiami.createSecretKey("DES");
byte[] data = {97, 87, 93, -50, -57, -65, -53, 104};
SecretKey key = new SecretKeySpec(data, 0, data.length, "DES");
String str1 = jiami.encryptToDES(key, "Hello");
System.out.println("des암호화 String : "+ str1);
String str2 = jiami.decryptByDES(key, str1);
System.out.println("복고화 String :" + str2);
제목 | 글쓴이 | 날짜 | 조회 |
---|---|---|---|
2003-09-20 |
11647 |
||
2003-09-20 |
11198 |
||
2003-09-20 |
20237 |
||
지구인 |
2010-08-27 |
19086 |
|
지구인 |
2009-09-07 |
13381 |
|
2010-01-29 |
15399 |
||
2004-10-07 |
16506 |
||
2014-01-11 |
2435 |
||
2014-01-07 |
2592 |
||
2014-01-07 |
2377 |
||
2014-01-07 |
2046 |
||
2960 [공개소스] 그누보드 4.36.27 |
2014-01-07 |
1787 |
|
2959 [공개소스] 그누보드 4.36.26 |
2014-01-07 |
1609 |
|
2958 [공개소스] 그누보드 4.36.25 |
2014-01-07 |
1174 |
|
즐거운개굴 |
2013-09-29 |
3000 |
|
2013-09-03 |
4907 |
||
2955 [웹 프로그래밍] ajax 특수문자 |
2013-08-29 |
5360 |
|
2013-08-26 |
4791 |
||
2013-08-25 |
3633 |
||
2013-08-24 |
4161 |
||
2013-08-23 |
4319 |
||
2013-08-23 |
3591 |
||
2013-08-22 |
1885 |
||
2948 [웹 프로그래밍] log4j |
2013-08-10 |
1621 |
|
2013-08-05 |
2147 |
||
2946 [웹 프로그래밍] Spring 기초 |
2013-08-05 |
2088 |
|
2013-07-30 |
1461 |
||
2013-07-23 |
1940 |