Java implementation of DES encryption algorithm

Source: Internet
Author: User
Tags stringbuffer

 PackageCom.cs99lzzs.shop.util;ImportJava.math.BigInteger;ImportJava.security.Key;Importjava.security.MessageDigest;Importjava.security.NoSuchAlgorithmException;ImportJava.security.spec.AlgorithmParameterSpec;ImportJavax.crypto.Cipher;Importjavax.crypto.SecretKeyFactory;ImportJavax.crypto.spec.DESKeySpec;ImportJavax.crypto.spec.IvParameterSpec;Importorg.apache.commons.lang.StringUtils;/*** Encryption Algorithm * *@version$Id: Descbcsecurity.java, v 0.1 July 24, 2017 am 9:41:59 chenxu Exp $*/ Public classdescbcsecurity { Public Static FinalString algorithm_des = "Des/cbc/pkcs5padding"; /**     *      * @paramData *@paramKey *@return     * @throwsException*/     Public Staticstring encode (string data, string key)throwsException {if(Stringutils.isempty (key) | |stringutils.isempty (data)) {            return NULL; }        returnencode (Data.getbytes (), key); }    /*** des algorithm, encryption * *@paramData * String to encrypt *@paramKey * Encrypts the private key, the length cannot be less than 8 bits *@returnencrypted byte array, commonly combined with BASE64 encoding using *@throwscryptexception * Exception*/    Private StaticString Encode (byte[] data, String key)throwsException {Try{deskeyspec DKs=NewDeskeyspec (Key.getbytes ()); Secretkeyfactory keyfactory= Secretkeyfactory.getinstance ("DES"); //the length of the key cannot be less than 8 bit bytesKey Secretkey =Keyfactory.generatesecret (DKS); Cipher Cipher=cipher.getinstance (algorithm_des); Ivparameterspec IV=NewIvparameterspec (Key.getbytes ()); Algorithmparameterspec Paramspec=IV;            Cipher.init (Cipher.encrypt_mode, Secretkey, Paramspec); byte[] bytes =cipher.dofinal (data); returnAshex (bytes); } Catch(Exception e) {Throw NewException (e); }    }         Public StaticString MD5 (String input)throwsnosuchalgorithmexception {String result=input; if(Input! =NULL) {MessageDigest MD= Messagedigest.getinstance ("MD5");//or "SHA-1"md.update (Input.getbytes ()); BigInteger Hash=NewBigInteger (1, Md.digest ()); Result= Hash.tostring (16);  while(Result.length () < 32) {//Max for SHA-1result = "0" +result; }        }        returnresult; }         Public StaticString Ashex (bytebuf[]) {StringBuffer Strbuf=NewStringBuffer (Buf.length * 2); inti;  for(i = 0; i < buf.length; i++) {              if(((int) Buf[i] & 0xff) < 0x10) Strbuf.append ("0"); Strbuf.append (long.tostring (int) Buf[i] & 0xFF, 16)); }          returnstrbuf.tostring (); } }

Java implementation of DES encryption algorithm

Related Article

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.