aes加密算法

delphi 、java、c# 、网页在线工具 4个相同

AES/ECB/PKCS5Padding

与网页在线工具加密结果相同

http://tool.chacuo.net/cryptblowfish

  1. package tt;
  2.  
  3. import java.io.UnsupportedEncodingException;
  4. import java.security.InvalidKeyException;
  5. import java.security.NoSuchAlgorithmException;
  6. import java.security.NoSuchProviderException;
  7. import java.security.SecureRandom;
  8.  
  9. import javax.crypto.BadPaddingException;
  10. import javax.crypto.Cipher;
  11. import javax.crypto.IllegalBlockSizeException;
  12. import javax.crypto.KeyGenerator;
  13. import javax.crypto.NoSuchPaddingException;
  14. import javax.crypto.SecretKey;
  15. import javax.crypto.spec.SecretKeySpec;
  16.  
  17. public class aesNoRandom {
  18. /**
  19. * 加密
  20. *
  21. * @param content 需要加密的内容
  22. * @param password 加密密码
  23. * @return
  24. */
  25. public static byte[] encrypt(String content, String password) {
  26. try {
  27. /*KeyGenerator kgen = KeyGenerator.getInstance("AES");
  28. kgen.init(128, new SecureRandom(password.getBytes()));
  29. SecretKey secretKey = kgen.generateKey();
  30. byte[] enCodeFormat = secretKey.getEncoded();
  31. SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");*/
  32. SecretKeySpec key = new SecretKeySpec(password.getBytes(), "AES");
  33. Cipher cipher = Cipher.getInstance("AES");// 创建密码器
  34. byte[] byteContent = content.getBytes("utf-8");
  35. cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
  36. byte[] result = cipher.doFinal(byteContent);
  37. return result; // 加密
  38. } catch (NoSuchAlgorithmException e) {
  39. e.printStackTrace();
  40. } catch (NoSuchPaddingException e) {
  41. e.printStackTrace();
  42. } catch (InvalidKeyException e) {
  43. e.printStackTrace();
  44. } catch (UnsupportedEncodingException e) {
  45. e.printStackTrace();
  46. } catch (IllegalBlockSizeException e) {
  47. e.printStackTrace();
  48. } catch (BadPaddingException e) {
  49. e.printStackTrace();
  50. }
  51. return null;
  52. }
  53.  
  54. /**解密
  55. * @param content 待解密内容
  56. * @param password 解密密钥
  57. * @return
  58. */
  59. public static byte[] decrypt(byte[] content, String password) {
  60. try {
  61. /*KeyGenerator kgen = KeyGenerator.getInstance("AES");
  62. kgen.init(128, new SecureRandom(password.getBytes()));
  63. SecretKey secretKey = kgen.generateKey();
  64. byte[] enCodeFormat = secretKey.getEncoded();
  65. SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");*/
  66. SecretKeySpec key = new SecretKeySpec(password.getBytes(), "AES");
  67. Cipher cipher = Cipher.getInstance("AES");// 创建密码器
  68. cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
  69. byte[] result = cipher.doFinal(content);
  70. return result; // 加密
  71. } catch (NoSuchAlgorithmException e) {
  72. e.printStackTrace();
  73. } catch (NoSuchPaddingException e) {
  74. e.printStackTrace();
  75. } catch (InvalidKeyException e) {
  76. e.printStackTrace();
  77. } catch (IllegalBlockSizeException e) {
  78. e.printStackTrace();
  79. } catch (BadPaddingException e) {
  80. e.printStackTrace();
  81. }
  82. return null;
  83. }
  84. }
  1. package tt;
  2.  
  3. import java.io.IOException;
  4. import java.io.UnsupportedEncodingException;
  5. import java.util.Scanner;
  6.  
  7. import sun.misc.*;
  8.  
  9. import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
  10. import javax.crypto.SecretKey;
  11.  
  12. import com.sun.java_cup.internal.runtime.virtual_parse_stack;
  13.  
  14. import tw2.CrytographicTool.CryptoAlgorithm;
  15.  
  16. public class jm {
  17.  
  18. private static String keyString="1234567890123456";
  19.  
  20. /**将二进制转换成16进制
  21. * @param buf
  22. * @return
  23. */
  24. public static String parseByte2HexStr(byte buf[]) {
  25. StringBuffer sb = new StringBuffer();
  26. for (int i = 0; i < buf.length; i++) {
  27. String hex = Integer.toHexString(buf[i] & 0xFF);
  28. if (hex.length() == 1) {
  29. hex = '0' + hex;
  30. }
  31. sb.append(hex.toUpperCase());
  32. }
  33. return sb.toString();
  34. }
  35.  
  36. /**
  37. * 将byte数组转换为表示16进制值的字符串, 如:byte[]{8,18}转换为:0813, 和public static byte[]
  38. * hexStr2ByteArr(String strIn) 互为可逆的转换过程
  39. *
  40. * @param arrB
  41. * 需要转换的byte数组
  42. * @return 转换后的字符串
  43. * @throws Exception
  44. * 本方法不处理任何异常,所有异常全部抛出
  45. */
  46. public static String byteArr2HexStr(byte[] arrB) {
  47. int iLen = arrB.length;
  48. // 每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍
  49. StringBuffer sb = new StringBuffer(iLen * 2);
  50. for (int i = 0; i < iLen; i++) {
  51. int intTmp = arrB[i];
  52. // 把负数转换为正数
  53. while (intTmp < 0) {
  54. intTmp = intTmp + 256;
  55. }
  56. // 小于0F的数需要在前面补0
  57. if (intTmp < 16) {
  58. sb.append("0");
  59. }
  60. sb.append(Integer.toString(intTmp, 16));
  61. }
  62. return sb.toString();
  63. }
  64.  
  65. /**将16进制转换为二进制
  66. * @param hexStr
  67. * @return
  68. */
  69. public static byte[] parseHexStr2Byte(String hexStr) {
  70. if (hexStr.length() < 1)
  71. return null;
  72. byte[] result = new byte[hexStr.length()/2];
  73. for (int i = 0;i< hexStr.length()/2; i++) {
  74. int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
  75. int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
  76. result[i] = (byte) (high * 16 + low);
  77. }
  78. return result;
  79. }
  80. public static void outBytes(byte[] abs) {
  81. for (int i = 0; i < abs.length; i++)
  82. System.out.printf("%d,", abs[i]);
  83. System.out.println();
  84. }
  85.  
  86. public static String myEncrypt(String plainText) throws UnsupportedEncodingException
  87. {
  88. String b64,cipherText,s16;
  89. byte [] bs;
  90. BASE64Encoder base64Encoder;
  91.  
  92. base64Encoder = new BASE64Encoder();
  93.  
  94. bs = plainText.getBytes("utf-8");
  95. b64=base64Encoder.encode(bs);
  96.  
  97. System.out.println(b64);
  98.  
  99. bs= aesNoRandom.encrypt(b64,keyString);
  100.  
  101. cipherText = base64Encoder.encode(bs);
  102.  
  103. cipherText=cipherText.replaceAll("\r\n", "");
  104.  
  105. return cipherText;
  106.  
  107. }
  108. public static String myDecrypt(String cipherText) throws IOException
  109. {
  110. String b64,plainText,str16;
  111. byte [] bs;
  112. BASE64Decoder base64Decoder;
  113.  
  114. base64Decoder = new BASE64Decoder();
  115.  
  116. bs=base64Decoder.decodeBuffer(cipherText);
  117.  
  118. bs= aesNoRandom.decrypt(bs, keyString);
  119.  
  120. str16 = new String(bs,"utf-8");
  121.  
  122. bs = base64Decoder.decodeBuffer(str16);
  123.  
  124. plainText = new String(bs,"utf-8");
  125.  
  126. return plainText;
  127. }
  128.  
  129. public static void main(String arg[]) {
  130.  
  131. System.out.println("encrypt testing");
  132.  
  133. try {
  134.  
  135. byte[] bs = null;
  136. String cipherText = "243434";
  137. String b64 = "";
  138. String s16=null;
  139. String astr;
  140. BASE64Encoder base64Encoder;
  141.  
  142. String plainTextString="";
  143. String plainTextBlowfishString="blowfish";
  144. String keyString="12345678901234567890123456789012";
  145. String keyString16="1234567890123456";
  146. String keyString8="12345678";
  147. byte[] keyBytes=null;
  148. String encryptString, decryptString;
  149.  
  150. Scanner sc=new Scanner(System.in);
  151. System.out.print("请输入符:");
  152. plainTextString=sc.nextLine();
  153.  
  154. cipherText= zbEncrypt(plainTextString);
  155. System.out.println(cipherText);
  156.  
  157. plainTextString = "";
  158. plainTextString=zbDecrypt(cipherText);
  159. System.out.println(plainTextString);
  160.  
  161. } catch (Exception e) {
  162. // TODO: handle exception
  163. e.printStackTrace();
  164. }
  165.  
  166. }
  167.  
  168. }

