【文件属性】:
文件名称:DES加密算法DES加密算法
文件大小:3KB
文件格式:JAVA
更新时间:2012-04-03 18:01:18
encryption
// 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, world!");
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
网友评论
- 不是我想要的 但是也感谢分享
- 这个JAVA 的不是太会调试。