通过自定义的key进行加密解密

41 篇文章 1 订阅

1.依赖

     <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcprov-jdk15on</artifactId>
            <version>1.60</version>
        </dependency>

2.工具类

package com.zykj.zycx.utils;


/*import org.apache.commons.net.util.Base64;*/
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * @author lc
 * @version 1.0
 * @date 2022/3/21 10:57
 */
public class AESUtil {
    static Logger logger = LoggerFactory.getLogger(AESUtil.class);
    // 密钥
    public static String key = "AD42F6697B035B7580E4FEF93BE20BAD"; // 32位
    private static String charset = "utf-8";
    // 偏移量
    private static int offset = 16;
    // 加密器类型:加密算法为AES,加密模式为CBC,补码方式为PKCS5Padding
    private static String transformation = "AES/CBC/PKCS5Padding";
    // 算法类型:用于指定生成AES的密钥
    private static String algorithm = "AES";



    /**
     * 加密
     *
     * @param content
     * @return
     */
    public static String encrypt(String content) {
        return encrypt(content, key);
    }

    /**
     * 解密
     *
     * @param content
     * @return
     */
    public static String decrypt(String content) {
        return decrypt(content, key);
    }

    /**
     * 加密
     *
     * @param content 需要加密的内容
     * @param key     加密密码(也可以自己直接用这个)
     * @return
     */
    public static String encrypt(String content, String key) {
        try {
            //构造密钥
            SecretKeySpec skey = new SecretKeySpec(key.getBytes(), algorithm);
            //创建初始向量iv用于指定密钥偏移量(可自行指定但必须为128位),因为AES是分组加密,下一组的iv就用上一组加密的密文来充当
            IvParameterSpec iv = new IvParameterSpec(key.getBytes(), 0, offset);
            //创建AES加密器
            Cipher cipher = Cipher.getInstance(transformation);
            byte[] byteContent = content.getBytes(charset);
            //使用加密器的加密模式
            cipher.init(Cipher.ENCRYPT_MODE, skey, iv);
            // 加密
            byte[] result = cipher.doFinal(byteContent);
            //使用BASE64对加密后的二进制数组进行编码
            return new Base64().encodeToString(result);
        } catch (Exception e) {
            logger.info("", e);
        }
        return null;
    }

    /**
     * AES(256)解密
     *
     * @param content 待解密内容
     * @param key     解密密钥
     * @return 解密之后
     * @throws Exception
     */
    public static String decrypt(String content, String key) {
        try {

            SecretKeySpec skey = new SecretKeySpec(key.getBytes(), algorithm);
            IvParameterSpec iv = new IvParameterSpec(key.getBytes(), 0, offset);
            Cipher cipher = Cipher.getInstance(transformation);
            //解密时使用加密器的解密模式
            cipher.init(Cipher.DECRYPT_MODE, skey, iv);// 初始化
            byte[] result = cipher.doFinal(new Base64().decode(content));
            return new String(result); // 解密
        } catch (Exception e) {
            logger.info("解密异常:", e);
        }
        return null;
    }

    // 通过秘钥判断code是否正确
    public static boolean checkCode(String code, String key) {
        String decrypt = decrypt(code, key);
        if (decrypt != null) {
            return true;
        }
        return false;
    }

    // 随机生成32位字符串
    public static String generate32Str() {
        return RandomUtils.getItemID(32);
    }

    public static void main(String[] args) {
        String s = "BUSINESS_CARD";
        String key = generate32Str();
        System.out.println(key);
        String encryptResultStr = encrypt(s,key);
        // 加密
        System.out.println("加密前:" + s);
        System.out.println("加密后:" + encryptResultStr);
        // 解密
        System.out.println("解密后:" + decrypt(encryptResultStr, key));

        System.out.println("符合:" + checkCode(encryptResultStr, key));

    }


}

3.枚举

package com.zykj.zycx.common.enums;

import com.zykj.zycx.utils.AESUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.security.Key;
import java.security.PrivateKey;
import java.util.Base64;

/**
 * 商户key枚举
 *
 * @author lc
 * @version 1.0
 * @date 2022/3/21 10:41
 */
public enum  BusinessEnum {
    BUSINESS_CARD(3, "12s6z02yo6X1OjB458mb2E89d9bqDF1j", "adxyNQCC+Tf4HgQM1a6klw==");


    private Integer businessId;
    private String businessKey; // 加密的盐
    private String businessCode; // 加密后的code

    // 私有构造
    BusinessEnum(int businessId, String businessKey, String businessCode) {
        this.businessId = businessId;
        this.businessKey = businessKey;
        this.businessCode = businessCode;
    }



    // 通过key来校验code
    public static boolean checkCode(BusinessEnum businessEnum) {
        boolean check = AESUtil.checkCode(businessEnum.businessCode, businessEnum.businessKey);
        return check;
    }

    public static boolean checkCode(String businessCode, String businessKey) {
        boolean check = AESUtil.checkCode(businessCode, businessKey);
        return check;
    }

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在Hive中实现RSA加密和解密自定义函数,你需要使用Hive的UDF(用户定义的函数)功能,并结合Java的RSA加密和解密算法。 下面是一个基本的示例,展示如何在Hive中实现RSA加密和解密自定义函数: 1. 创建一个Java类,例如`RSACrypto.java`,实现RSA加密和解密的逻辑。 ```javaimport java.security.*; import javax.crypto.*; import java.util.Base64; public class RSACrypto { public static String encrypt(String plainText, String publicKey) throws Exception { Cipher cipher = Cipher.getInstance("RSA"); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(Base64.getDecoder().decode(publicKey))); cipher.init(Cipher.ENCRYPT_MODE, pubKey); byte[] encryptedBytes = cipher.doFinal(plainText.getBytes()); return Base64.getEncoder().encodeToString(encryptedBytes); } public static String decrypt(String encryptedText, String privateKey) throws Exception { Cipher cipher = Cipher.getInstance("RSA"); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PrivateKey privKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKey))); cipher.init(Cipher.DECRYPT_MODE, privKey); byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText)); return new String(decryptedBytes); } } ``` 2. 编译`RSACrypto.java`文件,并将生成的`.class`文件打包成`jar`文件。 ```shelljavac RSACrypto.javajar cf rsacrypto.jar RSACrypto.class``` 3. 将`rsacrypto.jar`文件上传到Hive的服务器上。 4. 在Hive中创建一个函数,使用`CREATE FUNCTION`语句。 ```sqlCREATE FUNCTION rsa_encrypt AS 'com.example.RSACrypto' USING JAR 'hdfs:///path/to/rsacrypto.jar'; CREATE FUNCTION rsa_decrypt AS 'com.example.RSACrypto' USING JAR 'hdfs:///path/to/rsacrypto.jar'; ``` 5. 使用自定义函数进行RSA加密和解密。 ```sqlSELECT rsa_encrypt('Hello World', '<public_key>') AS encrypted_text; SELECT rsa_decrypt('<encrypted_text>', '<private_key>') AS decrypted_text; ``` 请确保替换`<public_key>`和`<private_key>`为实际的RSA公钥和私钥。还要注意,这只是一个简单的示例,实际上,您可能需要处理更复杂的情况,例如密钥管理和编码/解码方式等。 希望这个示例能帮助您实现在Hive中使用自定义函数进行RSA加密和解密

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LC超人在良家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值