aes加密算法 java
delphi 、java、c# 、網頁在線工具 4個相同算法
AES/ECB/PKCS5Paddingc#
與網頁在線工具加密結果相同數組
http://tool.chacuo.net/cryptblowfishapp
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#版本dom
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 = 128; rDel.KeySize = 128; rDel.Key = keyArray; rDel.Mode = acmode; rDel.Padding = mypadmode; ICryptoTransform cTransform = rDel.CreateEncryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Convert.ToBase64String(resultArray, 0, 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 = 128; rdel.BlockSize = 128; rdel.Key = keyArray; rdel.Mode = acmode; rdel.Padding = mypadmode; ICryptoTransform ctrans = rdel.CreateDecryptor(); byte[] result = ctrans.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return UTF8Encoding.UTF8.GetString(result); } } }
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 == 0) aPadmode = PaddingMode.None; else if (this.listBox1.SelectedIndex == 1) aPadmode = PaddingMode.PKCS7; else if (this.listBox1.SelectedIndex == 2) aPadmode = PaddingMode.Zeros; else if (this.listBox1.SelectedIndex == 3) aPadmode = PaddingMode.ANSIX923; else if (this.listBox1.SelectedIndex == 4) aPadmode = PaddingMode.ISO10126; CipherMode acmode = CipherMode.ECB; if (this.listBox2.SelectedIndex == 0) acmode = CipherMode.CBC; else if (this.listBox2.SelectedIndex == 1) acmode = CipherMode.ECB; else if (this.listBox2.SelectedIndex == 2) acmode = CipherMode.OFB; else if (this.listBox2.SelectedIndex == 3) acmode = CipherMode.CFB; else if (this.listBox2.SelectedIndex == 4) 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"; } }