17 AES对密钥key长度的优化

  1. 接上文,AES的密钥长度再底层定义了固定的长度,16,24,32.但是再实际生产情况下,可能会出现比较任意长度的密钥,如何处理?
  2. 在获取加解密对象的时候特殊处理:
import com.yutu.pwd.util.HexUtils;
import org.junit.jupiter.api.Test;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
/**
 * Aes加密
 */
public class AESTest {
    private static final String UTF8 = StandardCharsets.UTF_8.name();

    //加密算法名称
    private static final String ALGORITHM = "AES";
    //AES默认的长度只有16,24,32
    private static final String KEY = "12345678abcdefghss";

    @Test
    public void S() throws Exception{
        String str = "小汪学java";
        //DES加密
        String encrypt = encrypt(str);
        System.out.println("16进制 + Aes 加密后的字符串: " + encrypt);
        System.out.println("------------------------");
        String decrypt = decrypt(encrypt);
        System.out.println("16进制 + Aes 解密后的字符串: " + decrypt);
    }

    /**
     * des加密
     * @param text 待加密的内容
     * @return
     */
    private String encrypt(String text) throws Exception{
        Cipher cipher = getCipher2(Cipher.ENCRYPT_MODE, KEY);
        //加密
        byte[] encodedBytes = cipher.doFinal(text.getBytes(UTF8));
        return HexUtils.covertBytes2HexStr(encodedBytes);
    }

    /**
     * 解密
     * @param encodedStr 加密后的字符串
     * @return
     * @throws Exception
     */
    private String decrypt(String encodedStr) throws Exception{
        byte[] bytes = HexUtils.convertHex2Bytes(encodedStr);
        //Cipher cipher = Cipher.getInstance(ALGORITHM);
        Cipher cipher = getCipher2(Cipher.ENCRYPT_MODE, KEY);
        SecretKey secretKey = new SecretKeySpec(KEY.getBytes(UTF8), ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE,secretKey);//解密模式
        //获取解码后的字节数组
        byte[] decryptBytes = cipher.doFinal(bytes);
        return new String(decryptBytes,UTF8);
    }

    /**
     * 获取Cipher对象
     * @param type  加解密模式
     * @param seed  密钥key
     * @return
     */
    private Cipher getCipher(int type,String seed) throws Exception{
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        SecretKey secretKey = new SecretKeySpec(seed.getBytes(UTF8), ALGORITHM);
        cipher.init(type,secretKey);//解密模式
        return cipher;
    }

    /**
     * 不管传递的是多少位长度的密钥,最后都会生成指定长度的密钥
     * @param type
     * @param seed
     * @return
     * @throws Exception
     */
    private Cipher getCipher2(int type,String seed) throws Exception{
        Cipher cipher = Cipher.getInstance(ALGORITHM);

        //获取KeyGenerator对象,可以根据传入的key生成一个指定长度的key
        KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);

        SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");//此处是生成key的规则算法
        secureRandom.setSeed(seed.getBytes(UTF8));

        keyGenerator.init(128,secureRandom);

        //通过keyGenerator生成新的密钥
        SecretKey secretKey = keyGenerator.generateKey();
        //获取新密钥的字节数组
        byte[] encoded = secretKey.getEncoded();

        SecretKey secretKeySpec = new SecretKeySpec(encoded, ALGORITHM);
        cipher.init(type,secretKeySpec);//解密模式
        return cipher;
    }
}
### 使用 AutoGPTQ 库量化 Transformer 模型 为了使用 `AutoGPTQ` 对 Transformer 模型进行量化,可以遵循如下方法: 安装所需的依赖包是必要的操作。通过 pip 安装 `auto-gptq` 可以获取最新版本的库。 ```bash pip install auto-gptq ``` 加载预训练模型并应用 GPTQ (General-Purpose Tensor Quantization) 技术来减少模型大小和加速推理过程是一个常见的流程。下面展示了如何利用 `AutoGPTQForCausalLM` 类来进行这一工作[^1]。 ```python from transformers import AutoModelForCausalLM, AutoTokenizer from auto_gptq import AutoGPTQForCausalLM model_name_or_path = "facebook/opt-350m" quantized_model_dir = "./quantized_model" tokenizer = AutoTokenizer.from_pretrained(model_name_or_path) model = AutoModelForCausalLM.from_pretrained(model_name_or_path) # 加载已经量化的模型或者创建一个新的量化器对象用于量化未压缩过的模型 gptq_model = AutoGPTQForCausalLM.from_pretrained(quantized_model_dir, model=model, tokenizer=tokenizer) ``` 对于那些希望进一步优化其部署环境中的模型性能的人来说,`AutoGPTQ` 提供了多种配置选项来自定义量化参数,比如位宽(bit-width),这有助于平衡精度损失与运行效率之间的关系。 #### 注意事项 当处理特定硬件平台上的部署时,建议查阅官方文档以获得最佳实践指导和支持信息。此外,在实际应用场景之前应该充分测试经过量化的模型以确保满足预期的质量标准。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值