开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 5393|回复: 16
收起左侧

[交流] 一个aes加密的问题

[复制链接]

结帖率:100% (17/17)
发表于 2015-3-15 12:13:16 | 显示全部楼层 |阅读模式   广东省潮州市
  1. package comAES;

  2. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5. import javax.crypto.Cipher;
  6. import javax.crypto.spec.IvParameterSpec;
  7. import javax.crypto.spec.SecretKeySpec;


  8. public class AES {

  9.         private static byte[] a = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0 };;




  10.           public static final byte[] encrypt256(String paramString1, String paramString2)
  11.           {
  12.                
  13.             try
  14.             {
  15.               byte[] arrayOfByte2 = paramString2.getBytes("UTF-8");
  16.               IvParameterSpec localIvParameterSpec = new IvParameterSpec(a);
  17.               SecretKeySpec localSecretKeySpec = new SecretKeySpec(paramString1.getBytes("UTF-8"), "AES");
  18.               Cipher localCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  19.               localCipher.init(1, localSecretKeySpec, localIvParameterSpec);
  20.               byte[] arrayOfByte3 = localCipher.doFinal(arrayOfByte2);
  21.                byte[] arrayOfByte1 = arrayOfByte3;
  22.               return arrayOfByte1;
  23.             }
  24.             catch (Exception localException)
  25.             {
  26.               while (true)
  27.               {
  28.                 localException.printStackTrace();
  29.                 byte[] arrayOfByte1 = null;
  30.               }
  31.             }
  32.           }

  33.           public static final String encrypt256Base64String(String paramString1, String paramString2)
  34.           {
  35.             byte[] arrayOfByte = encrypt256(paramString1, paramString2);
  36.             if (arrayOfByte != null);
  37.             String str = Base64.encode(arrayOfByte);
  38.              return str;
  39.           }
  40.           public static void main(String[] args)throws Exception{
  41.                   String str="zxc123456";
  42.                   String str2="NXFmQPiPMuchITFiNXFmQPiPMuchITFi";
  43.                   String str3=encrypt256Base64String(str2,str);
  44.                         System.out.println(str3);
  45.           }
  46. }
  47.   

  48.   class Base64 {  

  49.                 private static final char[] legalChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();  
  50.             /**
  51.              * data[]进行编码
  52.              * [url=home.php?mod=space&uid=275307]@param[/url] data
  53.              * [url=home.php?mod=space&uid=161696]@Return[/url]
  54.              */  
  55.                 public static String encode(byte[] data) {  
  56.                     int start = 0;  
  57.                     int len = data.length;  
  58.                     StringBuffer buf = new StringBuffer(data.length * 3 / 2);  
  59.           
  60.                     int end = len - 3;  
  61.                     int i = start;  
  62.                     int n = 0;  
  63.           
  64.                     while (i <= end) {  
  65.                         int d = ((((int) data[i]) & 0x0ff) << 16)  
  66.                                 | ((((int) data[i + 1]) & 0x0ff) << 8)  
  67.                                 | (((int) data[i + 2]) & 0x0ff);  
  68.           
  69.                         buf.append(legalChars[(d >> 18) & 63]);  
  70.                         buf.append(legalChars[(d >> 12) & 63]);  
  71.                         buf.append(legalChars[(d >> 6) & 63]);  
  72.                         buf.append(legalChars[d & 63]);  
  73.           
  74.                         i += 3;  
  75.           
  76.                         if (n++ >= 14) {  
  77.                             n = 0;  
  78.                             buf.append(" ");  
  79.                         }  
  80.                     }  
  81.           
  82.                     if (i == start + len - 2) {  
  83.                         int d = ((((int) data[i]) & 0x0ff) << 16)  
  84.                                 | ((((int) data[i + 1]) & 255) << 8);  
  85.           
  86.                         buf.append(legalChars[(d >> 18) & 63]);  
  87.                         buf.append(legalChars[(d >> 12) & 63]);  
  88.                         buf.append(legalChars[(d >> 6) & 63]);  
  89.                         buf.append("=");  
  90.                     } else if (i == start + len - 1) {  
  91.                         int d = (((int) data[i]) & 0x0ff) << 16;  
  92.           
  93.                         buf.append(legalChars[(d >> 18) & 63]);  
  94.                         buf.append(legalChars[(d >> 12) & 63]);  
  95.                         buf.append("==");  
  96.                     }  
  97.           
  98.                     return buf.toString();  
  99.                 }  
  100.           
  101.                 private static int decode(char c) {  
  102.                     if (c >= 'A' && c <= 'Z')  
  103.                         return ((int) c) - 65;  
  104.                     else if (c >= 'a' && c <= 'z')  
  105.                         return ((int) c) - 97 + 26;  
  106.                     else if (c >= '0' && c <= '9')  
  107.                         return ((int) c) - 48 + 26 + 26;  
  108.                     else  
  109.                         switch (c) {  
  110.                         case '+':  
  111.                             return 62;  
  112.                         case '/':  
  113.                             return 63;  
  114.                         case '=':  
  115.                             return 0;  
  116.                         default:  
  117.                             throw new RuntimeException("unexpected code: " + c);  
  118.                         }  
  119.                 }  
  120.           
  121.                 /**
  122.                  * Decodes the given Base64 encoded String to a new byte array. The byte
  123.                  * array holding the decoded data is returned.
  124.                  */  
  125.           
  126.                 public static byte[] decode(String s) {  
  127.           
  128.                     ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  129.                     try {  
  130.                         decode(s, bos);  
  131.                     } catch (IOException e) {  
  132.                         throw new RuntimeException();  
  133.                     }  
  134.                     byte[] decodedBytes = bos.toByteArray();  
  135.                     try {  
  136.                         bos.close();  
  137.                         bos = null;  
  138.                     } catch (IOException ex) {  
  139.                         System.err.println("Error while decoding BASE64: " + ex.toString());  
  140.                     }  
  141.                     return decodedBytes;  
  142.                 }  
  143.           
  144.                 private static void decode(String s, OutputStream os) throws IOException {  
  145.                     int i = 0;  
  146.           
  147.                     int len = s.length();  
  148.           
  149.                     while (true) {  
  150.                         while (i < len && s.charAt(i) <= ' ')  
  151.                             i++;  
  152.           
  153.                         if (i == len)  
  154.                             break;  
  155.           
  156.                         int tri = (decode(s.charAt(i)) << 18)  
  157.                                 + (decode(s.charAt(i + 1)) << 12)  
  158.                                 + (decode(s.charAt(i + 2)) << 6)  
  159.                                 + (decode(s.charAt(i + 3)));  
  160.           
  161.                         os.write((tri >> 16) & 255);  
  162.                         if (s.charAt(i + 2) == '=')  
  163.                             break;  
  164.                         os.write((tri >> 8) & 255);  
  165.                         if (s.charAt(i + 3) == '=')  
  166.                             break;  
  167.                         os.write(tri & 255);  
  168.           
  169.                         i += 4;  
  170.                     }  
  171.                 

  172.                         }
  173.                 
  174.   }
