DES演算法java實現,可以用在項目中

來源:互聯網
上載者:User

標籤:des演算法

    DES演算法為密碼體制中的對稱密碼體制,又被稱為美國資料加密標準,是1972年美國IBM公司研製的對稱密碼體制密碼編譯演算法。 明文按64位進行分組,密鑰長64位,密鑰事實上是56位參與DES運算(第8、16、24、32、40、48、56、64位是校正位, 使得每個密鑰都有奇數個1)分組後的明文組和56位的密鑰按位替代或交換的方法形成密文組的加密方法。


    DES演算法具有極高安全性,到目前為止,除了用窮舉搜尋法對DES演算法進行攻擊外,還沒有發現更有效辦法。而56位長的密鑰的窮舉空間為256,這意味著如果一台電腦的速度是每一秒鐘檢測一百萬個密鑰,則它搜尋完全部密鑰就需要將近2285年的時間,可見,這是難以實現的


    其入口參數有三個:key、data、mode。key為加密解密使用的密鑰,data為加密

des演算法結構解密的資料,mode為其工作模式。當模式為加密模式時,明文按照64位進行分組,形成明文組,key用於對資料加密,當模式為解密模式時,key用於對資料解密。實際運用中,密鑰只用到了64位中的56位,這樣才具有高的安全性。


1、演算法主程式類 SymmetricAlgorithm


package com.gary.test.ws.test;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

public class SymmetricAlgorithm {
    private String strKey = "&^%$*#@~";
    private String info;

    public SymmetricAlgorithm(String info) {
        this.info = info;
    }

    public SymmetricAlgorithm(String info, String strKey) {
        this.info = info;
        this.strKey = strKey;
    }

    private Key getKey() {
        byte[] keyBtye = this.strKey.getBytes();

        byte[] _keyByte = new byte[8];

        for (int i = 0; (i < keyBtye.length) && (i < _keyByte.length); i++) {
            _keyByte[i] = keyBtye[i];
        }

        return new SecretKeySpec(_keyByte, "DES");
    }

    public String desEncrypt() {
        return desEncrypt(this.info, "UTF-8");
    }

    public String desEncrypt(String origin, String encoding) {
        if ((origin == null) || (encoding == null))
            return null;
        try {
            return encrypt(origin.getBytes(encoding), "DES");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String desDecrypt() {
        return desDecrypt(this.info, "UTF-8");
    }

    public String desDecrypt(String ciperData, String encoding) {
        if ((ciperData == null) || (encoding == null)) {
            return null;
        }
        byte[] b = decrypt(EncryptHelper.hex2byte(ciperData), "DES");
        try {
            return new String(b, encoding);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    private String encrypt(byte[] data, String algorithm) {
        try {
            Key key = getKey();

            Cipher c1 = Cipher.getInstance(algorithm);
            c1.init(1, key);
            byte[] cipherByte = c1.doFinal(data);
            return EncryptHelper.byte2hex(cipherByte);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return null;
    }

    private byte[] decrypt(byte[] data, String algorithm) {
        try {
            Key key = getKey();
            Cipher c1 = Cipher.getInstance(algorithm);
            c1.init(2, key);
            return c1.doFinal(data);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        SymmetricAlgorithm s =new SymmetricAlgorithm("gp3adm","&^%$*#@~");//加密
        
        String sa_pwd = s.desEncrypt("gp3adm","UTF-8");
        
        System.out.println("加密後:"+sa_pwd);
        String pwd = "84DFA223A4521331";

       String password = (new SymmetricAlgorithm(pwd)).desDecrypt();// 解密
       System.out.println("解密後:"+password);

    }
}



2、助手類 EncryptHelper


package com.gary.test.ws.test;

public class EncryptHelper
{
  public static final String DEFAULT_ENCODING = "UTF-8";

  public static String byte2hex(byte[] bytes)
  {
    StringBuffer retString = new StringBuffer();
    for (int i = 0; i < bytes.length; i++) {
      retString.append(Integer.toHexString(256 + (bytes[i] & 0xFF)).substring(1));
    }
    return retString.toString().toUpperCase();
  }

  public static byte[] hex2byte(String hex)
  {
    byte[] bts = new byte[hex.length() / 2];
    for (int i = 0; i < bts.length; i++) {
      bts[i] = (byte)Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16);
    }
    return bts;
  }
}


DES演算法java實現,可以用在項目中

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.