AES加解密

/**
 * 
 * @author csl 加密解密
 */
public class AesUtil
{

    /**
     * 密钥如超过16位,截至16位,不足16位,补/000至16位
     * 
     * @return 新密钥
     */
    public static String secureBytes(String key)
    {
        if (key.length() > 16)
        {
            key = key.substring(0, 16);
        }
        else if (key.length() < 16)
        {
            for (int i = (key.length() - 1); i < 15; i++)
            {
                key += "\000";
            }
        }
        return key;
    }
    
    /**
     * AES解密 用于数据库储存
     * 
     * @param sSrc
     * @return
     * @throws Exception
     */
    public static String decryptCode(String sSrc, String key)
    {
        
        String sKey = secureBytes(key);
        
        try
        {
            // 判断Key是否正确
            if (sKey == null)
            {
                return null;
            }
            // 判断Key是否为16位
            if (sKey.length() != 16)
            {
                sKey = secureBytes(sKey);
            }
            byte[] raw = sKey.getBytes("ASCII");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encrypted1 = hex2byte(sSrc);
            try
            {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original, "GBK");
                return originalString;
            }
            catch (Exception e)
            {
                return null;
            }
        }
        catch (Exception ex)
        {
            return null;
        }

    }
    
    /**
     * AES解密 用于数据库储存
     * 
     * @param sSrc
     * @return
     * @throws Exception
     */
    public static String decrypt(String sSrc, String key)
    {
        String sKey = secureBytes(key);
        try
        {
            // 判断Key是否正确
            if (sKey == null)
            {
                return null;
            }
            // 判断Key是否为16位
            if (sKey.length() != 16)
            {
                sKey = secureBytes(sKey);
            }
            byte[] raw = sKey.getBytes("ASCII");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encrypted1 = hex2byte(sSrc);
            try
            {
                byte[] original = cipher.doFinal(encrypted1);
                
                String originalString = new String(original, "UTF-8");
                // String originalString = new String(original, "GBK");
                
                return originalString;
            }
            catch (Exception e)
            {
                return null;
            }
        }
        catch (Exception ex)
        {
            return null;
        }
        
    }
    
    public static String encrypt4Contacts(String sSrc)
    {
        return sSrc;
    }
    
    /**
     * AES加密
     * 
     * @param sSrc
     * @return
     * @throws Exception
     */
    public static String encrypt(String sSrc, String key)
    {
        
        String sKey = secureBytes(key);
        try
        {
            if (sSrc == null || sKey == null)
            {
                return null;
            }
            // 判断Key是否为16位
            if (sKey.length() != 16)
            {
                sKey = secureBytes(sKey);
            }
            byte[] raw = sKey.getBytes("ASCII");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            byte[] encrypted = cipher.doFinal(sSrc.getBytes());
            return byte2hex(encrypted).toLowerCase();
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }
    
    /**
     * @param strhex
     * @return
     */
    public static byte[] hex2byte(String strhex)
    {
        if (strhex == null)
        {
            return null;
        }
        int l = strhex.length();
        if (l % 2 == 1)
        {
            return null;
        }
        byte[] b = new byte[l / 2];
        for (int i = 0; i != l / 2; i++)
        {
            b[i] = (byte)Integer.parseInt(strhex.substring(i * 2, i * 2 + 2), 16);
        }
        return b;
    }
    
    /**
     * @param b
     * @return
     */
    public static String byte2hex(byte[] b)
    {
        String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++)
        {
            stmp = (Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1)
            {
                hs = hs + "0" + stmp;
            }
            else
            {
                hs = hs + stmp;
            }
        }
        return hs.toUpperCase();
    }
      
    //编码方式  
    public static final String BM = "UTF-8";  
      
//    public static void main(String[] args) {
//    	String key = AesUtil.encrypt("vvgsgs", "1234");
//    	System.out.println(key);
//    }
    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MFC(Microsoft Foundation Classes)是微软提供的一种用于开发Windows应用程序的C++类库,而AES(Advanced Encryption Standard)是一种加密算法,能够对数据进行高强度的加密和解密。MFC AES加解密源码是指使用MFC类库实现AES算法的代码。 MFC AES加解密源码通常包含以下几个步骤: 1. 导入MFC类库:首先需要在工程中导入MFC类库,以便使用MFC提供的类和函数。 2. 导入AES算法库:接下来需要导入AES算法库,这个库中包含了实现AES加解密的函数和数据结构。 3. 初始化AES算法:在进行加解密前,需要先对AES算法进行初始化,这包括设置密钥、选择加解密模式等。 4. 加密数据:使用AES算法对待加密的数据进行加密操作,这通常包括将数据分组、执行轮数轮的变换,最后得到加密后的数据。 5. 解密数据:使用AES算法对加密后的数据进行解密操作,这与加密过程相反,也需要进行轮数轮的变换,最后得到解密后的数据。 6. 清理资源:在加解密完成后,需要清理AES算法的资源,包括释放密钥、清空缓冲区等。 MFC AES加解密源码通常采用C++编写,具体实现细节和函数调用方式会因不同的实现方式而有所不同。需要注意的是,由于AES算法属于对称加密算法,加解密所使用的密钥必须保密,否则会导致加密的数据受到威胁。 总之,MFC AES加解密源码是使用MFC类库实现对数据进行AES算法加解密的代码,通过正确调用相关函数和进行必要的初始化操作,可以实现对数据的加密和解密操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值