复制代码

点评

已经搞定 那些大神就不用惦记了   广东省潮州市  发表于 2015-3-15 16:53
居然没有编辑上文字 XpxbCE4ctjHWntY6S7lRZQ== 这个加密结果是zxc123456加密的 求个大大帮忙   广东省潮州市  发表于 2015-3-15 12:14

签到天数: 12 天

发表于 2015-3-15 20:05:13 | 显示全部楼层   广东省揭阳市
米和饭 发表于 2015-3-15 19:09
我也搞定了字节集iv的问题了

你这样让我好失落啊~
回复 支持 反对

使用道具 举报

签到天数: 12 天

发表于 2015-3-15 19:21:37 | 显示全部楼层   广东省揭阳市
米和饭 发表于 2015-3-15 19:09
我也搞定了字节集iv的问题了

快发我

点评

你这样让我很惆怅啊   广东省江门市  发表于 2015-3-15 20:04
回复 支持 反对

使用道具 举报

结帖率:90% (9/10)
发表于 2015-3-15 19:09:11 | 显示全部楼层   广东省江门市

我也搞定了字节集iv的问题了

点评

你这样让我好失落啊~   广东省揭阳市  详情 回复 发表于 2015-3-15 20:05
快发我   广东省揭阳市  详情 回复 发表于 2015-3-15 19:21
回复 支持 反对

使用道具 举报

结帖率:100% (17/17)
 楼主| 发表于 2015-3-15 19:02:47 | 显示全部楼层   广东省潮州市
米和饭 发表于 2015-3-15 19:00
不要告诉我,又是jar。。。。。

js.
回复 支持 反对

使用道具 举报

结帖率:90% (9/10)
发表于 2015-3-15 19:00:47 | 显示全部楼层   广东省江门市
ctw507 发表于 2015-3-15 18:52
三个字 想 要 得  美 丽

不要告诉我,又是jar。。。。。
回复 支持 反对

使用道具 举报

结帖率:100% (17/17)
 楼主| 发表于 2015-3-15 18:52:52 | 显示全部楼层   广东省潮州市
Hoibben 发表于 2015-3-15 18:37
傻逼小宇快发我~

三个字 想 要 得  美 丽
回复 支持 反对

使用道具 举报

签到天数: 12 天

发表于 2015-3-15 18:37:32 | 显示全部楼层   广东省揭阳市
ctw507 发表于 2015-3-15 16:53
一百块都不给你 自己已经搞定

傻逼小宇快发我~
回复 支持 反对

使用道具 举报

结帖率:100% (17/17)
 楼主| 发表于 2015-3-15 16:53:24 | 显示全部楼层   广东省潮州市

一百块都不给你 自己已经搞定

点评

傻逼小宇快发我~   广东省揭阳市  详情 回复 发表于 2015-3-15 18:37
回复 支持 反对

使用道具 举报

结帖率:0% (0/1)

签到天数: 1 天

发表于 2015-3-15 16:44:40 高大上手机用户 | 显示全部楼层   广东省广州市
500给你搞定
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报电话0663-3422125,QQ: 793400750,邮箱:wp@125.la
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备12094385号-1) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

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