c#版本

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Security.Cryptography;
  7.  
  8. namespace WindowsFormsApplication3
  9. {
  10. class enAES
  11. {
  12.  
  13. public static string Encrypt(string toEncrypt,PaddingMode mypadmode,string keystring,CipherMode acmode)
  14. {
  15. byte[] keyArray = UTF8Encoding.UTF8.GetBytes(keystring);
  16. byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
  17.  
  18. RijndaelManaged rDel = new RijndaelManaged();
  19. rDel.BlockSize = ;
  20. rDel.KeySize = ;
  21. rDel.Key = keyArray;
  22.  
  23. rDel.Mode = acmode;
  24. rDel.Padding = mypadmode;
  25.  
  26. ICryptoTransform cTransform = rDel.CreateEncryptor();
  27. byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, , toEncryptArray.Length);
  28.  
  29. return Convert.ToBase64String(resultArray, , resultArray.Length);
  30. }
  31.  
  32. public static string Decrypt(string toDecrypt, PaddingMode mypadmode, string keystring, CipherMode acmode)
  33. {
  34. byte[] keyArray = UTF8Encoding.UTF8.GetBytes(keystring);
  35. byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);
  36.  
  37. RijndaelManaged rdel = new RijndaelManaged();
  38. rdel.KeySize = ;
  39. rdel.BlockSize = ;
  40.  
  41. rdel.Key = keyArray;
  42.  
  43. rdel.Mode = acmode;
  44. rdel.Padding = mypadmode;
  45.  
  46. ICryptoTransform ctrans = rdel.CreateDecryptor();
  47. byte[] result = ctrans.TransformFinalBlock(toEncryptArray, , toEncryptArray.Length);
  48.  
  49. return UTF8Encoding.UTF8.GetString(result);
  50.  
  51. }
  52. }
  53. }

