aes加密算法

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

AES/ECB/PKCS5Padding

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

http://tool.chacuo.net/cryptblowfish

package tt;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom; import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec; public class aesNoRandom {
/**
* 加密
*
* @param content 需要加密的内容
* @param password 加密密码
* @return
*/
public static byte[] encrypt(String content, String password) {
try {
/*KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");*/
SecretKeySpec key = new SecretKeySpec(password.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");// 创建密码器
byte[] byteContent = content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
byte[] result = cipher.doFinal(byteContent);
return result; // 加密
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
} /**解密
* @param content 待解密内容
* @param password 解密密钥
* @return
*/
public static byte[] decrypt(byte[] content, String password) {
try {
/*KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");*/
SecretKeySpec key = new SecretKeySpec(password.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");// 创建密码器
cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
byte[] result = cipher.doFinal(content);
return result; // 加密
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
}
package tt;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Scanner; import sun.misc.*; import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
import javax.crypto.SecretKey; import com.sun.java_cup.internal.runtime.virtual_parse_stack; import tw2.CrytographicTool.CryptoAlgorithm; public class jm { private static String keyString="1234567890123456"; /**将二进制转换成16进制
* @param buf
* @return
*/
public static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
} /**
* 将byte数组转换为表示16进制值的字符串, 如:byte[]{8,18}转换为:0813, 和public static byte[]
* hexStr2ByteArr(String strIn) 互为可逆的转换过程
*
* @param arrB
* 需要转换的byte数组
* @return 转换后的字符串
* @throws Exception
* 本方法不处理任何异常,所有异常全部抛出
*/
public static String byteArr2HexStr(byte[] arrB) {
int iLen = arrB.length;
// 每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍
StringBuffer sb = new StringBuffer(iLen * 2);
for (int i = 0; i < iLen; i++) {
int intTmp = arrB[i];
// 把负数转换为正数
while (intTmp < 0) {
intTmp = intTmp + 256;
}
// 小于0F的数需要在前面补0
if (intTmp < 16) {
sb.append("0");
}
sb.append(Integer.toString(intTmp, 16));
}
return sb.toString();
} /**将16进制转换为二进制
* @param hexStr
* @return
*/
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1)
return null;
byte[] result = new byte[hexStr.length()/2];
for (int i = 0;i< hexStr.length()/2; i++) {
int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
public static void outBytes(byte[] abs) {
for (int i = 0; i < abs.length; i++)
System.out.printf("%d,", abs[i]);
System.out.println();
} public static String myEncrypt(String plainText) throws UnsupportedEncodingException
{
String b64,cipherText,s16;
byte [] bs;
BASE64Encoder base64Encoder; base64Encoder = new BASE64Encoder(); bs = plainText.getBytes("utf-8");
b64=base64Encoder.encode(bs); System.out.println(b64); bs= aesNoRandom.encrypt(b64,keyString); cipherText = base64Encoder.encode(bs); cipherText=cipherText.replaceAll("\r\n", ""); return cipherText; }
public static String myDecrypt(String cipherText) throws IOException
{
String b64,plainText,str16;
byte [] bs;
BASE64Decoder base64Decoder; base64Decoder = new BASE64Decoder(); bs=base64Decoder.decodeBuffer(cipherText); bs= aesNoRandom.decrypt(bs, keyString); str16 = new String(bs,"utf-8"); bs = base64Decoder.decodeBuffer(str16); plainText = new String(bs,"utf-8"); return plainText;
} public static void main(String arg[]) { System.out.println("encrypt testing"); try { byte[] bs = null;
String cipherText = "243434";
String b64 = "";
String s16=null;
String astr;
BASE64Encoder base64Encoder; String plainTextString="";
String plainTextBlowfishString="blowfish";
String keyString="12345678901234567890123456789012";
String keyString16="1234567890123456";
String keyString8="12345678";
byte[] keyBytes=null;
String encryptString, decryptString; Scanner sc=new Scanner(System.in);
System.out.print("请输入符:");
plainTextString=sc.nextLine(); cipherText= zbEncrypt(plainTextString);
System.out.println(cipherText); plainTextString = "";
plainTextString=zbDecrypt(cipherText);
System.out.println(plainTextString); } catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} } }

c#版本

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography; namespace WindowsFormsApplication3
{
class enAES
{ public static string Encrypt(string toEncrypt,PaddingMode mypadmode,string keystring,CipherMode acmode)
{
byte[] keyArray = UTF8Encoding.UTF8.GetBytes(keystring);
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt); RijndaelManaged rDel = new RijndaelManaged();
rDel.BlockSize = ;
rDel.KeySize = ;
rDel.Key = keyArray; rDel.Mode = acmode;
rDel.Padding = mypadmode; ICryptoTransform cTransform = rDel.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, , toEncryptArray.Length); return Convert.ToBase64String(resultArray, , resultArray.Length);
} public static string Decrypt(string toDecrypt, PaddingMode mypadmode, string keystring, CipherMode acmode)
{
byte[] keyArray = UTF8Encoding.UTF8.GetBytes(keystring);
byte[] toEncryptArray = Convert.FromBase64String(toDecrypt); RijndaelManaged rdel = new RijndaelManaged();
rdel.KeySize = ;
rdel.BlockSize = ; rdel.Key = keyArray; rdel.Mode = acmode;
rdel.Padding = mypadmode; ICryptoTransform ctrans = rdel.CreateDecryptor();
byte[] result = ctrans.TransformFinalBlock(toEncryptArray, , toEncryptArray.Length); return UTF8Encoding.UTF8.GetString(result); }
}
}

AES class

   private void button1_Click(object sender, EventArgs e)
{
byte[] bsPlain = Encoding.Default.GetBytes("blowfish");
byte[] key = Convert.FromBase64String("Y2xvc2V3YnE="); PaddingMode aPadmode=PaddingMode.PKCS7;
if (this.listBox1.SelectedIndex == )
aPadmode = PaddingMode.None;
else if (this.listBox1.SelectedIndex == )
aPadmode = PaddingMode.PKCS7;
else if (this.listBox1.SelectedIndex == )
aPadmode = PaddingMode.Zeros;
else if (this.listBox1.SelectedIndex == )
aPadmode = PaddingMode.ANSIX923;
else if (this.listBox1.SelectedIndex == )
aPadmode = PaddingMode.ISO10126; CipherMode acmode = CipherMode.ECB; if (this.listBox2.SelectedIndex == )
acmode = CipherMode.CBC;
else if (this.listBox2.SelectedIndex == )
acmode = CipherMode.ECB;
else if (this.listBox2.SelectedIndex == )
acmode = CipherMode.OFB;
else if (this.listBox2.SelectedIndex == )
acmode = CipherMode.CFB;
else if (this.listBox2.SelectedIndex == )
acmode = CipherMode.CTS; try
{
this.textBox2.Text = enAES.Encrypt(this.textBox1.Text, aPadmode, this.textBox4.Text, acmode); this.textBox3.Text = enAES.Decrypt(this.textBox2.Text, aPadmode, this.textBox4.Text, acmode);
}
catch (Exception)
{ this.textBox3.Text = "not support padding mode";
} }

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. voj 1754 spfa

    最优贸易 最优贸易 描述 C 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市.任意两个 城市之间最多只有一条道路直接相连.这 m 条道路中有一部分为单向通行的道路,一部分 ...

  2. 利用django.core.mail发送QQ邮件的配置

    1.在 settings.py 的最后面加上类似这些 EMAIL_USE_SSL = True EMAIL_HOST = 'smtp.qq.com' # 如果是 163 改成 smtp.163.com ...

  3. timer Compliant Controller project (4)layout and gerber, paning

    1 LAYOUT 2 Gerber 3 CAM350-Paining

  4. python 读写西门子PLC 包含S7协议和Fetch/Write协议,s7支持200smart,300PLC,1200PLC,1500PLC

    本文将使用一个gitHub开源的组件技术来读写西门子plc数据,使用的是基于以太网的TCP/IP实现,不需要额外的组件,读取操作只要放到后台线程就不会卡死线程,本组件支持超级方便的高性能读写操作 nu ...

  5. Mysql的日期转换成星期[某天对应周几]

    |—— 应用中会有各种不同的需求,要灵活应对:比如拿到某一日期要知道是周几 |——DAYOFWEEK(date) [返回日期date的星期索引(1=星期天,2=星期一, ……7=星期六).这些索引值对 ...

  6. Laravel学习之旅(一)

    路由 1.简介:简单的说就是将用户的请求转发给相应的程序进行处理: 2.作用:就是建立url和程序之间的映射. 3.请求类型:get.post.put.patch.delete 相比于thinkphp ...

  7. UT源码+105032014070

    设计三角形问题的程序 输入三个整数a.b.c,分别作为三角形的三条边,现通过程序判断由三条边构成的三角形的类型为等边三角形.等腰三角形.一般三角形(特殊的还有直角三角形),以及不构成三角形.(等腰直角 ...

  8. Struts2自定义标签2自定义一个按班级id查询出该班级下的学生,存放进值栈,并遍历出来。

    Struts2自定义标签的流程概念: (1)需要两个类:标签类(继承相应的tag类),基本类(继承Component).标签类专门负责从客户端取得用户输入的一些属性,这个普通的jsp自定义标签一样,取 ...

  9. oracle 表空间总结

                           表空间总结  一.认识表空间 1:表空间概念: 表空间是数据库中最大的逻辑单位,Oracle数据库采用表空间将相关的逻辑组件组合在一起,一个Oracle数 ...

  10. 【linux】linux权限管理

    一.权限的基本概念                                                   权限:访问计算机资源或服务的访问能力. Linux中,每一个资源或者服务的权限, ...