AES加解密

    public static void main(String[] args) throws Exception {
        // 原文:
        String message = "127.0.0.1:8379-" + System.currentTimeMillis();
        System.out.println("Message: " + message);
        // 128位密钥 = 16 bytes Key:
        byte[] key = "1234567890abcdef".getBytes(StandardCharsets.UTF_8);
        // 加密:
        long startTime = System.currentTimeMillis();
        String encrypted = encrypt(key, message);
        System.out.println((System.currentTimeMillis() - startTime) + " ,Encrypted: " + encrypt(key, message));
        // 解密:
        long startTime1 = System.currentTimeMillis();

        String decrypted = decrypt(key, encrypted);
        System.out.println((System.currentTimeMillis() - startTime1) + " ,Decrypted: " + decrypted);
    }
    
    // 加密:
    public static String encrypt(byte[] key, String inputStr) throws GeneralSecurityException {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
        // CBC模式需要生成一个16 bytes的initialization vector:
        SecureRandom sr = SecureRandom.getInstanceStrong();
        byte[] iv = sr.generateSeed(16);
        IvParameterSpec ivps = new IvParameterSpec(iv);
        cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivps);
        byte[] data = cipher.doFinal(inputStr.getBytes(StandardCharsets.UTF_8));
        // IV不需要保密,把IV和密文一起返回:
        return HexUtil.encodeHexStr(join(iv, data));
    }

    // 解密:
    public static String decrypt(byte[] key, String inputStr) throws GeneralSecurityException {
        byte[] input = HexUtil.decodeHex(inputStr);
        // 把input分割成IV和密文:
        byte[] iv = new byte[16];
        byte[] data = new byte[input.length - 16];
        System.arraycopy(input, 0, iv, 0, 16);
        System.arraycopy(input, 16, data, 0, data.length);
        // 解密:
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
        IvParameterSpec ivps = new IvParameterSpec(iv);
        cipher.init(Cipher.DECRYPT_MODE, keySpec, ivps);
        return new String(cipher.doFinal(data), StandardCharsets.UTF_8);
    }

    public static byte[] join(byte[] bs1, byte[] bs2) {
        byte[] r = new byte[bs1.length + bs2.length];
        System.arraycopy(bs1, 0, r, 0, bs1.length);
        System.arraycopy(bs2, 0, r, bs1.length, bs2.length);
        return r;
    }

  • 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、付费专栏及课程。

余额充值