des加密算法java代码
1、DES加密算法Java实现
(1)导入所需要的包:
pto.Cipher;
KA卖场
pto.SecretKey;
pto.SecretKeyFactory;
物流的定义 pto.spec.DESKeySpec;
(2)定义加密算法:
public class DesEncrypter {
Cipher ecipher;
Cipher dcipher;
public DesEncrypter(String passPhra) {
try {
// Create the key
DESKeySpec keySpec = new Bytes('UTF8'));
SecretKeyFactory keyFactory = Instance('DES');
SecretKey key = ateSecret(keySpec);
新疆吐鲁番动漫
// Create the cipher
ecipher = Instance('DES');
dcipher = Instance('DES');
// Initialize the cipher for encryption
ecipher.init(Cipher.ENCRYPT_MODE, key);
// Initialize the same cipher for decryption
dcipher.init(Cipher.DECRYPT_MODE, key);
} catch (Exception e) {
}
}
(3)定义加密函数:
public String encrypt(String str) {
try {
// Encode the string into bytes using utf-8
byte[] utf8 = Bytes('UTF8');
// Encrypt
byte[] enc = ecipher.doFinal(utf8);
// Encode bytes to ba64 to get a string
return new sun.misc.BASE64Encoder().encode(enc);
} catch (Exception e) {
}联贯
return null;
}
(4)定义解密函数:
// Decrypt
public String decrypt(String str) {
try {
// Decode ba64 to get bytes
byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
// Decrypt
byte[] utf8 = dcipher.doFinal(dec);
// Decode using utf-8
return new String(utf8, 'UTF8');
} catch (Exception e) {
}
return null;
}
}
(5)定义测试类,测试加密解之密是否正确:
public class TestDes {
悼念刚离去的亲人句子
public static void main(String[] args) {
try {
// Create encrypter/decrypter class
举足轻重
DesEncrypter encrypter = new DesEncrypter('your key');
// Encrypt
String encrypted = pt('Don't tell anybody!');
// Decrypt
八条宫智仁亲王 String decrypted = encrypter.decrypt(encrypted);
// Print out values
System.out.println('Encrypted: ' + encrypted);
System.out.println('Decrypted: ' + decrypted);
} catch (Exception e) {
}
}
}。
>十二月份的英语