aardio 官方社区

 找回密码
 注册会员

QQ登录

只需一步,快速开始

搜索
查看: 6327|回复: 6

aardio DES加密 java

[复制链接]

4

主题

11

回帖

99

积分

一级会员

积分
99
发表于 2013-9-27 11:27:45 | 显示全部楼层 |阅读模式
麻烦高手看下,我想用aardio实现以下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 sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Encrypt {

        Key key;

        public Encrypt(String str) {
                setKey(str);// 生成密匙
        }

        public Encrypt() {
                setKey("ABCDEFGHIJKLMNOPQRSTUVWX");
        }

         
         // 根据参数生成KEY
          
        public void setKey(String strKey) {
                try {
                        KeyGenerator _generator = KeyGenerator.getInstance("DES");
                        _generator.init(new SecureRandom(strKey.getBytes()));
                        this.key = _generator.generateKey();
                        _generator = null;
                } catch (Exception e) {
                        throw new RuntimeException(
                                        "DES加解密异常: " + e);
                       
                }
        }

         
           //加密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 byte[] getEncCode(byte[] byteS) {
                byte[] byteFina = null;
                Cipher cipher;
                try {
                        cipher = Cipher.getInstance("DES");
                        cipher.init(Cipher.ENCRYPT_MODE, key);
                        byteFina = cipher.doFinal(byteS);
                } catch (Exception e) {
                        throw new RuntimeException(
                                        "DES加解密异常: " + e);
                } finally {
                        cipher = null;
                }
                return byteFina;
        }

        //解密以byte[]密文输入,以byte[]明文输出
         
        private byte[] getDesCode(byte[] byteD) {
                Cipher cipher;
                byte[] byteFina = null;
                try {
                        cipher = Cipher.getInstance("DES");
                        cipher.init(Cipher.DECRYPT_MODE, key);
                        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);
                }
        }
}

9

主题

444

回帖

2546

积分

版主

积分
2546
发表于 2013-9-27 17:06:21 | 显示全部楼层
本帖最后由 roger 于 2013-9-27 17:07 编辑

aardio DES加密、解密:

  1. import crypt.des;
  2. import crypt.bin;
  3. import console;

  4. var des = crypt.des()
  5. des.setPassword("ABCDEFGH");// 设置密码
  6. str = des.encrypt("abc");// DES加密

  7. // 密文转换为BASE64文本格式
  8. base64 = crypt.bin.encodeBase64(str);
  9. console.log( "密文:",base64 )

  10. // BASE64解密,DES解密
  11. str = crypt.bin.decodeBase64(base64);
  12. console.log( "明文:",des.decrypt(str) );
复制代码
如果需要转为UTF8密码, 用下面的代码转换
str  = string.fromto( str,0,65001)

你发的JAVA代码用了随机密码,不同系统可能结果不一致,建议修改如下:

  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.FileWriter;
  4. import java.io.InputStreamReader;
  5. import java.io.PrintWriter;
  6. import java.security.Key;
  7. import java.security.SecureRandom;
  8. import javax.crypto.Cipher;
  9. import javax.crypto.KeyGenerator;
  10. import javax.crypto.spec.IvParameterSpec;
  11. import javax.crypto.spec.SecretKeySpec;
  12. import sun.misc.BASE64Decoder;
  13. import sun.misc.BASE64Encoder;

  14. public class Encrypt {

  15.     SecretKeySpec key;

  16.     public Encrypt(String str) {
  17.         setKey(str);// 生成密匙
  18.     }

  19.     public Encrypt() {
  20.         setKey("ABCDEFGH");
  21.     }

  22.     // 根据参数生成KEY
  23.     public void setKey(String strKey) {
  24.         this.key = new SecretKeySpec(strKey.getBytes(), "DES");  ;
  25.     }

  26.     //加密String明文输入,String密文输出
  27.     public String getEncString(String strMing) {
  28.         byte[] byteMi = null;
  29.         byte[] byteMing = null;
  30.         String strMi = "";
  31.         BASE64Encoder base64en = new BASE64Encoder();
  32.         try {
  33.             byteMing = strMing.getBytes("UTF8");
  34.             byteMi = this.getEncCode(byteMing);
  35.             strMi = base64en.encode(byteMi);
  36.         } catch (Exception e) {
  37.             throw new RuntimeException(
  38.                     "DES加解密异常: " + e);
  39.         } finally {
  40.             base64en = null;
  41.             byteMing = null;
  42.             byteMi = null;
  43.         }
  44.         return strMi;
  45.     }

  46.     // 解密 以String密文输入,String明文输出
  47.     public String getDesString(String strMi) {
  48.         BASE64Decoder base64De = new BASE64Decoder();
  49.         byte[] byteMing = null;
  50.         byte[] byteMi = null;
  51.         String strMing = "";
  52.         try {
  53.             byteMi = base64De.decodeBuffer(strMi);
  54.             byteMing = this.getDesCode(byteMi);
  55.             strMing = new String(byteMing, "UTF8");
  56.         } catch (Exception e) {
  57.             throw new RuntimeException(
  58.                     "DES加解密异常: " + e);
  59.         } finally {
  60.             base64De = null;
  61.             byteMing = null;
  62.             byteMi = null;
  63.         }
  64.         return strMing;
  65.     }

  66.     // 加密以byte[]明文输入,byte[]密文输出

  67.     private static byte[] iv = {0x12,0x34,0x56,0x78,(byte)0x90,(byte)0xAB,(byte)0xCD,(byte)0xEF};
  68.     private byte[] getEncCode(byte[] byteS) {
  69.         IvParameterSpec zeroIv = new IvParameterSpec(iv);
  70.         byte[] byteFina = null;
  71.         Cipher cipher;
  72.         try {
  73.             cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
  74.             cipher.init(Cipher.ENCRYPT_MODE, this.key, zeroIv);
  75.             byteFina = cipher.doFinal(byteS);
  76.         } catch (Exception e) {
  77.             throw new RuntimeException(
  78.                     "DES加解密异常: " + e);
  79.         } finally {
  80.             cipher = null;
  81.         }
  82.         return byteFina;
  83.     }

  84.     //解密以byte[]密文输入,以byte[]明文输出

  85.     private byte[] getDesCode(byte[] byteD) {
  86.         IvParameterSpec zeroIv = new IvParameterSpec(iv);
  87.         byte[] byteFina = null;
  88.         Cipher cipher;
  89.         try {
  90.             cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
  91.             cipher.init(Cipher.DECRYPT_MODE, this.key, zeroIv);
  92.             byteFina = cipher.doFinal(byteD);
  93.         } catch (Exception e) {
  94.             throw new RuntimeException(
  95.                     "DES加解密异常: " + e);
  96.         } finally {
  97.             cipher = null;
  98.         }
  99.         return byteFina;
  100.     }

  101.     public static void main(String args[]) {
  102.         Encrypt des = new Encrypt();
  103.         try {
  104.             BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  105.             System.out.print("请输入明文: >");

  106.             String str1 = in.readLine().trim();
  107.             PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(str1)));
  108.             String str2 = des.getEncString(str1);
  109.             String deStr = des.getDesString(str2);
  110.             System.out.println("密文:" + str2);
  111.             System.out.println("明文:" + deStr);

  112.             out.println("密文:" + str2);
  113.             out.println("明文:" + deStr);
  114.             out.close();
  115.         }catch(Exception e) {
  116.             System.out.println(e);
  117.         }
  118.     }
  119. }
