AesDecrypt加解密UDF

package com.XXX.udf;

import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;

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

public class AesDecrypt extends UDF {

    private final static String cryptTpye = "AES";
    /*
     * 转为十六进制
     */
    private static String asHex(byte buf[]) {
        StringBuffer strbuf = new StringBuffer(buf.length * 2);
        int i;

        for (i = 0; i < buf.length; i++) {
            if (((int) buf[i] & 0xff) < 0x10)
                strbuf.append("0");
            strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
        }
        return strbuf.toString();
    }

    /*
     * 转为二进制
     */
    private static byte[] asBin(String src) {
        if (src.length() < 1)
            return null;
        byte[] encrypted = new byte[src.length() / 2];
        for (int i = 0; i < src.length() / 2; i++) {
            int high = Integer.parseInt(src.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(src.substring(i * 2 + 1, i * 2 + 2), 16);
            encrypted[i] = (byte) (high * 16 + low);
        }
        return encrypted;
    }

    /*
     * 得到密钥key
     */
    public static String getRawKey() {

        try {
            KeyGenerator kgen = KeyGenerator.getInstance(cryptTpye);// 获取密匙生成器
            kgen.init(128); // 192 and 256 bits may not be available
            SecretKey skey = kgen.generateKey();// 生成密匙,可用多种方法来保存密匙
            byte[] raw = skey.getEncoded();
            return asHex(raw);
        } catch (Exception e) {
            return "";
        }
    }

    /*
     * 加密
     */
    public static String getEncrypt(String message, String rawKey) {
        byte[] key = asBin(rawKey);
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(key, cryptTpye);
            Cipher cipher = Cipher.getInstance(cryptTpye);// 创建密码器
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            byte[] encrypted = cipher.doFinal(message.getBytes());
            return asHex(encrypted);
        } catch (Exception e) {
            return "";
        }
    }

    /*
     * 解密
     */
    public static String getDecrypt(String encrypted, String rawKey) {
        byte[] tmp = asBin(encrypted);
        byte[] key = asBin(rawKey);
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(key, cryptTpye);
            Cipher cipher = Cipher.getInstance(cryptTpye);
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] decrypted = cipher.doFinal(tmp);
            return new String(decrypted);
        } catch (Exception e) {
            return "";
        }
    }

    public Text evaluate(Text input, Text rawKey) {
        if (null == input || null == rawKey) {
            return null;
        }
        String inputValue = input.toString().trim();
        String rawKeyValue = rawKey.toString().trim();
        if (null == inputValue || null == rawKey) {
            return null;
        }
        return new Text(getDecrypt(inputValue, rawKeyValue));
    }
}

 

posted @ 2019-05-17 09:48  问题不大1  阅读(2795)  评论(0编辑  收藏  举报