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