复制代码

评分

参与人数 1 +60 收起 理由
aaucn + 60 很给力!

查看全部评分

4

主题

11

回帖

99

积分

一级会员

积分
99
 楼主| 发表于 2013-9-30 10:43:49 | 显示全部楼层
roger 发表于 2013-9-27 17:06
aardio DES加密、解密:如果需要转为UTF8密码, 用下面的代码转换
str  = string.fromto( str,0,65001)

貌似java 和 aardio对汉字的加解密结果不一致
非常感谢。

点评

请注意看回帖,汉字受编码影响,Java代码里strMing.getBytes("UTF8")。AAuto可以使用 string.fromto( str,0,65001) 即可,细节上请自行完善一下。  发表于 2013-9-30 11:06

4

主题

11

回帖

99

积分

一级会员

积分
99
 楼主| 发表于 2013-9-30 13:04:42 | 显示全部楼层
roger 发表于 2013-9-27 17:06
aardio DES加密、解密:如果需要转为UTF8密码, 用下面的代码转换
str  = string.fromto( str,0,65001)
  1. import crypt.des;
  2. import crypt.bin;
  3. import console;


  4. var des = crypt.des()
  5. des.setPassword("abcdefgh");// 设置密码
  6. str =  string.fromto( "你好",0,65001);
  7. str = des.encrypt(str);// DES加密

  8. // 密文转换为BASE64文本格式
  9. base64 = crypt.bin.encodeBase64(str);
  10. console.log( "密文:",base64 )  

  11. // BASE64解密,DES解密
  12. str = crypt.bin.decodeBase64(base64);
  13. console.log( "明文:",des.decrypt(str) );
复制代码
加密结果已经正常了。
解密还是有点问题,
应该还是编码的问题,
不知道该怎么修改,麻烦你有时间了帮忙看看吧。

非常感谢你的耐心解答。

4

主题

852

回帖

4689

积分

荣誉会员

积分
4689
发表于 2013-9-30 13:14:15 | 显示全部楼层
xqh5945 发表于 2013-9-30 13:04
加密结果已经正常了。
解密还是有点问题,
应该还是编码的问题,

最后一句改成
string.fromto( des.decrypt(str) )
UTF8编码了就要UTF8还原,BASE64编码了就要BASE64还原,这么简单的错误自己看不出来?

4

主题

11

回帖

99

积分

一级会员

积分
99
 楼主| 发表于 2013-10-8 13:53:24 | 显示全部楼层
不争 发表于 2013-9-30 13:14
最后一句改成 UTF8编码了就要UTF8还原,BASE64编码了就要BASE64还原,这么简单的错误自己看不出来?

知道需要UTF8还原,就是不知道在aardio里怎么写。
已经按你说的修改好了,谢谢。
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

手机版|未经许可严禁引用或转载本站文章|aardio.com|aardio 官方社区 ( 皖ICP备09012014号 )

GMT+8, 2023-3-28 13:07 , Processed in 0.054199 second(s), 23 queries .

Powered by Discuz! X3.5

Copyright © 2001-2023 Tencent Cloud.

快速回复 返回顶部 返回列表