AES 加密解密 (javascript, java,php,c#)

释放双眼,带上耳机,听听看~!

** Javascript端代码:**


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
1<script src="crypto-js.min.js"></script>
2<script src="pad-zeropadding.js"></script>
3<script>
4var data = "Test String";
5var key  = CryptoJS.enc.Latin1.parse('1234567812345678');
6var iv   = CryptoJS.enc.Latin1.parse('1234567812345678');
7
8//加密
9var encrypted = CryptoJS.AES.encrypt(data,key,{iv:iv,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.ZeroPadding});
10
11document.write(encrypted.ciphertext);
12document.write('<br/>');
13document.write(encrypted.key);
14document.write('<br/>');
15document.write(encrypted.iv);
16document.write('<br/>');
17document.write(encrypted.salt);
18document.write('<br/>');
19document.write(encrypted);
20document.write('<br/>');
21
22
23//解密
24var decrypted = CryptoJS.AES.decrypt(encrypted,key,{iv:iv,padding:CryptoJS.pad.ZeroPadding});
25console.log(decrypted.toString(CryptoJS.enc.Utf8));
26</script>
27

Java端代码:

这里需要强调的就是Java的填充模式是NoPadding,用自己的编写的补零填充内容;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
11import javax.crypto.Cipher;
12import javax.crypto.spec.IvParameterSpec;
13import javax.crypto.spec.SecretKeySpec;
14import sun.misc.BASE64Decoder;
15
16public 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}
80

PHP端代码:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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);
8echo(base64_encode($encrypted));
9echo '<br/>';
10
11//解密
12$encryptedData = base64_decode("2fbwW9+8vPId2/foafZq6Q==");
13$decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $privateKey, $encryptedData, MCRYPT_MODE_CBC, $iv);
14echo($decrypted);
15?>
16

C#端代码:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Security.Cryptography;
6
7namespace 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}
59

 

给TA打赏
共{{data.count}}人
人已打赏
安全技术安全运维

Windows服务器如何发现被黑

2018-5-20 12:24:31

安全技术

bootstrap栅格系统自定义列

2021-12-21 16:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索