AES class

  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. byte[] bsPlain = Encoding.Default.GetBytes("blowfish");
  4. byte[] key = Convert.FromBase64String("Y2xvc2V3YnE=");
  5.  
  6. PaddingMode aPadmode=PaddingMode.PKCS7;
  7. if (this.listBox1.SelectedIndex == )
  8. aPadmode = PaddingMode.None;
  9. else if (this.listBox1.SelectedIndex == )
  10. aPadmode = PaddingMode.PKCS7;
  11. else if (this.listBox1.SelectedIndex == )
  12. aPadmode = PaddingMode.Zeros;
  13. else if (this.listBox1.SelectedIndex == )
  14. aPadmode = PaddingMode.ANSIX923;
  15. else if (this.listBox1.SelectedIndex == )
  16. aPadmode = PaddingMode.ISO10126;
  17.  
  18. CipherMode acmode = CipherMode.ECB;
  19.  
  20. if (this.listBox2.SelectedIndex == )
  21. acmode = CipherMode.CBC;
  22. else if (this.listBox2.SelectedIndex == )
  23. acmode = CipherMode.ECB;
  24. else if (this.listBox2.SelectedIndex == )
  25. acmode = CipherMode.OFB;
  26. else if (this.listBox2.SelectedIndex == )
  27. acmode = CipherMode.CFB;
  28. else if (this.listBox2.SelectedIndex == )
  29. acmode = CipherMode.CTS;
  30.  
  31. try
  32. {
  33. this.textBox2.Text = enAES.Encrypt(this.textBox1.Text, aPadmode, this.textBox4.Text, acmode);
  34.  
  35. this.textBox3.Text = enAES.Decrypt(this.textBox2.Text, aPadmode, this.textBox4.Text, acmode);
  36. }
  37. catch (Exception)
  38. {
  39.  
  40. this.textBox3.Text = "not support padding mode";
  41. }
  42.  
  43. }

form

