자바 대칭 암호 화(DES)
/**
* DESHelper.java
* cn.com.songjy.test
*
* Function: TODO
*
* ver date author
* ──────────────────────────────────
* 2012-6-29 songjianyong
*
* Copyright (c) 2012, TNT All Rights Reserved.
*/
package cn.com.songjy.test;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* ClassName:DESHelper
*
* @author songjianyong
* @version 1.0
* @since v1.0
* @Date 2012-6-29 1:36:05
*/
public class DESHelper {
/**
* @method main
* @param args
* @throws
* @since v1.0
*/
public static void main(String[] args) {
try {
//
String str = "mobile:13757352769|type:w|content: ";
// ( )
String password = "01010101";
String desc = DESHelper.DoDES(str, password, 0);
System.out.println(" :" + desc);
//
str = DESHelper.DoDES(desc, password, 1);
System.out.println(" :" + str);
} catch (Exception e1) {
e1.printStackTrace();
}
}
/**
*
* @method DoDES
* @param data
*
* @param password
* 8
* @param flag
* 0: ,1:
* @return
* @throws
* @since v1.0
*/
public static String DoDES(String data, String password, int flag) {
try {
SecureRandom random = new SecureRandom();
DESKeySpec desKey = new DESKeySpec(password.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(desKey);
Cipher cipher = Cipher.getInstance("DES");
if (flag == 0) {
BASE64Encoder base64encoder = new BASE64Encoder();
cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
return base64encoder.encode(cipher.doFinal(data
.getBytes("UTF-8")));
} else {
BASE64Decoder base64decoder = new BASE64Decoder();
byte[] encodeByte = base64decoder.decodeBuffer(data);
cipher.init(Cipher.DECRYPT_MODE, securekey, random);
byte[] decoder = cipher.doFinal(encodeByte);
return new String(decoder, "UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
테스트 효과:
비밀문서:r0N21ttXuQY3yAsf2S+5M16WSltXCx2B9U4e53su+Q08Ep5D1Wgcla1qiLT6N4M 9
명문:mobile:13757352769|type:w|content:테스트
인용:
http://www.linuxidc.com/Linux/2011-04/34990.htm
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 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에 따라 라이센스가 부여됩니다.