DES对称分组密码系统的Java实现
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<ct.length;i++) System.out.print(ct[i]+" "); print(""); for(int i=0;i<ct.length;i++) ct[i]=(byte)(ct[i]&0xFF); String ciphertxt=new String(ct); print("Cipher Text :"+ciphertxt); byte[] ctcode = ciphertxt.getBytes(); //Decripting byte[] pt=this.decripting(ctcode,0); String ptt=new String(pt); print("Plaintext after decripting:"); print(ptt); }catch(Exception ex) { return; } } public static byte[] hex2Bytes(String str) { if (str==null) { return null; } else if (str.length() < 2) { return null; } else { int len = str.length() / 2; byte[] buffer = new byte[len]; for (int i=0; i<len; i++) { buffer[i] = (byte) Integer.parseInt( str.substring(i*2,i*2+2),16); } return buffer; } } public static String bytes2Hex(byte[] data) { if (data==null) { return null; } else { int len = data.length; String str = ""; for (int i=0; i<len; i++) { if ((data[i]&0xFF)<16) str = str + "0" + java.lang.Integer.toHexString(data[i]&0xFF); else str = str + java.lang.Integer.toHexString(data[i]&0xFF); } return str.toUpperCase(); } } }
- 粉丝: 0
- 资源: 3
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- RTL8188FU-Linux-v5.7.4.2-36687.20200602.tar(20765).gz
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
- SPC统计方法基础知识.pptx
评论3