文件名称:DES对称分组密码系统的Java实现
文件大小:662KB
文件格式:DOC
更新时间:2013-07-03 05:06:16
DES对称分组密码系统
DES对称分组密码系统
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
class DES01 {
private String strkey;
private SecretKey skey=null;
private String[] algo=
{"DES/ECB/PKCS5Padding","DES/ECB/NoPadding","DES"};
public DES01(String key)
{
strkey=key;
}
public void keyGenerating() throws Exception
{
byte[] bkey=strkey.getBytes();
KeySpec ks = new DESKeySpec(bkey);
SecretKeyFactory kf
= SecretKeyFactory.getInstance("DES");
skey = kf.generateSecret(ks);
}
public static void main(String[] a) {
DES01 des = new DES01("IAMASTUDENT");
des.test02("STUDENTWANGFENGLIMING");
}
public byte[] Encripting(String plaintext,int i)
throws Exception
{
byte[] bpt=plaintext.getBytes();
Cipher cf = Cipher.getInstance(algo[i]);
if(skey==null)this.keyGenerating();
cf.init(Cipher.ENCRYPT_MODE,skey);
byte[] bct = cf.doFinal(bpt);
return bct;
}
public byte[] decripting(byte[] bct,int i)
throws Exception
{
Cipher cf = Cipher.getInstance(algo[i]);
if(skey==null)this.keyGenerating();
cf.init(Cipher.DECRYPT_MODE,skey);
byte[] bpt = cf.doFinal(bct);
return bpt;
}
public void test01(String mess)
{
try{
byte[] ct=this.Encripting(mess,0);
byte[] pt=this.Decripting(ct,0);
String ptt=new String(pt);
System.out.println(ptt);
}catch(Exception ex)
{
return;
}
}
public void test02(String mess)
{
try{
//Encripting
print("Plaintext to be encripted:");
print(mess);
byte[] ct=this.Encripting(mess,0);
//Exploiting the results
print("Byte array of cipher:");
for(int i=0;i