JS front-end 3DES encryption background Java decryption

Source: Internet
Author: User

Import Java.security.key;import Java.security.securerandom;import Javax.crypto.cipher;import Javax.crypto.secretkeyfactory;import Javax.crypto.spec.desedekeyspec;import Sun.misc.base64decoder;import    Sun.misc.BASE64Encoder;        /** * DES encryption decryption * * @author * * */public class Desencrypt {key key; Public desencrypt (String str) {setkey (str);//Generate secret key} public Desencrypt () {Setkey ("abc123.*      Abc123.*abc123.*abc123.* ");            /** * Generates key based on parameter */public void Setkey (String strkey) {try {//Contrast des              Secretkeyfactory keyfactory = secretkeyfactory.getinstance ("Desede");          This.key = Keyfactory.generatesecret (New Desedekeyspec (Strkey.getbytes ("UTF8")); } catch (Exception e) {throw new RuntimeException ("Error Initializing Sqlmap class.          Cause: "+ e); }}/** * Encrypt string plaintext input, string ciphertext output */public StrinG Encrypt (String strming) {byte[] Bytemi = null;          byte[] byteming = null;          String strmi = "";          Base64encoder base64en = new Base64encoder ();              try {byteming = strming.getbytes ("UTF8");              Bytemi = This.getenccode (byteming);          Strmi = Base64en.encode (Bytemi); } catch (Exception e) {throw new RuntimeException ("Error Initializing Sqlmap class.          Cause: "+ e);              } finally {base64en = null;              byteming = null;          Bytemi = null;      } return Strmi; /** * Decrypted with string cipher input, string plaintext output * * @param strmi * @return */public String D          Ecrypt (String strmi) {Base64decoder base64de = new Base64decoder ();          byte[] byteming = null;          byte[] Bytemi = null;          String strming = "";             try {Bytemi = Base64de.decodebuffer (Strmi); byteming = This.getdescode (Bytemi);          strming = new String (byteming, "UTF8"); } catch (Exception e) {throw new RuntimeException ("Error Initializing Sqlmap class.          Cause: "+ e);              } finally {base64de = null;              byteming = null;          Bytemi = null;      } return strming; }/** * Encrypted with byte[] plaintext input, byte[] ciphertext output * * @param byteS * @return */private byte[] G          Etenccode (byte[] byteS) {byte[] Bytefina = null;          Cipher Cipher;              try {//contrast des cipher = cipher.getinstance ("Desede");              Cipher.init (Cipher.encrypt_mode, Key,securerandom.getinstance ("sha1prng"));          Bytefina = Cipher.dofinal (ByteS); } catch (Exception e) {throw new RuntimeException ("Error Initializing Sqlmap class.          Cause: "+ e);          } finally {cipher = NULL;} return Bytefina; }/** * decrypted with byte[] ciphertext input, byte[] Clear Text Output * * @param byted * @return */private byte[]          Getdescode (byte[] byted) {Cipher Cipher;          byte[] Bytefina = null;              try {//contrast des cipher = cipher.getinstance ("Desede");              Cipher.init (Cipher.decrypt_mode, Key,securerandom.getinstance ("sha1prng"));          Bytefina = Cipher.dofinal (byted); } catch (Exception e) {throw new RuntimeException ("Error Initializing Sqlmap class.          Cause: "+ e);          } finally {cipher = NULL;      } return Bytefina;            } public static void Main (String args[]) {desencrypt des = new desencrypt ();          String str1 = "asssaaasss.*/";          DES encryption String str2 = Des.encrypt (STR1);          Desencrypt des1 = new Desencrypt ();          String destr = Des1.decrypt (STR2); System.out.priNtln ("Ciphertext:" + str2);           Des decryption System.out.println ("Clear text:" + destr);   }    }


Des has a key length of 8 bits and a 3DES key length of 32 bits.

3DES key Material key factory, encryption algorithm is different.


Front-end JS encryption



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

JS front-end 3DES encryption background Java decryption

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.