加密 

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

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

public class APDes3 {
   
    /**
     * 加密String明文输入,经过BASE64编码String密文输出
     *
     * @param text,keystr,ivstr
     * @return
     */
    public static String encrypt(String text, String keystr,String ivstr) throws Exception
    {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        byte[] keyBytes= new byte[16];
        byte[] ivBytes = new byte[16];
       
        byte[] b= keystr.getBytes("UTF-8");
        byte[] v = ivstr.getBytes("UTF-8");
       
        int len= b.length;
        int len2 = v.length;
       
        if (len > keyBytes.length) len = keyBytes.length;
        if (len2> ivBytes.length) len2 = ivBytes.length;
       
        System.arraycopy(b, 0, keyBytes, 0, len);
        System.arraycopy(v,0,ivBytes,0,len2);
       
        SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
        IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
        cipher.init(Cipher.ENCRYPT_MODE,keySpec,ivSpec);
        BASE64Encoder decoder = new BASE64Encoder();
        byte [] results = cipher.doFinal(text.getBytes("UTF8"));
        return decoder.encode(results);
    }
   
    /**
     * 解密 以BASE64形式String密文输入,String明文输出
     *
     * @param text,keystr,ivstr
     * @return
     */
    public static String decrypt(String text, String keystr,String ivstr) throws Exception
    {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        byte[] keyBytes= new byte[16];
        byte[] ivBytes = new byte[16];
       
        byte[] b= keystr.getBytes("UTF-8");
        byte[] v = ivstr.getBytes("UTF-8");
       
        int len= b.length;
        int len2 = v.length;
       
        if (len > keyBytes.length) len = keyBytes.length;
        if (len2> ivBytes.length) len2 = ivBytes.length;
       
        System.arraycopy(b, 0, keyBytes, 0, len);
        System.arraycopy(v,0,ivBytes,0,len2);
       
        SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
        IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
        cipher.init(Cipher.DECRYPT_MODE,keySpec,ivSpec);
        BASE64Decoder decoder = new BASE64Decoder();
        byte [] results = cipher.doFinal(decoder.decodeBuffer(text));
        return new String(results,"UTF-8");
    }
    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        String text = "Account =aqtest201,Password =test4321,NewPassword =test4321;";
        String key = "ITMSYNlCKMTRUDFJ";
        String ivStr = "12345678";
        String strenc = APDes3.encrypt(text,key,ivStr);//加密
        System.out.println("strenc is :" + strenc);
        String strDes = APDes3.decrypt(strenc,key,ivStr);//解密
        System.out.println("strDes is :" + strDes);
        strenc = java.net.URLEncoder.encode(strenc);
        System.out.println("URLEncoder.encode(data) :" +strenc);
        strenc = java.net.URLDecoder.decode(strenc);
        System.out.println("URLDecoder.decoder(data) :" +strenc);

    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值