Java 3DES 암호 화 복호화 예시
package des3;
import java.io.IOException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
@SuppressWarnings("restriction")
public class TripleDESTest {
/**
* Key;
*
* @param strKey
*/
private Key getKey(String strKey) {
Key key = null;
try {
KeyGenerator _generator = KeyGenerator.getInstance("DES");
_generator.init(new SecureRandom(strKey.getBytes()));
key = _generator.generateKey();
_generator = null;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return key;
}
/**
* 3DES
*
* @param
* @return strMi
*/
public String getEncString(String strMing, String strKey) {
byte[] byteMi = null;
byte[] byteMing = null;
String strMi = "";
Key key = getKey(strKey);
BASE64Encoder encoder = new BASE64Encoder();
try {
byteMing = strMing.getBytes("utf-8");
byteMi = getEncCode(byteMing, key);
strMi = encoder.encode(byteMi);
} catch (Exception e) {
e.printStackTrace();
} finally {
encoder = null;
byteMi = null;
byteMing = null;
}
return strMi;
}
/**
* 3DES
*
* @param
* @return strMi
*/
public String getTwiceEncString(String strMing, String strKey) {
return getEncString(getEncString(strMing, strKey), strKey);
}
/**
* 3DES
*
* @param
* @return strMing
*/
public String getDecString(String strMi, String strKey) {
BASE64Decoder base64Decoder = new BASE64Decoder();
byte[] byteMing = null;
byte[] byteMi = null;
String strMing = "";
Key key = getKey(strKey);
try {
byteMi = base64Decoder.decodeBuffer(strMi);
byteMing = getDecCode(byteMi, key);
strMing = new String(byteMing, "utf-8");
} catch (IOException e) {
e.printStackTrace();
} finally {
base64Decoder = null;
byteMing = null;
byteMi = null;
}
return strMing;
}
/**
* 3DES
*
* @param
* @return strMing
*/
public String getTwiceDecString(String strMi, String strKey) {
return getDecString(getDecString(strMi, strKey), strKey);
}
/**
* 3DES
*
* @param byts
* @return
*/
private byte[] getEncCode(byte[] byts, Key key) {
byte[] byteFina = null;
Cipher cipher;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byteFina = cipher.doFinal(byts);
} catch (Exception e) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
}
/**
* 3DES
*
* @param bytd
* @return
*/
private byte[] getDecCode(byte[] bytd, Key key) {
byte[] byteFina = null;
Cipher cipher = null;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byteFina = cipher.doFinal(bytd);
} catch (Exception e) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
}
public static void main(String[] args) {
TripleDESTest td = new TripleDESTest();
Key k = td.getKey("Key");
System.out.println(" key :" + k);
String encyStr = td.getEncString("Test", "Key");
System.out.println(" :" + encyStr);
String decyStr = td.getDecString(encyStr, "Key");
System.out.println(" :" + decyStr);
encyStr = td.getTwiceEncString("Test", "Key");
System.out.println(" :" + encyStr);
decyStr = td.getTwiceDecString(encyStr, "Key");
System.out.println(" :" + decyStr);
}
}
실행 결과:
획득 한 키 키 키 는 com.sun.crypto.provider 입 니 다.DESKey@fffe7bef한 번 의 암호 화 후의 비밀문 은 dh2ue38w81g=한 번 의 복호화 후의 명문 은 Test 두 번 의 암호 화 후의 비밀문 은:8EBUOcWqIZlpg1n1YaqW+A==두 번 의 복호화 후의 명문 은:Test 이다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.