📄 encrypt.java~67~
字号:
package chat;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;
public class Encrypt {
final String Algorithm = "DES/CBC/PKCS5Padding";
String myinfo = "要加密的信息,a";
public String jiaMi(String info){
String jiaMiInfo=null;
try{
byte[] bytes = {
'e', '+', 's', 'u', '%', '*', '4', 'O'};
byte[] key = new String("de$%^@#-").getBytes("UTF8");
SecretKey deskey = new SecretKeySpec(key, "DES");
Cipher c1 = Cipher.getInstance(Algorithm);
IvParameterSpec ivSpec = new IvParameterSpec(bytes);
c1.init(Cipher.ENCRYPT_MODE, deskey, ivSpec);
byte[] cipherByte = c1.doFinal(info.getBytes("UTF8"));
System.out.println(cipherByte);
jiaMiInfo=cipherByte.toString();
}catch(Exception e){
}
return jiaMiInfo;
}
public String jieMi(byte[] info){
String jieMiInfo=null;
try{
byte[] bytes = {
'e', '+', 's', 'u', '%', '*', '4', 'O'};
byte[] key = new String("de$%^@#-").getBytes("UTF8");
SecretKey deskey = new SecretKeySpec(key, "DES");
Cipher c1 = Cipher.getInstance(Algorithm);
IvParameterSpec ivSpec = new IvParameterSpec(bytes);
//byte[] cipherByte = c1.doFinal(info.getBytes());
c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.DECRYPT_MODE, deskey, ivSpec);
byte[] clearByte = c1.doFinal(info);
jieMiInfo= new String(clearByte);
}catch(Exception e){
e.printStackTrace();
}
return jieMiInfo;
}
public void run() {
//添加新安全算法,如果用JCE就要把它添加进去
//Security.addProvider(new com.sun.crypto.provider.SunJCE());
//final String Algorithm="DES"; //定义加密算法,可用 DES,DESede,Blowfish
final String Algorithm = "DES/CBC/PKCS5Padding";
String myinfo = "要加密的信息";
try {
//生成密钥
byte[] bytes = {
'e', '+', 's', 'u', '%', '*', '4', 'O'};
byte[] key = new String("de$%^@#-").getBytes();
SecretKey deskey = new SecretKeySpec(key, "DES");
Cipher c1 = Cipher.getInstance(Algorithm);
IvParameterSpec ivSpec = new IvParameterSpec(bytes);
c1.init(Cipher.ENCRYPT_MODE, deskey, ivSpec);
//KeyGenerator keygen = KeyGenerator.getInstance(Algorithm);
//SecretKey deskey = keygen.generateKey();
//加密
System.out.println("加密前的二进串:" + byte2hex(myinfo.getBytes()));
System.out.println("加密前的信息:" + myinfo);
//Cipher c1 = Cipher.getInstance(Algorithm);
//c1.init(Cipher.ENCRYPT_MODE,deskey);
byte[] cipherByte = c1.doFinal(myinfo.getBytes());
System.out.println("加密后的二进串:" + byte2hex(cipherByte));
//解密
c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.DECRYPT_MODE, deskey, ivSpec);
byte[] clearByte = c1.doFinal(cipherByte);
System.out.println("解密密钥:" + (new String(deskey.getEncoded())));
System.out.println("解密后的二进串:" + byte2hex(clearByte));
System.out.println("解密后的信息:" + (new String(clearByte)));
}
catch (java.security.NoSuchAlgorithmException e1) {
e1.printStackTrace();
}
catch (javax.crypto.NoSuchPaddingException e2) {
e2.printStackTrace();
}
catch (java.lang.Exception e3) {
e3.printStackTrace();
}
}
public String byte2hex(byte[] b) { //二行制转字符串
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1) hs = hs + "0" + stmp;
else hs = hs + stmp;
if (n < b.length - 1) hs = hs + ":";
}
return hs.toUpperCase();
}
public static void main(String[] args) {
Encrypt my = new Encrypt();
// my.run();
// try {
String s=my.jiaMi("要加密的信息");
// my.jieMi(s);
//byte[] b=new String(s).getBytes();
System.out.println(s.getBytes());
//System.out.println(s);
//try {
// my.jieMi(s.getBytes("UTF8"));
//}
//catch (UnsupportedEncodingException ex) {
//}
// System.out.println(my.jieMi( );
//}
// catch (UnsupportedEncodingException ex) {
// }
// my.jieMI("[B@148aa23".getBytes());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -