Hello everyone:
today is free to study the next everyone is shouting AES encryption! I thought it was something profound! Finally understand, heart relieved! Share it with everyone! Des is actually: data Encryption Standard English abbreviation! is a cryptographic label just, AES is the Advanced Encryption Standard English abbreviation, everyone called abbreviation is accustomed to, make us these not contact person confused! My heart is really choking the panic!
This is in the collection of information when took an example practiced hand, but there is a problem is, put this code in the text to run with Cmd when there is garbled situation! Fortunately, the comments do not affect the effect! However, the program to really encounter such a situation, you have to transcode, because the text encoding is GBK, and I want to paste the code is UTF-8
Import Java.util.*;import java.io.*;p ublic class Test{private string encoderesult;//encoded string private string decoderesult;/ /decoded after string public Test () {}//encoding set public void Setencoderesult (String encoderesult) {char[] src = encoderesult.tochararray (); /Split the string to be encoded into a character array StringBuilder sb = new StringBuilder ();//Save the encoded character//divide the string to be encoded into a character array for (int i = 0; i< src.length; i++) {if ( Character.isdigit (Src[i])) {if (i! = src.length-1) {//Meet condition 3char[] temp = new Char[character.getnumericvalue (Src[i]) +1]; Arrays.fill (temp,src[i+1]); sb.append (temp); Sb.append ("_");} else{//satisfies the condition 2sb.append (Src[i]);}} else if (src[i] = = ' _ ')//satisfies the condition 5{sb.append ("\\UL"); Sb.append ("_");} else if (i = = src.length-1)//satisfies the condition 1, and to the end of the string {sb.append (Src[i]);} else//satisfies the condition 1 and is not at the end of the string {sb.append (src[i]); Sb.append ("_");}} This.encoderesult = new string (SB);//Create return encoded string}//get encoded result public string Getencoderesult () {return encoderesult;} Decode setting public void Setdecoderesult (String encoderesult) {string[] temp = Encoderesult.split ("_"); StringBuilder sb = new StringBuilder (); for (int i = 0; i&Lt Temp.length; i++) {if (Temp[i].equals ("\\UL")) Sb.append ("_"), Else if (Temp[i].length () >1) Sb.append (Temp[i].length ()-1); Elsesb.append (Temp[i]);} This.decoderesult = new String (SB);} Gets the decoded result public String Getdecoderesult () {return decoderesult;} public static void Main (string[] args) {System.out.println ("Enter the string to be encoded (end with Enter):");//There is a garbled problem here, The encoding in the text document is GBK and its encoding is UTF-8,CMD not recognized! String Source = ""; try{bufferedreader br = new BufferedReader (new InputStreamReader (system.in)); source = Br.readline ();} catch (IOException e) {e.printstacktrace ();} Test e = new test (); E.setencoderesult (source); System.out.println ("Post-coded results:" +e.getencoderesult ()); E.setdecoderesult (E.getencoderesult ()); SYSTEM.OUT.PRINTLN ("Decoded Result:" +e.getdecoderesult ());}}
<pre name= "code" class= "HTML" > enter the string to be encoded (end with Enter): abcdc123 encoded result: A_b_c_d_c_22_333_3 decoded result: abcdc123
"The simplest encryption"
1. Simple concept
PlainText: Pre-encryption information
Ciphertext: Information after a secret
Algorithms: Algorithms for encryption or decryption
Key: The key used by the algorithm
Example:
Add 1 123456 per digit and get 234567.
123456 of them are clear text,
234567 is the cipher,
The encryption key is 1,
The encryption algorithm is per-
<span style= "FONT-SIZE:18PX;" >import Java.security.securerandom;import Javax.crypto.cipher;import Javax.crypto.keygenerator;import Javax.crypto.spec.secretkeyspec;import sun.misc.base64decoder;import sun.misc.base64encoder;/** * Encoding Tool class * 1. Will byte[] Convert to various binary strings * 2.base encode * 3.base decode * 4. Get the MD5 value of byte[] * 5. Get string MD5 value * 6. BASE64 encryption with MD5 * 7.AES encryption * 8.AES encryption for B ASE Code * 9.AES decryption * 10. Decrypt Base Code AES * @author uikoo9 * @version 0.0.7.20140601 */public class Test {public stat IC void Main (string[] args) throws Exception {String content = "I love You, my Motherland"; System.out.println ("Before encryption:" + content); String key = "123456"; SYSTEM.OUT.PRINTLN ("Encryption key and decryption key:" + key); String encrypt = aesencrypt (content, key); SYSTEM.OUT.PRINTLN ("After encryption:" + encrypt); String decrypt = Aesdecrypt (encrypt, key); System.out.println ("After decryption:" + decrypt);} /** * AES Encryption is base code * @param content to be encrypted * @param encryptkey encryption Key * @return Encrypted base code * @throws Exception * /public static string Aesencrypt (string content, StriNg Encryptkey) throws Exception {return Base64Encode (aesencrypttobytes (content, Encryptkey));} /** * AES Encryption * @param content to be encrypted * @param encryptkey encryption Key * @return encrypted byte[] * @throws Exception */public static byte [] aesencrypttobytes (String content, String Encryptkey) throws Exception {Keygenerator KGen = keygenerator.getinstance (" AES "); Kgen.init (+, New SecureRandom (Encryptkey.getbytes ())); Cipher Cipher = cipher.getinstance ("AES"); Cipher.init (Cipher.encrypt_mode, New Secretkeyspec (Kgen.generatekey (). Getencoded (), "AES"), Return Cipher.dofinal (Content.getbytes ("Utf-8")); /** * Base encode * @param bytes to encode byte[] * @return encoded base code */public static String base64encode (byte[] bytes {return new Base64encoder (). Encode (bytes);} /** * Decrypts base code AES * @param encryptstr to decrypt base code * @param decryptkey decryption Key * @return decrypted String * @throws EXC Eption */public static string Aesdecrypt (String encryptstr, String decryptkey) throws Exception {return aesdecryptbybytes (base64dEcode (ENCRYPTSTR), Decryptkey);} /** * AES Decryption * @param encryptbytes to decrypt byte[] * @param decryptkey decryption Key * @return decrypted String * @throws Exception */public St atic string Aesdecryptbybytes (byte[] encryptbytes, String decryptkey) throws Exception {Keygenerator KGen = keygenerator. getinstance ("AES"); Kgen.init (+, New SecureRandom (Decryptkey.getbytes ())); Cipher Cipher = cipher.getinstance ("AES"); Cipher.init (Cipher.decrypt_mode, New Secretkeyspec (Kgen.generatekey (). Getencoded (), "AES")); byte[] decryptbytes = cipher.dofinal (encryptbytes); return new String (decryptbytes);} /** * Base decode * @param base64code to decode base code * @return Decoded byte[] * @throws Exception */public static byte[] Base64decode (String base64code) throws Exception{return new Base64decoder (). Decodebuffer (Base64code);}} </span><span style= "FONT-SIZE:14PX;" ></span>
Before encryption: I love you, motherland encryption key and decryption key: 123456 after encryption: dxybz5ep/qcxggorqkqhxmttuwa7aiui4zc39exjtoc= after decryption: I love you, my Motherland
here is the method of MD5 encryption
Import java.security.messagedigest;import sun.misc.base64encoder;/** * Encoding Tool class * 1. Convert byte[] to various binary strings * 2.base encode * 3. Base decode * 4. Gets the MD5 value of byte[] * 5. Gets the string MD5 value * 6. BASE64 encryption with MD5 * 7.AES encryption * 8.AES encryption for Base code * 9.AES Decryption * 10. Base 6 4 Code AES Decryption * @author uikoo9 * @version 0.0.7.20140601 */public class Test {public static void main (string[] args) throws Exception {String content = "I love You, my Motherland"; System.out.println ("Before encryption:" + content); String encrypt = md5encrypt (content); System.out.println ("MD5 after encryption:" + Encrypt);} /** * Combine base64 to implement MD5 encryption * @param msg to encrypt String * @return get MD5 to base64 * @throws Exception */public static string Md5encrypt (S Tring msg) throws Exception{return Base64Encode (MD5 (msg));} /** * Get string MD5 value * @param MSG * @return MD5 * @throws Exception */public static byte[] MD5 (String msg) throws Exception {R Eturn Md5jiami (Msg.getbytes ());} /** * Base encode * @param bytes to encode byte[] * @return encoded base code */public static String base64encode (byte[] bytes ) {return new Base64encoDer (). Encode (bytes);} /** * Get byte[] MD5 value * @param bytes byte[] * @return MD5 * @throws Exception */public static byte[] Md5jiami (byte[] bytes) Throws Exception {MessageDigest MD = messagedigest.getinstance ("MD5"); md.update (bytes); return Md.digest ();}}
Before encryption: I love you, Motherland MD5 after encryption: 56txrq9eyebsikyssfprrq==
find this MD5 encrypted, decryption method not found! The Apprentice is not fine! Can't restore!
Test a few small examples, summed up a sentence, that is, the algorithm is the two sides of their own definition, is a criterion, according to the code to program on it!
Finally understand what is encrypted, so happy Ah!
The two questions reserved for This code also ask everyone to help solve the AH! 1. Garbled problem 2.MD5 the decryption Ah!
If you are still interested in the study, please refer to this document!
http://zh.wikipedia.org/wiki/Advanced Encryption Standard #addroundkey.e6.ad.a5.e9.aa.a4
Des/aes/md5 Encryption method