📄 des_cbc.java
字号:
package des_cbc;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.*;
import sun.misc.*;
public class DES_CBC {
// The initialization vector should be 8 bytes
static private final byte[] EncryptionIV = { 0x11, 0x22, 0x4F, 0x58, (byte) 0x88,
0x10, 0x40, 0x38};
private final static String DES = "DES/CBC/PKCS5Padding";
static private String EncryptionString;
public DES_CBC(String EncryptionString) {
this.EncryptionString = EncryptionString;
}
public byte[] EncryptionByteData(byte[] SourceData) throws Exception {
//String EncryptionString="Hello123Hello123Hello123Hello123";
byte[] retByte = null;
//Create SecretKey object
byte[] EncryptionByte = EncryptionString.getBytes();
DESKeySpec dks = new DESKeySpec(EncryptionByte);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(dks);
// Create IvParameterSpec object with initialization vector
IvParameterSpec spec = new IvParameterSpec(EncryptionIV);
// Create Cipter object
Cipher cipher = Cipher.getInstance(DES);
// Initialize Cipher object
cipher.init(Cipher.ENCRYPT_MODE, securekey, spec);
// Encrypting data
retByte = cipher.doFinal(SourceData);
return retByte;
}
public String EncryptionStringData(String SourceData) throws Exception {
String retStr = null;
byte[] retByte = null;
// Transform SourceData to byte array
byte[] sorData = SourceData.getBytes();
// Encrypte data
retByte = EncryptionByteData(sorData);
// Encode encryption data
BASE64Encoder be = new BASE64Encoder();
retStr = be.encode(retByte);
return retStr;
}
public static void main(String[] args) throws Exception {
DES_CBC c = new DES_CBC("12345678"); //This is the key
System.out.println(c.EncryptionStringData("13764528738fefqawe"));//The source data
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -