java 加密算法

MD5和SHA1

public static String SHA1(String decript) {
    try {
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        digest.update(decript.getBytes());
        byte messageDigest[] = digest.digest();
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < messageDigest.length; i++) {
            String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
            if (shaHex.length() < 2) {
                hexString.append(0);
            }
            hexString.append(shaHex);
        }
        return hexString.toString();

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

/**
 * MD5加密
 *
 * @param
 * @return 加密后的字符串
 */
public static String MD5(String s) {
    char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

    try {
        byte[] btInput = s.getBytes();
        // 获得MD5摘要算法的 MessageDigest 对象
        MessageDigest mdInst = MessageDigest.getInstance("MD5");
        // 使用指定的字节更新摘要
        mdInst.update(btInput);
        // 获得密文
        byte[] md = mdInst.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;
    }
}

3DESECB 加密&解密

package com.unicom.util.encrypt;

import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class ThreeDESECB {
    public static final String KEY_CHARSET_UTF8 = "UTF-8";
    public static final String ALGORITHM_MODE = "DESede";
    public static final String CIPHER_MODE = "DESede/ECB/PKCS5Padding";

    public static String encrypt(final String src, final String key) throws Exception {
        final DESedeKeySpec dks = new DESedeKeySpec(key.getBytes(KEY_CHARSET_UTF8));
        final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM_MODE);
        final SecretKey securekey = keyFactory.generateSecret(dks);

        final Cipher cipher = Cipher.getInstance(CIPHER_MODE);
        cipher.init(Cipher.ENCRYPT_MODE, securekey);
        final byte[] b = cipher.doFinal(src.getBytes());

        final BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(b).replaceAll("\r", "").replaceAll("\n", "");
    }

    public static String decrypt(final String src, final String key) throws Exception {
        final BASE64Decoder decoder = new BASE64Decoder();
        final byte[] bytesrc = decoder.decodeBuffer(src);
        final DESedeKeySpec dks = new DESedeKeySpec(key.getBytes(KEY_CHARSET_UTF8));
        final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM_MODE);
        final SecretKey securekey = keyFactory.generateSecret(dks);
        final Cipher cipher = Cipher.getInstance(CIPHER_MODE);
        cipher.init(Cipher.DECRYPT_MODE, securekey);
        final byte[] retByte = cipher.doFinal(bytesrc);
        return new String(retByte);
    }

    public static void main(String[] args) throws Exception {
        String key = "sdsdfdsynddgfgwabccvzaad";
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String sysTime = df.format(new Date());

        // 加密流程
        String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpYXQiOjE1NDc1NDUwNzgsImVudElkIjoiTFRDUzIwMTYwOTE4MDcifQ.eg_Agqw7nCa3A5LwEh6B1Yfp6U6TRrPqBCqh3MYLgjJasx1vu_tFPL9oq0BhqHR7N9GbbzDjyuX4XfdwaxvhG";
        String parmToken = token + sysTime;
        String sgin = encrypt(URLEncoder.encode(parmToken, "UTF-8"), key);
        System.out.println(sgin);

        // 解密流程
        String tele_decrypt = decrypt(sgin, key);
        System.out.println("模拟代码解密:" + tele_decrypt);
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值