Http://www.tuicool.com/articles/nERnqe
Http://www.cnblogs.com/AloneSword/p/3485912.html "Here is a detailed description of the specific symmetric and asymmetric algorithms"
AES encryption and decryption inside C #
C # code written in Visual Studio
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'll go Out"); public static string Encrypt (string toencrypt, String key, String iv) {byte[] Keyarray = Utf8encoding.utf
8.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.utf
8.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);
}
}
}
The encrypted and decrypted string can print successfully, but Console.WriteLine ("=3=="), then the output is gone, and the last output thread returns a value of 0, then there is no. C # do not understand, do not delve into, on the part of the implementation, is in line with the requirements. AES Encryption decryption in Java:
Java code, testing is also possible
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% BlockS
ize));
} 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;
The 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;
}
}
AES Encryption decryption of PHP
PHP Code, PHP, a lot of things are good, directly with the function, but the current knowledge of PHP fill mode is only zeropadding, so other languages can only follow it:
<?php
$privateKey = "1234567812345678";
$iv = "1234567812345678";
$data = "Test String";
Encryption
$encrypted = Mcrypt_encrypt (mcrypt_rijndael_128, $privateKey, $data, MCRYPT_MODE_CBC, $iv);
Echo ($encrypted);
Echo ' <br/> ';
Echo (Base64_encode ($encrypted));
Echo ' <br/> ';
Decryption
$encryptedData = Base64_decode ("2fbww9+8vpid2/foafzq6q==");
$decrypted = Mcrypt_decrypt (mcrypt_rijndael_128, $privateKey, $encryptedData, MCRYPT_MODE_CBC, $iv);
Echo ($decrypted);
? >
JavaScript under AES Plus decryption, try it, you need to download the kit in https://code.google.com/p/crypto-js/
<script type= "Text/javascript" src= "aes.js" ></script> <script type= "Text/javascript"
Pad-zeropadding.js "></script>
Import files, aes.js need to import the Crypto-js compressed Rollups folder under the Aes.js file, if the introduction of the Components folder under the Aes.js will be an error
<script type= "Text/javascript" > var data = "Test String";
var key = CryptoJS.enc.Latin1.parse (' 1234567812345678 ');
var IV = CryptoJS.enc.Latin1.parse (' 1234567812345678 '); Encrypt var encrypted = CryptoJS.AES.encrypt (data,key,{iv:iv,mode:cryptojs.mode.cbc,padding:cryptojs.pad.zeropadding}
);
document.write (Encrypted.ciphertext);
document.write (' <br/> ');
document.write (Encrypted.key);
document.write (' <br/> ');
document.write (ENCRYPTED.IV);
document.write (' <br/> ');
document.write (Encrypted.salt);
document.write (' <br/> ');
document.write (encrypted);
document.write (' <br/> ');
Decrypt var decrypted = CryptoJS.AES.decrypt (encrypted,key,{iv:iv,padding:cryptojs.pad.zeropadding});
Console.log (decrypted.tostring (CryptoJS.enc.Utf8)); </script>
Successfully decrypted, the last decrypted string strings in the browser console to see. AES Encryption decryption of Python
Finally add an AES under Python, you need to install the Python Crypto:
#!/usr/bin/env python
#-*-coding:utf-8-*-from
crypto.cipher import AES
import base64
PADDING = ' " c4/> #PADDING = '
pad_it = Lambda s:s+ (16-len (s)%16) *padding
key = ' 1234567812345678 '
IV = ' 12345678123456 The '
Source = ' Test String '
generator = aes.new (key, AES. MODE_CBC, iv)
crypt = Generator.encrypt (pad_it (source)
cryptedstr = Base64.b64encode (crypt)
print Cryptedstr
generator = aes.new (key, AES. MODE_CBC, iv)
recovery = generator.decrypt (crypt)
print Recovery.rstrip (PADDING)
Note that Python needs to be populated with ' yes ', and if the space is filled, the Python string will be different from the other languages. In addition, note that the generator used in the encryption, decryption time to be rebuilt and then decrypted, or decryption failed. The resulting string, in the Python console, sees the tail as a number of nul such things, so Recovery.rstrip (PADDING) to remove is the original string.
You can see that the intermediate result of AES encryption is the byte[] type, and direct new String (byte[]) will not see meaningful intermediate results, and here is Base64, because each language has such support. In the same language, there are bytestohexstring such a way.
Cross-language encryption and decryption requirements are: aes/cbc/zeropadding 128-bit mode, key and IV, the same code unified with Utf-8. If you don't support zeropadding, use nopadding.