Java最简单的DES加密算法实现案例
Base64.java
packagecom.mstf.des;
importjava.io.UnsupportedEncodingException;
/**
*base64编码/解码
*@authorceet
*
*/
publicclassBase64{
publicstaticStringencode(Stringdata){
returnnewString(encode(data.getBytes()));
}
publicstaticStringdecode(Stringdata){
try{
returnnewString(decode(data.toCharArray()),"utf-8");
}catch(UnsupportedEncodingExceptione){
e.printStackTrace();
returnnull;
}
}
privatestaticchar[]alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
.toCharArray();
privatestaticbyte[]codes=newbyte[256];
static{
for(inti=0;i<256;i++){
codes[i]=-1;
}
for(inti='A';i<='Z';i++){
codes[i]=(byte)(i-'A');
}
for(inti='a';i<='z';i++){
codes[i]=(byte)(26+i-'a');
}
for(inti='0';i<='9';i++){
codes[i]=(byte)(52+i-'0');
}
codes['+']=62;
codes['/']=63;
}
publicstaticchar[]encode(byte[]data){
char[]out=newchar[((data.length+2)/3)*4];
for(inti=0,index=0;i>=6;
out[index+2]=alphabet[(trip?(val&0x3F):64)];
val>>=6;
out[index+1]=alphabet[val&0x3F];
val>>=6;
out[index+0]=alphabet[val&0x3F];
}
returnout;
}
publicstaticbyte[]decode(char[]data){
inttempLen=data.length;
for(intix=0;ix255)||codes[data[ix]]<0){
--tempLen;
}
}
intlen=(tempLen/4)*3;
if((tempLen%4)==3){
len+=2;
}
if((tempLen%4)==2){
len+=1;
}
byte[]out=newbyte[len];
intshift=0;
intaccum=0;
intindex=0;
for(intix=0;ix255)?-1:codes[data[ix]];
if(value>=0){
accum<<=6;
shift+=6;
accum|=value;
if(shift>=8){
shift-=8;
out[index++]=(byte)((accum>>shift)&0xff);
}
}
}
if(index!=out.length){
thrownewError("Miscalculateddatalength(wrote"+index
+"insteadof"+out.length+")");
}
returnout;
}
}
DESUtil.java
packagecom.mstf.des;
importjava.security.Key;
importjava.security.SecureRandom;
importjavax.crypto.Cipher;
importjavax.crypto.KeyGenerator;
/**
*DES对称算法(加密/解密)
*
*@authorceet
*
*/
publicclassDESUtil{
privateKeykey;
publicDESUtil(StringstrKey){
setKey(strKey);
}
publicvoidsetKey(StringstrKey){
try{
KeyGeneratorgenerator=KeyGenerator.getInstance("DES");
generator.init(newSecureRandom(strKey.getBytes()));//根据参数生成key
this.key=generator.generateKey();
}catch(Exceptione){
e.printStackTrace();
}
}
publicStringencrypt(Stringsource){
returnencrypt(source,"utf-8");
}
publicStringdecrypt(StringencryptedData){
returndecrypt(encryptedData,"utf-8");
}
publicStringencrypt(Stringsource,StringcharSet){
Stringencrypt=null;
try{
byte[]ret=encrypt(source.getBytes(charSet));
encrypt=newString(Base64.encode(ret));
}catch(Exceptione){
e.printStackTrace();
encrypt=null;
}
returnencrypt;
}
publicStringdecrypt(StringencryptedData,StringcharSet){
StringdescryptedData=null;
try{
byte[]ret=descrypt(Base64.decode(encryptedData.toCharArray()));
descryptedData=newString(ret,charSet);
}catch(Exceptione){
e.printStackTrace();
descryptedData=null;
}
returndescryptedData;
}
privatebyte[]encrypt(byte[]primaryData){
try{
Ciphercipher=Cipher.getInstance("DES");//Cipher对象实际完成加密操作
cipher.init(Cipher.ENCRYPT_MODE,this.key);//用密钥初始化Cipher对象(加密)
returncipher.doFinal(primaryData);
}catch(Exceptione){
e.printStackTrace();
returnnull;
}
}
privatebyte[]descrypt(byte[]encryptedData){
try{
Ciphercipher=Cipher.getInstance("DES");//Cipher对象实际完成解密操作
cipher.init(Cipher.DECRYPT_MODE,this.key);//用密钥初始化Cipher对象(解密)
returncipher.doFinal(encryptedData);
}catch(Exceptione){
e.printStackTrace();
returnnull;
}
}
publicstaticvoidmain(String[]args){
Stringcode="ceet";
DESUtildesUtil=newDESUtil("key");
Stringencrypt=desUtil.encrypt(code);
Stringdecrypt=desUtil.decrypt(encrypt);
System.out.println("原内容:"+code);
System.out.println("加密:"+encrypt);
System.out.println("解密:"+decrypt);
}
}
以上这篇Java最简单的DES加密算法实现案例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。