Maison > développement back-end > tutoriel php > PHP、Java、Net和Javascript的AES加密解密实现

PHP、Java、Net和Javascript的AES加密解密实现

WBOY
Libérer: 2016-08-08 09:23:36
original
1041 Les gens l'ont consulté

# PHP篇

<?php $privateKey = "1234567812345678";
    $iv     = "1234567812345678";
    $data   = "Test String";

    //加密
    $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $privateKey, $data, MCRYPT_MODE_CBC, $iv);
    echo &#39;<br/>';
    echo(base64_encode($encrypted));
    echo '<br>';

    //解密
    $encryptedData = base64_decode("2fbwW9+8vPId2/foafZq6Q==");
    $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $privateKey, $encryptedData, MCRYPT_MODE_CBC, $iv);
    echo($decrypted);
?>
Copier après la connexion

#Javascript篇

    <script type="text/javascript" src="aes.js"></script>
    <script type="text/javascript" src="pad-zeropadding.js"></script>
    <script type="text/javascript">
&#160;&#160;&#160;&#160;&#160;&#160;&#160; var data = "Test String";
&#160;&#160;&#160;&#160;&#160;&#160;&#160; var key&#160; = CryptoJS.enc.Latin1.parse(&#39;1234567812345678&#39;);
&#160;&#160;&#160;&#160;&#160;&#160;&#160; var iv&#160;&#160; = CryptoJS.enc.Latin1.parse(&#39;1234567812345678&#39;);

&#160;&#160;&#160;&#160;&#160;&#160;&#160; //加密
&#160;&#160;&#160;&#160;&#160;&#160;&#160; var encrypted = CryptoJS.AES.encrypt(data,key,{iv:iv,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.ZeroPadding});

&#160;&#160;&#160;&#160;&#160;&#160;&#160; document.write(encrypted.ciphertext);
&#160;&#160;&#160;&#160;&#160;&#160;&#160; document.write(&#39;<br/>&#39;);
&#160;&#160;&#160;&#160;&#160;&#160;&#160; document.write(encrypted.key);
&#160;&#160;&#160;&#160;&#160;&#160;&#160; document.write(&#39;<br/>&#39;);
&#160;&#160;&#160;&#160;&#160;&#160;&#160; document.write(encrypted.iv);
&#160;&#160;&#160;&#160;&#160;&#160;&#160; document.write(&#39;<br/>&#39;);
&#160;&#160;&#160;&#160;&#160;&#160;&#160; document.write(encrypted.salt);
&#160;&#160;&#160;&#160;&#160;&#160;&#160; document.write(&#39;<br/>&#39;);
&#160;&#160;&#160;&#160;&#160;&#160;&#160; document.write(encrypted);
&#160;&#160;&#160;&#160;&#160;&#160;&#160; document.write(&#39;<br/>&#39;);

&#160;&#160;&#160;&#160;&#160;&#160;&#160; //解密
&#160;&#160;&#160;&#160;&#160;&#160;&#160; var decrypted = CryptoJS.AES.decrypt(encrypted,key,{iv:iv,padding:CryptoJS.pad.ZeroPadding});
&#160;&#160;&#160;&#160;&#160;&#160;&#160; console.log(decrypted.toString(CryptoJS.enc.Utf8));
&#160;&#160;&#160; </script>
Copier après la connexion

# 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;
        }
    }
Copier après la connexion

# .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);
        }
    }
}
Copier après la connexion

跨语言加解密的要求是:AES/CBC/ZeroPadding 128位模式,key和iv一样,编码统一用utf-8。不支持ZeroPadding的就用NoPadding.

以上就介绍了PHP、Java、Net和Javascript的AES加密解密实现,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal