AES加密和解密

AES 加密解密和 DES 加密解密代码一样,只需要修改加密算法就行,拷贝 ESC 代码

package com.leon.desaes;
import com.sun.org.apache.xml.internal.security.utils.Base64;

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

public class AesDemo {
    // DES加密算法,key的大小必须是8个字节

    public static void main(String[] args) throws Exception {
        String input ="硅谷";
        // AES加密算法,比较高级,所以key的大小必须是16个字节
        String key = "1234567812345678";

        String transformation = "AES"; // 9PQXVUIhaaQ=
        // 指定获取密钥的算法
        String algorithm = "AES";
        // 先测试加密,然后在测试解密
        String encryptDES = encryptDES(input, key, transformation, algorithm);
        System.out.println("加密:" + encryptDES);
        String s = dncryptDES(encryptDES, key, transformation, algorithm);
        System.out.println("解密:" + s);

    }

    /**
     * 使用DES加密数据
     *
     * @param input          : 原文
     * @param key            : 密钥(DES,密钥的长度必须是8个字节)
     * @param transformation : 获取Cipher对象的算法
     * @param algorithm      : 获取密钥的算法
     * @return : 密文
     * @throws Exception
     */
    private static String encryptDES(String input, String key, String transformation, String algorithm) throws Exception {
        // 获取加密对象
        Cipher cipher = Cipher.getInstance(transformation);
        // 创建加密规则
        // 第一个参数key的字节
        // 第二个参数表示加密算法
        SecretKeySpec sks = new SecretKeySpec(key.getBytes(), algorithm);
        // ENCRYPT_MODE:加密模式
        // DECRYPT_MODE: 解密模式
        // 初始化加密模式和算法
        cipher.init(Cipher.ENCRYPT_MODE,sks);
        // 加密
        byte[] bytes = cipher.doFinal(input.getBytes());

        // 输出加密后的数据
        String encode = Base64.encode(bytes);

        return encode;
    }

    /**
     * 使用DES解密
     *
     * @param input          : 密文
     * @param key            : 密钥
     * @param transformation : 获取Cipher对象的算法
     * @param algorithm      : 获取密钥的算法
     * @throws Exception
     * @return: 原文
     */
    private static String dncryptDES(String input, String key, String transformation, String algorithm) throws Exception {
        // 1,获取Cipher对象
        Cipher cipher = Cipher.getInstance(transformation);
        // 指定密钥规则
        SecretKeySpec sks = new SecretKeySpec(key.getBytes(), algorithm);
        cipher.init(Cipher.DECRYPT_MODE, sks);
        // 3. 解密
        byte[] bytes = cipher.doFinal(Base64.decode(input));

        return new String(bytes);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的uniapp中使用AES加密解密的示例代码: ```javascript // 加密 function encryptData(data, key, iv) { const cipher = uniCrypto.createCipheriv('aes-128-cbc', key, iv) let encrypted = cipher.update(data, 'utf8', 'base64') encrypted += cipher.final('base64') return encrypted } // 解密 function decryptData(data, key, iv) { const decipher = uniCrypto.createDecipheriv('aes-128-cbc', key, iv) let decrypted = decipher.update(data, 'base64', 'utf8') decrypted += decipher.final('utf8') return decrypted } // 使用示例 const data = 'Hello World!' const key = '1234567812345678' const iv = '8765432187654321' const encrypted = encryptData(data, key, iv) console.log('加密后的数据:', encrypted) const decrypted = decryptData(encrypted, key, iv) console.log('解密后的数据:', decrypted) ``` 在上面的代码中,`encryptData`函数接收要加密的数据、加密密钥和初始化向量(iv),并使用`uniCrypto.createCipheriv`方法创建一个AES加密器,然后使用`cipher.update`和`cipher.final`方法进行加密,最后返回加密后的数据。 `decryptData`函数接收要解密的数据、解密密钥和初始化向量(iv),并使用`uniCrypto.createDecipheriv`方法创建一个AES解密器,然后使用`decipher.update`和`decipher.final`方法进行解密,最后返回解密后的数据。 在使用时,只需将要加密的数据、加密密钥和初始化向量(iv)传递给`encryptData`函数即可获得加密后的数据,将加密后的数据、解密密钥和初始化向量(iv)传递给`decryptData`函数即可获得解密后的数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值