参考别人的代码写的aes加密,记录一下(AES,ECB模式,填充PKCS5Padding,数据块128位,偏移量无,以hex16进制输出)...

package org.jimmy.autosearch2019.test;

import java.security.SecureRandom;

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

public class TestAes2019052801 {

    public static void main(String[] args) {
        try {
            String content = "123456";
            String secretKey = "123456";
            byte[] encryptedContentBytes = encrypt(content, secretKey);
            String encryptedContent = parseBinaryToHexStr(encryptedContentBytes);
            System.out.println("加密前的文本:" + content);
            System.out.println("加密后的文本:" + encryptedContent);
            byte[] encryptedContentToBinaryStr = parseHexToBinaryStr(encryptedContent);
            byte[] decryptedContentBytes = decrypt(encryptedContentToBinaryStr, secretKey);
            String decryptedContent = new String(decryptedContentBytes);
            System.out.println("解密后的文本:" + decryptedContent);
        } catch(Exception e) { 
            e.printStackTrace();
        }
    }
    
    /**
     * @author ラピスラズリ(Dawn)
     * @date 2019年5月28日 下午2:56:42
     * @detail 16进制字符串转换2进制字符串
     */
    public static byte[] parseHexToBinaryStr(String hexStr) throws Exception {
        byte[] bytes = null;
        if(hexStr.length() < 1) {
            return bytes;
        }
        bytes = new byte[hexStr.length() / 2];
        for(int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            bytes[i] = (byte) (high * 16 + low);
        }
        return bytes;
    }
    
    /**
     * @author ラピスラズリ(Dawn)
     * @date 2019年5月28日 下午2:54:56
     * @detail 2进制字符串转换16进制字符串
     */
    public static String parseBinaryToHexStr(byte[] bytes) throws Exception {
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(bytes[i] & 0xff);
            if(hex.length() == 1) {
                hex = "0" + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }
    
    /**
     * @author ラピスラズリ(Dawn)
     * @date 2019年5月28日 下午3:30:33
     * @detail 解密
     */
    public static byte[] decrypt(byte[] content, String secretKey) throws Exception {
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(128, new SecureRandom(secretKey.getBytes()));
        SecretKey generatedSecretKey = keyGenerator.generateKey();
        byte[] encodedBytes = generatedSecretKey.getEncoded();
        SecretKeySpec secretKeySpec = new SecretKeySpec(encodedBytes, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        byte[] result = cipher.doFinal(content);  
        return result;
    }
    
    /**
     * @author ラピスラズリ(Dawn)
     * @date 2019年5月28日 下午2:55:25
     * @detail aes加密
     */
    public static byte[] encrypt(String content, String secretKey) throws Exception {
        // 创建AES的Key生产者
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        // 利用用户密码作为随机数初始化出
        // 128位的key生产者
        //加密没关系,SecureRandom是生成安全随机数序列,password.getBytes()是种子,
        //只要种子相同,序列就一样,所以解密只要有password就行
        keyGenerator.init(128, new SecureRandom(secretKey.getBytes()));
        // 根据用户密码,生成一个密钥
        SecretKey generatedSecretKey = keyGenerator.generateKey();
        // 返回基本编码格式的密钥,如果此密钥不支持编码,则返回
        byte[] encodedBytes = generatedSecretKey.getEncoded();
        // 转换为AES专用密钥
        SecretKeySpec secretKeySpec = new SecretKeySpec(encodedBytes, "AES");
        // 创建密码器
        Cipher cipher = Cipher.getInstance("AES");
        byte[] contentBytes = content.getBytes("utf-8");
        // 初始化为加密模式的密码器
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        //加密
        byte[] result = cipher.doFinal(contentBytes);
        return result;
    }

}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AES128 ECB PKCS5Padding算法是一种对称加密算法,具有较快的加密速度和较高的安全性。 在C代码中实现上述算法,我们需要使用相关的库函数。以OpenSSL为例,在C语言中可以使用OpenSSL提供的API进行AES128 ECB PKCS5Padding的加解密操作。 下面是一个简单的示例代码: ```c #include <stdio.h> #include <openssl/aes.h> int main() { // 原始数据 unsigned char plaintext[17] = "Hello, world!"; unsigned char ciphertext[16]; unsigned char recoveredtext[16]; // 密钥 unsigned char aes_key[16] = "1234567890123456"; AES_KEY key; // 设置加密密钥 if (AES_set_encrypt_key(aes_key, 128, &key) < 0) { printf("Error setting encryption key.\n"); return -1; } // 加密 AES_encrypt(plaintext, ciphertext, &key); // 解密 if (AES_set_decrypt_key(aes_key, 128, &key) < 0) { printf("Error setting decryption key.\n"); return -1; } AES_decrypt(ciphertext, recoveredtext, &key); printf("Plaintext: %s\n", plaintext); printf("Ciphertext: "); for (int i = 0; i < 16; i++) { printf("%02x ", ciphertext[i]); } printf("\n"); printf("Recovered text: %s\n", recoveredtext); return 0; } ``` 上述代码中,我们首先定义了原始数据(明文)`plaintext`,密钥`aes_key`以及存储加密后数据(密文)`ciphertext`和解密后数据(恢复文本)`recoveredtext`的数组。然后,使用`AES_set_encrypt_key`函数设置加密密钥,并调用`AES_encrypt`函数进行加密操作。再使用`AES_set_decrypt_key`函数设置解密密钥,并调用`AES_decrypt`函数进行解密操作。最后,打印输出明文、密文和恢复文本。 需要注意的是,ECB模式不适合处理大量数据加密,因为它没有使用初始化向量,相同的明文将会得到相同的密文,存在安全性隐患。而且ECB模式也没有提供数据完整性验证,因此在实际应用中,建议使用更为安全的加密模式
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值