md5加密 3-DES 加密 解密

android md5加密 3-DES 加密 解密

md5加密,该加密算法是单向加密,即加密的数据不能再通过解密还原。

3-DES加密,该加密算法是可逆的,解密方可以通过与加密方约定的密钥匙进行解密。

代码:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Base64;
import android.util.Log;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;


/**
 * android 加密 解密 汇总 sijienet.com
 */
public class MainActivity extends AppCompatActivity {

    public static final String tag = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String content="等待加密内容sijienet.com";
        String keys="123asd";
        String desEncrypt = get3DESEncrypt(content, keys);
        showLog("encode after=="+desEncrypt.trim().intern());
        showLog("decode after=="+get3DESDecrypt(desEncrypt, keys));


    }


    public String get3DESDecrypt(String src, String spkey) {
        String requestValue = "";
        try {
            //得到3-DES的密钥匙
            //URLDecoder.decodeTML控制码进行转义的过程
            String URLValue = getURLDecoderdecode(src);
            //进行3-DES加密后的内容进行BASE64编码
            byte[] base64DValue = Base64.decode(URLValue, Base64.DEFAULT);
            //要进行3-DES加密的内容在进行/"UTF-16LE/"取字节
            requestValue = deCrypt(base64DValue, spkey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return requestValue;
    }


    public String get3DESEncrypt(String src, String spkey) {
        String requestValue = "";
        try {
            //得到3-DES的密钥匙
            byte[] enKey = getEncryptKey(spkey);
            //要进行3-DES加密的内容在进行/"UTF-16LE/"取字节
            byte[] src2 = src.getBytes("UTF-16LE");
            //进行3-DES加密后的内容的字节
            byte[] encryptedData = Encrypt(src2, enKey);
            //进行3-DES加密后的内容进行BASE64编码
            String base64String = getBase64Encode(encryptedData);
            //BASE64编码去除换行符后
            String base64Encrypt = filter(base64String);
            //对BASE64编码中的HTML控制码进行转义的过程
            requestValue = getURLEncode(base64Encrypt);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return requestValue;
    }


    /**
     * 3-DES 解密
     */
    public String deCrypt(byte[] debase64, String spKey) {
        String strDe = null;
        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance("DESede");
            byte[] key = getEncryptKey(spKey);
            DESedeKeySpec dks = new DESedeKeySpec(key);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
            SecretKey sKey = keyFactory.generateSecret(dks);
            cipher.init(Cipher.DECRYPT_MODE, sKey);
            byte ciphertext[] = cipher.doFinal(debase64);
            strDe = new String(ciphertext, "UTF-16LE");
        } catch (Exception ex) {
            strDe = "";
            ex.printStackTrace();
        }
        return strDe;
    }


    /**
     * 对字符串进行URLDecoder.encode(strEncoding)编码
     */
    public String getURLEncode(String src) {
        String requestValue = "";
        try {
            requestValue = URLEncoder.encode(src, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return requestValue;
    }

    /**
     * 对字符串进行URLDecoder.encode(strEncoding)编码
     */
    public String getURLDecoderdecode(String src) {
        String requestValue = "";
        try {
            requestValue = URLDecoder.decode(src, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return requestValue;
    }


    /**
     * 去掉字符串的换行符号
     */
    private String filter(String str) {
        String output = null;
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < str.length(); i++) {
            int asc = str.charAt(i);
            if (asc != 10 && asc != 13)
                sb.append(str.subSequence(i, i + 1));
        }
        output = new String(sb);
        return output;
    }

    /**
     * base 64 encode
     */
    public String getBase64Encode(byte[] src) {
        String requestValue = "";
        try {
            requestValue = Base64.encodeToString(src, Base64.DEFAULT);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return requestValue;
    }


    /**
     * 3-DES加密
     */
    private byte[] Encrypt(byte[] src, byte[] enKey) {
        byte[] encryptedData = null;
        try {
            DESedeKeySpec dks = new DESedeKeySpec(enKey);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
            SecretKey key = keyFactory.generateSecret(dks);
            Cipher cipher = Cipher.getInstance("DESede");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            encryptedData = cipher.doFinal(src);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return encryptedData;
    }

    /**
     * 获取加密后密钥
     */
    private byte[] getEncryptKey(String spKey) {
        byte[] desKey = null;
        try {
            byte[] md5 = md5(spKey);
            desKey = new byte[24];
            int i;
            for (i = 0; i < md5.length && i < 24; i++) {
                desKey[i] = md5[i];
            }
            if (i < 24) {
                desKey[i] = 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return desKey;
    }


    /**
     * md5 加密 不能够解密 单向
     */
    private byte[] md5(String strSrc) {
        byte[] outs = null;
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            outs = md5.digest(strSrc.getBytes("UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return outs;
    }

    private void showLog(String msg) {
        Log.i(tag, msg + "");
    }
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值