AES加密CBC模式兼容互通四种编程语言平台Java、PHP、C#
密码学中的高级加密标准(Advanced Encryption Standard,AES),又称高级加密标准Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。经过五年的甄选流程,高级加密标准由美国国家标准与技术研究院 (NIST)于2001年11月26日发布于FIPS PUB197,并在2002年5月26日成为有效的标准。2006年,高级加密标准已然成为对称密钥加密中最流行的算法之一。该算法为比利时密码学家Joan Daemen和VincentRijmen所设计,结合两位作者的名字,以Rijndael之命名之,投稿高级加密标准的甄选流程。(Rijdael的发音近于 "Rhinedoll"。)
算法/模式/填充 16字节加密后数据长度 不满16字节加密后长度 AES/CBC/NoPadding 16 不支持 AES/CBC/PKCS5Padding 32 16 AES/CBC/ISO10126Padding 32 16 AES/CFB/NoPadding 16 原始数据长度 AES/CFB/PKCS5Padding 32 16 AES/CFB/ISO10126Padding 32 16 AES/ECB/NoPadding 16 不支持 AES/ECB/PKCS5Padding 32 16 AES/ECB/ISO10126Padding 32 16 AES/OFB/NoPadding 16 原始数据长度 AES/OFB/PKCS5Padding 32 16 AES/OFB/ISO10126Padding 32 16 AES/PCBC/NoPadding 16 不支持 AES/PCBC/PKCS5Padding 32 16 AES/PCBC/ISO10126Padding 32 16
看到这么多模式,目标是希望找到 PHP、Javascript、Java、C# 的AES加密模式一个交集;后来发现PHP的AES加密填充只有ZeroPadding(补零 - 因为数据长度不是16的整数倍就需要填充),而Java是没有这种填充模式,那么只能利用Java的填充模式NoPadding写一个。
Java代码:
1 /* 2 * To change this template, choose Tools | Templates 3 * and open the template in the editor. 4 */ 5 6 /** 7 * 8 * @author Jacker 9 */ 10 11 import javax.crypto.Cipher; 12 import javax.crypto.spec.IvParameterSpec; 13 import javax.crypto.spec.SecretKeySpec; 14 import sun.misc.BASE64Decoder; 15 16 public class Encryption 17 { 18 public static void main(String args[]) throws Exception { 19 System.out.println(encrypt()); 20 System.out.println(desEncrypt()); 21 } 22 23 public static String encrypt() throws Exception { 24 try { 25 String data = "Test String"; 26 String key = "1234567812345678"; 27 String iv = "1234567812345678"; 28 29 Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); 30 int blockSize = cipher.getBlockSize(); 31 32 byte[] dataBytes = data.getBytes(); 33 int plaintextLength = dataBytes.length; 34 if (plaintextLength % blockSize != 0) { 35 plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize)); 36 } 37 38 byte[] plaintext = new byte[plaintextLength]; 39 System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length); 40 41 SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES"); 42 IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes()); 43 44 cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec); 45 byte[] encrypted = cipher.doFinal(plaintext); 46 47 return new sun.misc.BASE64Encoder().encode(encrypted); 48 49 } catch (Exception e) { 50 e.printStackTrace(); 51 return null; 52 } 53 } 54 55 public static String desEncrypt() throws Exception { 56 try 57 { 58 String data = "2fbwW9+8vPId2/foafZq6Q=="; 59 String key = "1234567812345678"; 60 String iv = "1234567812345678"; 61 62 byte[] encrypted1 = new BASE64Decoder().decodeBuffer(data); 63 64 Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); 65 SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES"); 66 IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes()); 67 68 cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec); 69 70 byte[] original = cipher.doFinal(encrypted1); 71 String originalString = new String(original); 72 return originalString; 73 } 74 catch (Exception e) { 75 e.printStackTrace(); 76 return null; 77 } 78 } 79 }
PHP代码:
1 <?php 2 $privateKey = "1234567812345678"; 3 $iv = "1234567812345678"; 4 $data = "Test String"; 5 6 //加密 7 $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $privateKey, $data, MCRYPT_MODE_CBC, $iv); 8 echo(base64_encode($encrypted)); 9 echo '<br/>'; 10 11 //解密 12 $encryptedData = base64_decode("2fbwW9+8vPId2/foafZq6Q=="); 13 $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $privateKey, $encryptedData, MCRYPT_MODE_CBC, $iv); 14 echo($decrypted); 15 ?>
C#代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Security.Cryptography; 6 7 namespace pda_demo 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 String encryptData = Program.Encrypt("Test String", "1234567812345678", "1234567812345678"); 14 Console.WriteLine(encryptData); 15 16 String decryptData = Program.Decrypt("2fbwW9+8vPId2/foafZq6Q==", "1234567812345678", "1234567812345678"); 17 Console.WriteLine(decryptData); 18 19 Console.Read(); 20 } 21 22 public static string Encrypt(string toEncrypt, string key, string iv) 23 { 24 byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key); 25 byte[] ivArray = UTF8Encoding.UTF8.GetBytes(iv); 26 byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt); 27 28 RijndaelManaged rDel = new RijndaelManaged(); 29 rDel.Key = keyArray; 30 rDel.IV = ivArray; 31 rDel.Mode = CipherMode.CBC; 32 rDel.Padding = PaddingMode.Zeros; 33 34 ICryptoTransform cTransform = rDel.CreateEncryptor(); 35 byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); 36 37 return Convert.ToBase64String(resultArray, 0, resultArray.Length); 38 } 39 40 public static string Decrypt(string toDecrypt, string key, string iv) 41 { 42 byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key); 43 byte[] ivArray = UTF8Encoding.UTF8.GetBytes(iv); 44 byte[] toEncryptArray = Convert.FromBase64String(toDecrypt); 45 46 RijndaelManaged rDel = new RijndaelManaged(); 47 rDel.Key = keyArray; 48 rDel.IV = ivArray; 49 rDel.Mode = CipherMode.CBC; 50 rDel.Padding = PaddingMode.Zeros; 51 52 ICryptoTransform cTransform = rDel.CreateDecryptor(); 53 byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); 54 55 return UTF8Encoding.UTF8.GetString(resultArray); 56 } 57 } 58 }