Java使用 使用AES加解密算法,加密模式:ECB,填充:Zeropadding

public class diaoduService {

        //Java中不支持Zeropadding填充,使用NoPadding处理,结果都是一样的
    private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/NoPadding";// 默认的加密算法

    /**
     * AES 加密操作
     *
     * @param content 待加密内容
     * @param password 加密密码
     * @return
     */
    public  String encrypt(String content, String password) {
        try {
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);// 创建密码器
            int blockSize = cipher.getBlockSize();
            byte[] byteContent = content.getBytes();
            int plaintextLength = byteContent.length;
            if (plaintextLength % blockSize != 0) {
                plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
            }
            byte[] plaintext = new byte[plaintextLength];
            System.arraycopy(byteContent, 0, plaintext, 0, byteContent.length);
            cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(password));// 初始化为加密模式的密码器
            return Base64.encodeBase64String(cipher.doFinal(plaintext));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    /**
     * AES 解密操作
     *
     * @param content
     * @param password
     * @return
     */
    public  String decrypt(String content, String password) {
        try {
            // 实例化
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, getSecretKey(password));// 初始化为加密模式的密码器
            // 执行操作
            byte[] result = cipher.doFinal(Base64.decodeBase64(content));// 解密
            return new String(result, StandardCharsets.UTF_8);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    /**
     * 生成加密秘钥
     *
     * @return
     */
    private static SecretKeySpec getSecretKey(final String password) {
        int len = password.getBytes().length;
        if (len % 16 != 0) {
            len = len + (16 - (len % 16));
        }
        byte[] newpass = new byte[len];
        System.arraycopy(password.getBytes(), 0, newpass, 0, password.getBytes().length);
        return new SecretKeySpec(newpass, "AES");
    }

    public static void main(String[] args) {
        try {
            String time=decrypt("","");
           System.out.println(time);
            
        }catch (Exception e){
            e.printStackTrace();
        }
       

    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是 AES 加密解密算法 ECB 模式的 C 语言实现。这个实现假设输入数据已经被填充AES 块大小的倍数(例如,128 位块大小的 AES 需要填充为 16 字节的倍数)。 ```c #include <stdio.h> #include <string.h> #include <stdlib.h> #include <stdint.h> #include "aes.h" void aes_encrypt_ecb(const uint8_t *key, const uint8_t *input, uint8_t *output, size_t length) { aes_context ctx; aes_setkey_enc(&ctx, key, 128); for (size_t i = 0; i < length; i += 16) { aes_encrypt(&ctx, input + i, output + i); } } void aes_decrypt_ecb(const uint8_t *key, const uint8_t *input, uint8_t *output, size_t length) { aes_context ctx; aes_setkey_dec(&ctx, key, 128); for (size_t i = 0; i < length; i += 16) { aes_decrypt(&ctx, input + i, output + i); } } int main() { // Example usage uint8_t key[16] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c}; uint8_t input[32] = {0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d, 0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34, 0x31, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d, 0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34}; uint8_t output[32]; aes_encrypt_ecb(key, input, output, 32); aes_decrypt_ecb(key, output, input, 32); printf("Input: "); for (int i = 0; i < 32; ++i) { printf("%02x ", input[i]); } printf("\n"); printf("Output: "); for (int i = 0; i < 32; ++i) { printf("%02x ", output[i]); } printf("\n"); return 0; } ``` 这个实现使用了 Crypto++ 库中的 AES 实现。在 `aes.h` 头文件中定义了 `aes_context` 结构体,用于保存 AES 加密算法的上下文信息。`aes_setkey_enc()` 和 `aes_setkey_dec()` 函数分别用于设置加密和解密密钥。`aes_encrypt()` 和 `aes_decrypt()` 函数分别用于加密和解密数据块。在 `aes_encrypt_ecb()` 和 `aes_decrypt_ecb()` 函数中,使用了 `for` 循环来对输入数据进行分块并进行加密或解密操作。最后,`main()` 函数演示了如何使用这些函数来加密和解密数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值