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. ssh隧道(通过跳板机)连接mysql

    案例: A服务器   B服务器   C服务器mysql 现在mysql服务器C只能通过内网访问,B服务器就能通过内网连接访问到mysql A服务器无法直接连接C服务器mysql,所以要通过跳板机(跳板 ...

  2. IOS-CoreData(增删改查、表关联、分页和模糊查询、多个数据库)

    1>什么是CoreData Core Data是iOS5之后才出现的一个框架,它提供了对象-关系映射(ORM)的功能,即能够将OC对象转化成数据,保存在SQLite数据库文件中,也能够将保存在数 ...

  3. 最短路之SPFA算法

    部分来自:http://blog.csdn.net/juststeps/article/details/8772755 求最短路径的算法有许多种,除了排序外,恐怕是OI界中解决同一类问题算法最多的了. ...

  4. 从 TWAIN 设备中扫描图像

    转自(http://yonsm.net/scan-images-from-a-twain-device/) 一.简介 TWAIN 数据源管理程序 (DSM) 工业标准的软件库,用于从静态图像设备提取图 ...

  5. Nginx实践03-配置虚拟主机的3种方式

    基于IP.端口号.名称3种方式 1.基于IP的虚拟主机配置(使用最少) 基于ip的虚拟主机配置,需要配置单个网卡上多个ip地址,这种方式管理比较麻烦,所以用的很少. 1.1 设置单个网卡多个IP 查看 ...

  6. 简单了解json以及使用google json 2.2

    json简介: JSON: JavaScript对象表示法(JavaScript Object Notation) JSON是存储和交换信息的语法. JSON是轻量级的文本交互格式 JSON独立于语言 ...

  7. SQL竖列变横列

    DROP TABLE IF EXISTS curriculumTable; CREATE TABLE curriculumTable ( id INT PRIMARY KEY AUTO_INCREME ...

  8. XMU 1246

    http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1246 求区间内素数个数,经典问题,区间长度10^6,数的取值最多能到10^12(此题范围稍小) 用 ...

  9. CI框架------codeIgniter

    之前学习了thinkphp,学完之后印象不太深刻,在网上询问了一下,他们都说多学几个框架,以后可以自己写框架. 于是自己就放下thinkphp,下定决心再学一个,于是又从网上看了几个框架,综合比较了一 ...

  10. 汉诺塔(Hanoi)——小小算法

    传送门: 袁咩咩的小小博客 汉诺(Hanoi)塔源于古印度,是非常著名的智力趣题,大意如下: 勃拉玛是古印度的一个开天辟地的神,其在一个庙宇中留下了三根金刚石的棒,第一 根上面套着64个大小不一的圆形 ...