|
发表于 2013-9-27 17:06:21
|
显示全部楼层
本帖最后由 roger 于 2013-9-27 17:07 编辑
aardio DES加密、解密:
- import crypt.des;
- import crypt.bin;
- import console;
-
- var des = crypt.des()
- des.setPassword("ABCDEFGH");// 设置密码
- str = des.encrypt("abc");// DES加密
- // 密文转换为BASE64文本格式
- base64 = crypt.bin.encodeBase64(str);
- console.log( "密文:",base64 )
- // BASE64解密,DES解密
- str = crypt.bin.decodeBase64(base64);
- console.log( "明文:",des.decrypt(str) );
复制代码 如果需要转为UTF8密码, 用下面的代码转换
str = string.fromto( str,0,65001)
你发的JAVA代码用了随机密码,不同系统可能结果不一致,建议修改如下:
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.FileWriter;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.security.Key;
- import java.security.SecureRandom;
- import javax.crypto.Cipher;
- import javax.crypto.KeyGenerator;
- import javax.crypto.spec.IvParameterSpec;
- import javax.crypto.spec.SecretKeySpec;
- import sun.misc.BASE64Decoder;
- import sun.misc.BASE64Encoder;
- public class Encrypt {
- SecretKeySpec key;
- public Encrypt(String str) {
- setKey(str);// 生成密匙
- }
- public Encrypt() {
- setKey("ABCDEFGH");
- }
- // 根据参数生成KEY
- public void setKey(String strKey) {
- this.key = new SecretKeySpec(strKey.getBytes(), "DES"); ;
- }
- //加密String明文输入,String密文输出
- public String getEncString(String strMing) {
- byte[] byteMi = null;
- byte[] byteMing = null;
- String strMi = "";
- BASE64Encoder base64en = new BASE64Encoder();
- try {
- byteMing = strMing.getBytes("UTF8");
- byteMi = this.getEncCode(byteMing);
- strMi = base64en.encode(byteMi);
- } catch (Exception e) {
- throw new RuntimeException(
- "DES加解密异常: " + e);
- } finally {
- base64en = null;
- byteMing = null;
- byteMi = null;
- }
- return strMi;
- }
- // 解密 以String密文输入,String明文输出
- public String getDesString(String strMi) {
- BASE64Decoder base64De = new BASE64Decoder();
- byte[] byteMing = null;
- byte[] byteMi = null;
- String strMing = "";
- try {
- byteMi = base64De.decodeBuffer(strMi);
- byteMing = this.getDesCode(byteMi);
- strMing = new String(byteMing, "UTF8");
- } catch (Exception e) {
- throw new RuntimeException(
- "DES加解密异常: " + e);
- } finally {
- base64De = null;
- byteMing = null;
- byteMi = null;
- }
- return strMing;
- }
- // 加密以byte[]明文输入,byte[]密文输出
- private static byte[] iv = {0x12,0x34,0x56,0x78,(byte)0x90,(byte)0xAB,(byte)0xCD,(byte)0xEF};
- private byte[] getEncCode(byte[] byteS) {
- IvParameterSpec zeroIv = new IvParameterSpec(iv);
- byte[] byteFina = null;
- Cipher cipher;
- try {
- cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
- cipher.init(Cipher.ENCRYPT_MODE, this.key, zeroIv);
- byteFina = cipher.doFinal(byteS);
- } catch (Exception e) {
- throw new RuntimeException(
- "DES加解密异常: " + e);
- } finally {
- cipher = null;
- }
- return byteFina;
- }
- //解密以byte[]密文输入,以byte[]明文输出
- private byte[] getDesCode(byte[] byteD) {
- IvParameterSpec zeroIv = new IvParameterSpec(iv);
- byte[] byteFina = null;
- Cipher cipher;
- try {
- cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
- cipher.init(Cipher.DECRYPT_MODE, this.key, zeroIv);
- byteFina = cipher.doFinal(byteD);
- } catch (Exception e) {
- throw new RuntimeException(
- "DES加解密异常: " + e);
- } finally {
- cipher = null;
- }
- return byteFina;
- }
- public static void main(String args[]) {
- Encrypt des = new Encrypt();
- try {
- BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
- System.out.print("请输入明文: >");
- String str1 = in.readLine().trim();
- PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(str1)));
- String str2 = des.getEncString(str1);
- String deStr = des.getDesString(str2);
- System.out.println("密文:" + str2);
- System.out.println("明文:" + deStr);
- out.println("密文:" + str2);
- out.println("明文:" + deStr);
- out.close();
- }catch(Exception e) {
- System.out.println(e);
- }
- }
- }
复制代码 |
评分
-
查看全部评分
|