zoukankan      html  css  js  c++  java
  • JAVA实现AES的加密和解密算法

    JAVA实现AES的加密和解密算法 加密模式为 AES-128-CBC

     1 import javax.crypto.Cipher;
     2 import javax.crypto.spec.IvParameterSpec;
     3 import javax.crypto.spec.SecretKeySpec;
     4 
     5 import sun.misc.BASE64Decoder;
     6 import sun.misc.BASE64Encoder;
     7 
     8 /**AES 是一种可逆加密算法,对用户的敏感信息加密处理
     9 * 对原始数据进行AES加密后,在进行Base64编码转化;
    10 */
    11 public class AESOperator {
    12 /*
    13 * 加密用的Key 可以用26个字母和数字组成
    14 * 此处使用AES-128-CBC加密模式,key需要为16位。
    15 */
    16 private String sKey=”0123456789abcdef”;
    17 private String ivParameter=”0123456789abcdef”;
    18 private static AESOperator instance=null;
    19 private AESOperator(){
    20 
    21 }
    22 public static AESOperator getInstance(){
    23 if (instance==null)
    24 instance= new AESOperator();
    25 return instance;
    26 }
    27 // 加密
    28 public String encrypt(String sSrc) throws Exception {
    29 Cipher cipher = Cipher.getInstance(“AES/CBC/PKCS5Padding”);
    30 byte[] raw = sKey.getBytes();
    31 SecretKeySpec skeySpec = new SecretKeySpec(raw, “AES”);
    32 IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());//使用CBC模式,需要一个向量iv,可增加加密算法的强度
    33 cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
    34 byte[] encrypted = cipher.doFinal(sSrc.getBytes(“utf-8″));
    35 return new BASE64Encoder().encode(encrypted);//此处使用BASE64做转码。
    36 }
    37 
    38 // 解密
    39 public String decrypt(String sSrc) throws Exception {
    40 try {
    41 byte[] raw = sKey.getBytes(“ASCII”);
    42 SecretKeySpec skeySpec = new SecretKeySpec(raw, “AES”);
    43 Cipher cipher = Cipher.getInstance(“AES/CBC/PKCS5Padding”);
    44 IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
    45 cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
    46 byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);//先用base64解密
    47 byte[] original = cipher.doFinal(encrypted1);
    48 String originalString = new String(original,”utf-8″);
    49 return originalString;
    50 } catch (Exception ex) {
    51 return null;
    52 }
    53 }
    54 
    55 public static void main(String[] args) throws Exception {
    56 // 需要加密的字串
    57 String cSrc = “我来自www.xxxx.com”;
    58 System.out.println(cSrc);
    59 // 加密
    60 long lStart = System.currentTimeMillis();
    61 String enString = AESOperator.getInstance().encrypt(cSrc);
    62 System.out.println(“加密后的字串是:” + enString);
    63 
    64 long lUseTime = System.currentTimeMillis() – lStart;
    65 System.out.println(“加密耗时:” + lUseTime + “毫秒”);
    66 // 解密
    67 lStart = System.currentTimeMillis();
    68 String DeString = AESOperator.getInstance().decrypt(enString);
    69 System.out.println(“解密后的字串是:” + DeString);
    70 lUseTime = System.currentTimeMillis() – lStart;
    71 System.out.println(“解密耗时:” + lUseTime + “毫秒”);
    72 }
    73 }
  • 相关阅读:
    VIJOS-P1340 拯救ice-cream(广搜+优先级队列)
    uva 11754 Code Feat
    uva11426 GCD Extreme(II)
    uvalive 4119 Always an Interger
    POJ 1442 Black Box 优先队列
    2014上海网络赛 HDU 5053 the Sum of Cube
    uvalive 4795 Paperweight
    uvalive 4589 Asteroids
    uvalive 4973 Ardenia
    DP——数字游戏
  • 原文地址:https://www.cnblogs.com/wdpnodecodes/p/7847355.html
Copyright © 2011-2022 走看看