AES 加密算法 跨语言的更多相关文章

  1. Atitit.跨语言 java c#.net php js常用的codec encode算法api 兼容性  应该内置到语言里面

    Atitit.跨语言 java c#.net php js常用的codec encode算法api 兼容性  应该内置到语言里面 1. 常用算法1 1.1. 目录2 1.2. 定义和用法编辑2 1.3 ...

  2. AES 加密算法的原理详解

    AES 加密算法的原理详解 本教程摘选自 https://blog.csdn.net/qq_28205153/article/details/55798628 的原理部分. AES简介 高级加密标准( ...

  3. 密码学基础:AES加密算法

    [原创]密码学基础:AES加密算法-密码应用-看雪论坛-安全社区|安全招聘|bbs.pediy.com 目录 基础部分概述: 第一节:AES算法简介 第二节:AES算法相关数学知识 素域简介 扩展域简 ...

  4. Atitit java c# php c++ js跨语言调用matlab实现边缘检测等功能attilax总结

    Atitit java c# php c++ js跨语言调用matlab实现边缘检测等功能attilax总结 1.1. 边缘检测的基本方法Canny最常用了1 1.2. 编写matlab边缘检测代码, ...

  5. 跨语言和跨编译器的那些坑(CPython vs IronPython)

    代码是宝贵的,世界上最郁闷的事情,便是写好的代码,还要在另外的平台上重写一次,或是同时维护功能相同的两套代码.所以才需要跨平台. 不仅如此,比如有人会吐槽Python的原生解释器CPython跑得太慢 ...

  6. AES加密算法C++实现

    我从网上下载了一套AES加密算法的C++实现,代码如下: (1)aes.h #ifndef SRC_UTILS_AES_H #define SRC_UTILS_AES_H class AES { pu ...

  7. Golang通过Thrift框架完美实现跨语言调用

    每种语言都有自己最擅长的领域,Golang 最适合的领域就是服务器端程序. 做为服务器端程序,需要考虑性能同时也要考虑与各种语言之间方便的通讯.采用http协议简单,但性能不高.采用TCP通讯,则需要 ...

  8. Apache Thrift 跨语言服务开发框架

    Apache Thrift 是一种支持多种编程语言的远程服务调用框架,由 Facebook 于 2007 年开发,并于 2008 年进入 Apache 开源项目管理.Apache Thrift 通过 ...

  9. Atitti 跨语言异常的转换抛出 java js

    Atitti 跨语言异常的转换抛出 java js 异常的转换,直接反序列化为json对象e对象即可.. Js.没有完整的e机制,可以参考java的实现一个stack层次机制的e对象即可.. 抛出Ru ...

随机推荐

  1. mybatis关联查询resultmap的使用详解resultmap

    因为该案例比较典型,所以记录一下,恐后期有所疑问,以便用时便于会议. 案例典型在 关联关系典型 主表一张业务模板表 TABLE_NAME  COLUMN_NAME COMMENTS YMIT_BIZ_ ...

  2. 51nod-1455-dp/缩小范围

    1455 宝石猎人  题目来源: CodeForces 基准时间限制:2 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  关注 苏塞克岛是一个有着30001个小岛的群岛,这 ...

  3. Linux vi编辑器的使用

    vi是Visual Interface的简称,它是Linux/Unix下的文本编辑器,例如你想编辑文件english.txt,则你可以在终端下输入 vi english.txt命令,然后就进入了编辑界 ...

  4. laravel中数据库迁移的使用:

    创建数据库迁移文件: php artisan make:migration create_links_table 创建完表之后,设置字段: public function up() { Schema: ...

  5. tortoiseGIT保存用户名密码

    虽然GIT可以使用SSH来免去输入用户名密码的麻烦,但是更多的人我相信还是比较喜欢使用tortoiseGIT. 使用HTTP模式的代码库可以通过保存用户名密码的方式来免去重复输入的麻烦. 首先安装gi ...

  6. 利用Sonar定制自定义JS扫描规则(三)——SSLR JavaScript Toolkit 使用说明

    在上一篇blog中讲了在sonar中如何新增自定义的JS规则,这里面比较难的地方是XPath语句的编写,而要编写正确的XPath语句,首先要拿到语法的AST,下面我们就来介绍如何使用SSLR Java ...

  7. 使用百度地图SDK出现的问题及解决方法

    1. 第一个错误信息如下: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.baiduma ...

  8. C# #if, #else和#endif预处理指令

        #if 使您可以开始条件指令,测试一个或多个符号以查看它们是否计算为 true.如果它们的计算结果确实为true,则编译器将计算位于 #if 与最近的 #endif 指令之间的所有代码.例如, ...

  9. tf.cast()数据类型转换

    tf.cast()函数的作用是执行 tensorflow 中张量数据类型转换,比如读入的图片如果是int8类型的,一般在要在训练前把图像的数据格式转换为float32. cast定义: cast(x, ...

  10. .net常用正则表达式小结

    好久没有些博客了,今天就随便写点工作当中遇到的一些问题.正则表达式估计大家在开发的过程中都会遇到,下面是我平时用到的以及自己整理的一些常用的正则表达式,供大家学习和参考. "^\d+$&qu ...