JAVA密碼編譯演算法系列-AesEBC

來源:互聯網
上載者:User

標籤:ascii   ring   ati   字母   misc   mode   轉碼   digest   odi   

package ***;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.io.UnsupportedEncodingException;import java.security.MessageDigest;import javax.crypto.Cipher;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;public class AesEBC {    /*已確認    * 加密用的Key 可以用26個字母和數字組成    * 此處使用AES-128-CBC加密模式,key需要為16位。    */        private static String sKey="1234567890123456";        private static String ivParameter="1234567890123456";        private static AesEBC instance=null;        //private static         private AesEBC(){        }        public static AesEBC getInstance(){            if (instance==null)                instance= new AesEBC();            return instance;        }        // 加密        public String encrypt(String sSrc, String encodingFormat, String sKey, String ivParameter) throws Exception {            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");            byte[] raw = sKey.getBytes();            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);            byte[] encrypted = cipher.doFinal(sSrc.getBytes(encodingFormat));            return new BASE64Encoder().encode(encrypted);//此處使用BASE64做轉碼。    }        // 解密        public String decrypt(String sSrc, String encodingFormat, String sKey, String ivParameter) throws Exception {            try {                byte[] raw = sKey.getBytes("ASCII");                SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");                Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");                cipher.init(Cipher.DECRYPT_MODE, skeySpec);                byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);//先用base64解密                byte[] original = cipher.doFinal(encrypted1);                String originalString = new String(original,encodingFormat);                return originalString;            } catch (Exception ex) {                return null;            }    }        public static void main(String[] args) throws Exception {            // 需要加密的字串            String cSrc = "123456";            System.out.println("加密前的字串是:"+cSrc);            // 加密            String enString = AesEBC.getInstance().encrypt(cSrc,"utf-8",sKey,ivParameter);            System.out.println("加密後的字串是:"+ enString);            System.out.println("yXVUkR45PFz0UfpbDB8/ew==".equals(enString));            // 解密            String DeString = AesEBC.getInstance().decrypt(enString,"utf-8",sKey,ivParameter);            System.out.println("解密後的字串是:" + DeString);        }}

 

JAVA密碼編譯演算法系列-AesEBC

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.