Hive Udf函数AES加密

pom.xml

XML 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>


    <repositories>
        <repository>
            <id>spring</id>
            <url>https://maven.aliyun.com/repository/spring</url>
        </repository>
    </repositories>
    <groupId>com.asiainfo.udf</groupId>
    <artifactId>udf</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <!-- hive的udf依赖的包 -->
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-exec</artifactId>
            <version>2.1.0</version>
        </dependency>
        <dependency>
            <groupId>jdk.tools</groupId>
            <artifactId>jdk.tools</artifactId>
            <version>1.8</version>
            <scope>system</scope>
            <systemPath>C:/Program Files/Java/jdk1.8.0_311/lib/tools.jar</systemPath>
        </dependency>
    </dependencies>
    <!-- 打包类的依赖的jar的插件 -->
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

源码

java 复制代码
package com.care.udf;
import org.apache.commons.codec.binary.Base64;
import org.apache.hadoop.hive.ql.exec.UDF;


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


public class AesUtil extends UDF{


    String password = "xxxxxxxxxxxxxxxx";


    public String evaluate ( String type , String content) throws Exception {
        if (content == null) return null;


        if(! type.equals("encode") && ! type.equals("decode")){
            throw new Exception("Parmeter one is needed encode/decode");
        }


        if( type.equals("encode")){
            //进行加密
            return Encrypt(content,password);
        }else {
            //进行解密
            return Decrypt(content,password);
        }
    }




    // 加密
    public static String Encrypt(String sSrc, String sKey) throws Exception {
        if (sKey == null) {
            System.out.print("Key为空null");
            return null;
        }
        // 判断Key是否为16位
        if (sKey.length() != 16) {
            System.out.print("Key长度不是16位");
            return null;
        }
        byte[] raw = sKey.getBytes("utf-8");
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
        return new Base64().encodeToString(encrypted);//此处使用BASE64做转码功能,同时能起到2次加密的作用。
    }


    // 解密
    public static String Decrypt(String sSrc, String sKey) throws Exception {
        try {
            // 判断Key是否正确
            if (sKey == null) {
                System.out.print("Key为空null");
                return null;
            }
            // 判断Key是否为16位
            if (sKey.length() != 16) {
                System.out.print("Key长度不是16位");
                return null;
            }
            byte[] raw = sKey.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encrypted1 = new Base64().decode(sSrc);//先用base64解密
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original,"utf-8");
                return originalString;
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }




    /**
     *
     * @Description AES解密,加密内容转化为16位二进制后解密mxg20190906
     * @return String
     * @author xiuning
     */
    public static String aesDecrypt(String sSrc, String sKey) throws Exception {
        try {
            // 判断Key是否正确
            if (sKey == null) {
                System.out.print("Key为空null");
                return null;
            }
            // 判断Key是否为16位
            if (sKey.length() != 16) {
                System.out.print("Key长度不是16位");
                return null;
            }
            byte[] raw = sKey.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
//            byte[] encrypted1 = new Base64().decode(sSrc);//先用base64解密
            try {
                byte[] original = cipher.doFinal( parseHexStr2Byte(sSrc));
                String originalString = new String(original,"utf-8");
                return originalString;
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }


    /**将16进制转换为二进制
     * @param hexStr
     * @return
     */
    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1)
            return null;
        byte[] result = 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);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }
    public static void main(String[] args) throws Exception {
        System.out.println(new AesUtil().evaluate("encode","test"));
        System.out.println(new AesUtil().evaluate("decode","06QRnaWh2cIcVV+YLtCJCw=="));
    }




}
相关推荐
青春万岁!!3 天前
hive 动态分区参数设置错误导致数据不稳定
大数据·数据仓库·hive·hadoop
roman_日积跬步-终至千里4 天前
为什么 Hive 无法通过同步 JDBC 导出百万级数据?
数据仓库·hive·hadoop
roman_日积跬步-终至千里4 天前
Hive JDBC vs MySQL JDBC:**“服务端推完就跑,客户端慢慢吃”**详解
数据仓库·hive·hadoop
m0_716255005 天前
第二部分 电商离线数仓 全套项目代码(可直接在你伪分布式 Hive 运行)
hive·hadoop·分布式
隐于花海,等待花开8 天前
41.ABS / POW / SQRT 函数深度解析
大数据·hive
隐于花海,等待花开9 天前
40.RAND 函数深度解析
hive·hadoop
孤雪心殇10 天前
快速上手数仓基础知识
数据仓库·hive·spark
隐于花海,等待花开10 天前
39.ROUND / FLOOR / CEIL 函数深度解析
hive·hadoop
看海的四叔11 天前
【SQL】SQL-管好你的字符串
大数据·数据库·hive·sql·数据分析·字符串