JAVA 语言AES CBC模式加解密数据实现

   在多可文档系统中文件接口需要和其他系统实现用户统一登录,其他数据加密传输,要保障算法和数据的一致性    对系统接口使用有很大帮助。系统选择使用AES加密算法的CBC模式(128位密钥),实现各系统间加密数据的传输。多可提供各种语言的算法实现,以下是JAVA语言的具体算法实现(其他语言参考博主相关文章):
   加解密文本用16进制字符串表示,如果您需要base64编码请修改编码和解密部分即可。
 

package aes;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.URLEncoder;

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

public class hello {
    
    public static final String IV = "1234567890ABCDEF";
    public static final String KEY_ALGORITHM = "AES";
    public static final String CIPHER_ALGORITHM_CBC = "AES/CBC/NoPadding";
 
    public static String bytes2HexString(byte[] b) {
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            result.append(String.format("%02X",b[i]));
        }
        return result.toString();
    }
    
    public static byte[] aesCBCEncrypt(byte[] data, byte[] key) throws Exception {
        if(key.length != 16) {
            throw new RuntimeException("Invalid AES key length (must be 16 bytes)");
        } else { 
             Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC);
             int blockSize = cipher.getBlockSize();
             int length = data.length;
             // 计算需填充长度
             if (length % blockSize != 0) {
                 length = length + (blockSize - (length % blockSize));
             }
             byte[] plaintext = new byte[length];
             // 拷贝数据
             System.arraycopy(data, 0, plaintext, 0, data.length);
             cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, KEY_ALGORITHM), new IvParameterSpec(IV.getBytes()));
              
             return cipher.doFinal(plaintext);
        } 
    } 
     
    private static byte charToByte(char c) {   
        return (byte) "0123456789ABCDEF".indexOf(c);   
    }  
    
    public static byte[] hex2Bytes(String s) throws Exception{  
        int len = s.length();  
        if (len % 2 == 1){ 
            throw new RuntimeException("Invalid data");
        }
        char[] hexChars = s.toCharArray();   
        byte[] r = new byte[len / 2];   
        for (int i = 0; i < len / 2; i++) {   
            int pos = i * 2;   
            r[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));   
        }  
        
        return r;   
    }

    public static String aesCBCDecrypt(byte[] data, byte[] key) throws Exception {
        if(key.length != 16) {
            throw new RuntimeException("Invalid AES key length (must be 16 bytes)");
        } else { 
             Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC);  
             cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, KEY_ALGORITHM), new IvParameterSpec(IV.getBytes()));
          
             // 
             byte[] plaintext = cipher.doFinal(data);  
             return new String(plaintext, "utf-8").trim();  
        } 
    } 

    public static void main(String[] args) { 
         
        String key = "2018201820182018";
        String inStr = "多可文档管理系统"; 
        
        try
        {  
            //加密
            byte[] outBytes = aesCBCEncrypt(inStr.getBytes("utf-8"), key.getBytes("utf-8")); 
            String s = bytes2HexString(outBytes); 
            System.out.println(s);
            //解密
            String s2 = aesCBCDecrypt(hex2Bytes(s), key.getBytes("utf-8")); 
            System.out.println(s2);
            
        } catch (Exception e) {
            e.printStackTrace();
        }   
    }

}