您好,欢迎光临本网站![请登录][注册会员]  
文件名称: DES加密算法DES加密算法
  所属分类: Java
  开发工具:
  文件大小: 3kb
  下载次数: 0
  上传时间: 2009-03-15
  提 供 者: yur***
 详细说明: // Java crypt example SJ import javax.crypto.*; import javax.crypto.spec.*; import java.io.*; import java.lang.*; import java.util.*; import java.security.*; public class EncryptionExample { protected String calg = "Blowfish"; // AES. DES, Blowfish protected int keyLen = 128; // 128 for AES, Blowfish, 64 for DES public static void main(String[] args) { EncryptionExample s = new EncryptionExample(); // to call nonstatic methods SecretKeySpec key = s.readkey(); String mess = new String("Hello, worl d!"); byte[] messb = mess.getBytes(); System.out.println("Plain|" + mess +"|=|" + s.bintohex(messb)); byte[] ct = s.encrypt(messb, key); System.out.println("Encry:" + s.bintohex(ct)); byte[] pt = s.decrypt(ct, key); String dmess = new String(pt); System.out.println("Decry|" + dmess +"|=|" + s.bintohex(pt)); } // main() // encrypt message t with key k public byte[] encrypt(byte[] t, SecretKeySpec k) { try { Cipher c = Cipher.getInstance(calg); c.init(Cipher.ENCRYPT_MODE, k); return c.doFinal(t); } catch (Exception e) { System.err.println("Encryption failed: " + e); } return null; } // decrypt message t with key k public byte[] decrypt(byte[] t, SecretKeySpec k) { try { Cipher c = Cipher.getInstance(calg); c.init(Cipher.DECRYPT_MODE, k); return c.doFinal(t); } catch (Exception e) { System.err.println("Decryption failed: " + e); } return null; } // reads key string from user, returns SecretKeySpec public SecretKeySpec readkey() { SecretKeySpec kp = null; String line; byte [] bin = null; try { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Give the key or passphrase (" + keyLen/4 + " hex digits or long ascii string) \n : "); line = in.readLine(); // check if input is all hex or not boolean ishex = true; for (int i = 0; i < line.length(); i++) if (Character.digit(line.charAt(i), 16) < 0) { ishex = false; break; } // check hex key length if (ishex && line.length() != keyLen/4) System.err.println("Wrong hex ley lenght (" + line.length() + "/" + keyLen/4 + ")"); // make binary key if (ishex) bin = hextobin(line); else bin = asciitobin(line); // make key for crypto algorithm kp = new SecretKeySpec(bin, calg); System.out.println("Key = |" + bintohex(kp.getEncoded()) + "|"); } catch (Exception e) { System.err.println("Key generation failed" + e); } return kp; } // readkey() // make binary out of hex string public byte[] hextobin(String s) { int len = (s.length()+1)/2; byte[] A = new byte[len]; for (int i = 0; i < len; i++) A[i] = Integer.valueOf(s.substring(i*2, i*2+2), 16).byteValue(); return A; } // returns new 128 bit key using MD5 of the string s public byte[] asciitobin(String s) { byte[] A = null; try { MessageDigest md = MessageDigest.getInstance("MD5"); A = md.digest(s.getBytes()); } catch (Exception e) { System.err.println("Digest failed" + e); } return A; } // returns new hex string representation of A public String bintohex(byte[] A) { int len = A.length; StringBuffer sb = new StringBuffer(len*2); for (int i = 0; i < len; i++) { if ((A[i] & 0xFF) < 0x10) sb.append("0"); sb.append(Integer.toHexString(A[i] & 0xFF)); } return sb.toString(); } } // class ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索: encryption
 输入关键字,在本站1000多万海量源码库中尽情搜索: