# php篇
#javascript篇
# java篇
import javax.crypto.cipher;import javax.crypto.spec.ivparameterspec;import javax.crypto.spec.secretkeyspec;import org.junit.test; @test public void testcrosslanguageencrypt() throws exception{ system.out.println(encrypt()); system.out.println(desencrypt()); } public static string encrypt() throws exception { try { string data = test string; string key = 1234567812345678; string iv = 1234567812345678; cipher cipher = cipher.getinstance(aes/cbc/nopadding); int blocksize = cipher.getblocksize(); byte[] databytes = data.getbytes(); int plaintextlength = databytes.length; if (plaintextlength % blocksize != 0) { plaintextlength = plaintextlength + (blocksize - (plaintextlength % blocksize)); } byte[] plaintext = new byte[plaintextlength]; system.arraycopy(databytes, 0, plaintext, 0, databytes.length); secretkeyspec keyspec = new secretkeyspec(key.getbytes(), aes); ivparameterspec ivspec = new ivparameterspec(iv.getbytes()); cipher.init(cipher.encrypt_mode, keyspec, ivspec); byte[] encrypted = cipher.dofinal(plaintext); return new sun.misc.base64encoder().encode(encrypted); } catch (exception e) { e.printstacktrace(); return null; } } public static string desencrypt() throws exception { try { string data = 2fbww9+8vpid2/foafzq6q==; string key = 1234567812345678; string iv = 1234567812345678; byte[] encrypted1 = new sun.misc.base64decoder().decodebuffer(data); cipher cipher = cipher.getinstance(aes/cbc/nopadding); secretkeyspec keyspec = new secretkeyspec(key.getbytes(), aes); ivparameterspec ivspec = new ivparameterspec(iv.getbytes()); cipher.init(cipher.decrypt_mode, keyspec, ivspec); byte[] original = cipher.dofinal(encrypted1); string originalstring = new string(original); return originalstring; } catch (exception e) { e.printstacktrace(); return null; } }
# .net篇
using system;using system.collections.generic;using system.linq;using system.text;using system.security.cryptography;namespace test{ class class1 { static void main(string[] args) { console.writeline(i am comming); string source = test string; string encryptdata = class1.encrypt(source, 1234567812345678, 1234567812345678); console.writeline(=1==); console.writeline(encryptdata); console.writeline(=2==); string decryptdata = class1.decrypt(2fbww9+8vpid2/foafzq6q==, 1234567812345678, 1234567812345678); console.writeline(decryptdata); console.writeline(=3==); console.writeline(i will go out); } public static string encrypt(string toencrypt, string key, string iv) { byte[] keyarray = utf8encoding.utf8.getbytes(key); byte[] ivarray = utf8encoding.utf8.getbytes(iv); byte[] toencryptarray = utf8encoding.utf8.getbytes(toencrypt); rijndaelmanaged rdel = new rijndaelmanaged(); rdel.key = keyarray; rdel.iv = ivarray; rdel.mode = ciphermode.cbc; rdel.padding = paddingmode.zeros; 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, string key, string iv) { byte[] keyarray = utf8encoding.utf8.getbytes(key); byte[] ivarray = utf8encoding.utf8.getbytes(iv); byte[] toencryptarray = convert.frombase64string(todecrypt); rijndaelmanaged rdel = new rijndaelmanaged(); rdel.key = keyarray; rdel.iv = ivarray; rdel.mode = ciphermode.cbc; rdel.padding = paddingmode.zeros; icryptotransform ctransform = rdel.createdecryptor(); byte[] resultarray = ctransform.transformfinalblock(toencryptarray, 0, toencryptarray.length); return utf8encoding.utf8.getstring(resultarray); } }}
跨语言加解密的要求是:aes/cbc/zeropadding 128位模式,key和iv一样,编码统一用utf-8。不支持zeropadding的就用nopadding.
以上就介绍了php、java、net和javascript的aes加密解密实现,包括了方面的内容,希望对php教程有兴趣的朋友有所帮助。