采用AZDG加密方式進(jìn)行加密

Azerbaijan Development Group(AzDG)開發(fā)的可逆加密算法 AzDGCrypt 對用戶資料進(jìn)行加密。如提供正確的私有密匙,可通過本加密算法對數(shù)據(jù)進(jìn)行加密及解密。

格式化代碼:http://tool.oschina.net/codeformat/java



import java.io.IOException;

import java.security.MessageDigest;


public class Azdg {
    public static void main(String[] args) {
        Azdg azdg = new Azdg();

        System.out.println(System.currentTimeMillis() / 1000);

        String clearText = "no=sh0105&time=12319399372223`112r3r223"; //明文

        String cipherText = ""; //密文

        String cleanText = ""; //解密后的

        String key = "1234567890"; //密鑰,可自定義

        System.out.println("明文為:" + clearText);

        cipherText = azdg.encrypt(clearText, key);

        System.out.println("加密后為:" + cipherText);

        cleanText = azdg.decrypt(cipherText, key);

        System.out.println("解密后為:" + cleanText);
    }

    /**

    * 加密算法

    * */
    public String encrypt(String txt, String key) {
        String encrypt_key = "0f9cfb7a9acced8a4167ea8006ccd098";

        int ctr = 0;

        String tmp = "";

        int i;

        for (i = 0; i < txt.length(); i++) {
            ctr = (ctr == encrypt_key.length()) ? 0 : ctr;

            tmp = tmp + encrypt_key.charAt(ctr) +
                (char) (txt.charAt(i) ^ encrypt_key.charAt(ctr));

            ctr++;
        }

        return base64_encode(key(tmp, key));
    }

    /**

    * 解密算法

    */
    public String decrypt(String cipherText, String key) {
        // base64解碼
        cipherText = base64_decode(cipherText);

        cipherText = key(cipherText, key);

        String tmp = "";

        for (int i = 0; i < cipherText.length(); i++) {
            int c = cipherText.charAt(i) ^ cipherText.charAt(i + 1);

            String x = "" + (char) c;

            tmp += x;

            i++;
        }

        return tmp;
    }

    public String key(String txt, String encrypt_key) {
        encrypt_key = strMD5(encrypt_key);

        int ctr = 0;

        String tmp = "";

        for (int i = 0; i < txt.length(); i++) {
            ctr = (ctr == encrypt_key.length()) ? 0 : ctr;

            int c = txt.charAt(i) ^ encrypt_key.charAt(ctr);

            String x = "" + (char) c;

            tmp = tmp + x;

            ctr++;
        }

        return tmp;
    }

    public String base64_encode(String str) {
        return new sun.misc.BASE64Encoder().encode(str.getBytes());
    }

    public String base64_decode(String str) {
        sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();

        if (str == null) {

            return null;
        }

        try {
            return new String(decoder.decodeBuffer(str));
        } catch (IOException e) {
            e.printStackTrace();

            return null;
        }
    }

    public static final String strMD5(String s) {
        char[] hexDigits = {
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                
                'a', 'b', 'c', 'd', 'e', 'f'
            };

        try {
            byte[] strTemp = s.getBytes();

            MessageDigest mdTemp = MessageDigest.getInstance("MD5");

            mdTemp.update(strTemp);

            byte[] md = mdTemp.digest();

            int j = md.length;

            char[] str = new char[j * 2];

            int k = 0;

            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];

                str[k++] = hexDigits[byte0 >>> 4 & 0xf];

                str[k++] = hexDigits[byte0 & 0xf];
            }

            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}

測試效果:

1561106159
明文為:jxtyfrc哇O(∩_∩)O~
加密后為:VWJWKV98BXkAOFB0BmdT5ZOAABQDeVLiibIAXVPiibtXdVlGWHk=
解密后為:jxtyfrc哇O(∩_∩)O~

如果:sun.misc.BASE64Encoder()提示錯誤找不到可以,嘗試刪除JRE System Libraries并重新添加解決,在Java Build Path窗口,點擊第三個選項卡Libraries,選擇JRE System Libraries。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容