Java code:
Import Javax.crypto.Cipher;
Import Javax.crypto.spec.SecretKeySpec;
Import org.apache.commons.codec.binary.Base64; /** * * @author Administrator */public class AES {//Encrypt public static string Encrypt (String ssrc, String
SKey) throws Exception {if (SKey = = null) {System.out.print ("key is null null");
return null;
//Determine if key is 16-bit if (Skey.length ()!=) {System.out.print ("key length is not 16 bits");
return null;
} byte[] Raw = skey.getbytes ("Utf-8");
Secretkeyspec Skeyspec = new Secretkeyspec (Raw, "AES"); Cipher Cipher = cipher.getinstance ("aes/ecb/pkcs5padding");//"algorithm/Mode/complement Method" Cipher.init (Cipher.encrypt_mode, Skeyspe
c);
Byte[] encrypted = cipher.dofinal (Ssrc.getbytes ("Utf-8"));
return new Base64 (). encodetostring (encrypted);//Here use BASE64 to do the transcoding function, at the same time can play the role of 2 times encryption. }//Decrypt public static string Decrypt (String ssrc, String sKey) throws Exception{try {//judge whether the key is correct if (SKey = = null) {System.out.print ("key is null null");
return null;
//Determine if key is 16-bit if (Skey.length ()!=) {System.out.print ("key length is not 16 bits");
return null;
} byte[] Raw = skey.getbytes ("Utf-8");
Secretkeyspec Skeyspec = new Secretkeyspec (Raw, "AES");
Cipher Cipher = cipher.getinstance ("aes/ecb/pkcs5padding");
Cipher.init (Cipher.decrypt_mode, Skeyspec); byte[] encrypted1 = new Base64 (). Decode (SSRC);//First Decrypt try {base64] byte[= Original
NAL (encrypted1);
String originalstring = new String (Original, "Utf-8");
return originalstring;
catch (Exception e) {System.out.println (e.tostring ());
return null; } catch (Exception ex) {SYstem.out.println (Ex.tostring ());
return null; } public static void Main (string[] args) throws Exception {/* * use AES-128-ECB encryption mode here, key needs to be 16
Bit
*/String Ckey = "1234567890123456";
Strings that need to be encrypted the regulatory commission = "Www.gowhere.so";
SYSTEM.OUT.PRINTLN (Regulatory Commission); Encrypted String enstring = AES.
Encrypt (Regulatory Commission, Ckey);
System.out.println ("Encrypted string is:" + enstring); Decrypt String destring = AES.
Decrypt (enstring, Ckey);
System.out.println ("Decrypted string is:" + destring); }
}
C # code
<summary>///AES Encryption///</summary>///<param name= "text" > Encrypted character </PARAM&G
T <param name= "password" > encrypted password </param>///<param name= "IV" > key </param>///<ret urns></returns> public static string Encrypt (String toencrypt,string key) {byte[] Keyar
Ray = UTF8Encoding.UTF8.GetBytes (key);
byte[] Toencryptarray = UTF8Encoding.UTF8.GetBytes (Toencrypt);
RijndaelManaged Rdel = new RijndaelManaged ();
Rdel.key = Keyarray;
Rdel.mode = CIPHERMODE.ECB;
rdel.padding = PADDINGMODE.PKCS7;
ICryptoTransform ctransform = Rdel.createencryptor ();
byte[] Resultarray = Ctransform.transformfinalblock (toencryptarray, 0, toencryptarray.length);
Return convert.tobase64string (resultarray, 0, resultarray.length);
}///<summary>///AES decryption///</summary> <param name= "text" ></param>///<param name= "password" ></param>///<p Aram Name= "IV" ></param>///<returns></returns> public static string Decrypt (String ToD
Ecrypt,string key) {byte[] Keyarray = UTF8Encoding.UTF8.GetBytes (key);
byte[] Toencryptarray = convert.frombase64string (Todecrypt);
RijndaelManaged Rdel = new RijndaelManaged ();
Rdel.key = Keyarray;
Rdel.mode = CIPHERMODE.ECB;
rdel.padding = PADDINGMODE.PKCS7;
ICryptoTransform ctransform = Rdel.createdecryptor ();
byte[] Resultarray = Ctransform.transformfinalblock (toencryptarray, 0, toencryptarray.length);
Return UTF8Encoding.UTF8.GetString (resultarray); }
There is another article:
https://blog.csdn.net/softwave/article/details/53939824