java简单的加密解密,加密无特殊字符串

1.直接上代码

package com.myerong.cosumer.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

import org.apache.log4j.Logger;
import org.apache.commons.codec.binary.Hex;

/**
* @ClassName: SecUtil 
* @Description: 加密 解密 数据库信息 
* @author lhy
* @date 2018年6月6日 上午9:36:23 
* @version V1.0
 */
public class SecUtil  {

    private static final Logger logger = Logger.getLogger(SecUtil.class.getName());

    /**
     * 自定义 KEY
     */
    private static byte[] keybytes = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x50,
            0x37, 0x38, 0x39, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46 };



    public static void main(String[] args) {
        BufferedReader reader;
        try {
            String st = "";
            do{
                if("".equals(st)) {
                    logger.info("AES加密与解密操作:");
                    logger.info("\"E\":加密 \t\"D\":解密\t\t\"Q\":退出");
                    logger.info("请输入操作代码:");
                }
                reader = new BufferedReader(new InputStreamReader(System.in));
                st = reader.readLine();
                if("E".equalsIgnoreCase(st)) {
                    logger.info("请输入待加密字符串:");
                    st = reader.readLine();
                    if(!"".equals(st.trim())) {
                        logger.info("加密前:" + st.trim());
                        logger.info("加密后:" + encrypt(st.trim()) + "\n\n");
                    }
                    st = "";
                }else if("D".equalsIgnoreCase(st)) {
                    logger.info("请输入待解密字符串:");
                    st = reader.readLine();
                    if(!"".equals(st.trim())) {
                        logger.info("解密前:" + st.trim());
                        logger.info("解密后:" + decrypt(st.trim()) + "\n\n");
                    }
                    st = "";
                }
            } while(!st.equalsIgnoreCase("Q"));
        } catch (Exception e) {
            logger.error(e);
        }
    }




    /** 
    * @Title: encrypt 
    * @author yunlin.liu
    * @Description: 加密
    * @param @param value
    * @param @return    设定文件 
    * @return String    返回类型 
    * @throws 
    */
    public static String encrypt(String value) {

        String s = null;

        int mode = Cipher.ENCRYPT_MODE;

        try {
            Cipher cipher = initCipher(mode);

            byte[] outBytes = cipher.doFinal(value.getBytes());

            s = String.valueOf(Hex.encodeHex(outBytes));
        } catch (Exception e) {
            logger.error(e);
        }

        return s;
    }


    /** 
    * @Title: decrypt 
    * @author yunlin.liu
    * @Description: 解密 
    * @param @param value
    * @param @return    设定文件 
    * @return String    返回类型 
    * @throws 
    */
    public static String decrypt(String value) {
        String s = null;

        int mode = Cipher.DECRYPT_MODE;

        try {
            Cipher cipher = initCipher(mode);

            byte[] outBytes = cipher
                    .doFinal(Hex.decodeHex(value.toCharArray()));

            s = new String(outBytes);
        } catch (Exception e) {
            logger.error(e);
        }

        return s;
    }


    /** 
    * @Title: initCipher 
    * @author yunlin.liu
    * @Description: 初始化密码
    * @param @param mode
    * @param @return
    * @param @throws NoSuchAlgorithmException
    * @param @throws NoSuchPaddingException
    * @param @throws InvalidKeyException    设定文件 
    * @return Cipher    返回类型 
    * @throws 
    */
    private static Cipher initCipher(int mode) throws NoSuchAlgorithmException,
            NoSuchPaddingException, InvalidKeyException {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        Key key = new SecretKeySpec(keybytes, "AES");
        cipher.init(mode, key);
        return cipher;
    }
}

2.运行测试看图

这里写图片描述
这里写图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值