PHP、Java、Net和Javascript的AES加密解密實現

來源:互聯網
上載者:User
# PHP篇

';    echo(base64_encode($encrypted));    echo '
'; //解密 $encryptedData = base64_decode("2fbwW9+8vPId2/foafZq6Q=="); $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $privateKey, $encryptedData, MCRYPT_MODE_CBC, $iv); echo($decrypted);?>

#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教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.