자바 알 기 쉬 운 MD5 암호 화(직접 실행 가능)(1)1/2 페이지

3136 단어 자바MD5 암호 화
1.md5 암호 화,이 암호 화 알고리즘 은 단 방향 암호 화,즉 암호 화 된 데 이 터 는 복호화 로 복원 할 수 없습니다.관련 클래스 는 java.security.Message Digest 패키지 에 포 함 됩 니 다.2,3-DES 암호 화,이 암호 화 알고리즘 은 역 효 과 를 가 집 니 다.복호화 자 는 암호 화 자 와 약 속 된 비밀 열 쇠 를 통 해 복호화 할 수 있 습 니 다.관련 클래스 는 javax.crypto.*패키지 에 포함 되 어 있 습 니 다.3.base 64 인 코딩 은 8bit 바이트 코드 를 전송 하 는 데 가장 자주 사용 되 는 인 코딩 방식 입 니 다.관련 종 류 는 sun.misc.BASE 64 Decoder 와 sun.misc.BASE 64 Encoder 에 있 습 니 다.4.URLEncoder 인 코딩 은 문자 인 코딩 으로 전 송 된 매개 변 수 는 규범 에 따 른 텍스트 로 구성 되도록 합 니 다.관련 클래스 는 java.net.URLEncoder 패키지 에 있 습 니 다.세부 사항:1.MD5 암호 화 를 진행 하여 byte[]획득 

/**
* MD5
* @param String SPKEY
* @return byte[] md5 byte[]
*/
private byte[] md5(String strSrc)
{
byte[] returnByte = null;
try
{
MessageDigest md5 = MessageDigest.getInstance("MD5");
returnByte = md5.digest(strSrc.getBytes("GBK"));
}
catch(Exception e)
{
e.printStackTrace();
}
return returnByte;
}
2,3-DES 의 비밀 열 쇠 를 획득 

/**
* 3-DES
* , 24 ,md5 16 , 8 0
* @param String SPKEY
* @return byte[] md5 byte[]
*/
private byte[] getEnKey(String spKey)
{
byte[] desKey=null;
try
{
byte[] desKey1 = md5(spKey);
desKey = new byte[24];
int i = 0;
while (i < desKey1.length && i < 24) {
desKey[i] = desKey1[i];
i++;
}
if (i < 24) {
desKey[i] = 0;
i++;
}
}
catch(Exception e){
e.printStackTrace();
}
return desKey;
}
3,3-DES 암호 화

/**
* 3-DES
* @param byte[] src 3-DES byte[]
* @param byte[] enKey 3-DES
* @return byte[] 3-DES byte[]
*/
public byte[] Encrypt(byte[] src,byte[] enKey)
{
byte[] encryptedData = null;
try
{
DESedeKeySpec dks = new DESedeKeySpec(enKey);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
SecretKey key = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.ENCRYPT_MODE, key);
encryptedData = cipher.doFinal(src);
}
catch(Exception e)
{
e.printStackTrace();
}
return encryptedData;
}
4.문자열 에 대해 Base 64 인 코딩

/**
* Base64
* @param byte[] src
*
* @return String
*/
public String getBase64Encode(byte[] src)
{
String requestValue="";
try{
BASE64Encoder base64en = new BASE64Encoder();
requestValue=base64en.encode(src);
//System.out.println(requestValue);
}
catch(Exception e){
e.printStackTrace();
}

return requestValue;
}
1 2 다음 페이지 전문 을 읽다

좋은 웹페이지 즐겨찾기