java - Java中的Hive Udf(加解密)

标签 java hadoop encryption hive

实际上,我已经用 Java 编写了一个用于加密和解密的 Hive UDF。 但它有一些小错误。我找不到它,有人可以请纠正并建议我做一些更改..

问题:

 When i tried to execute this code using Hive it is showing some 'Null' columns for each
 row.
   Encrypted Ex:  1      fdfsvansjw=
                  NULL   NULL
                  2      adf4vandjw=
                  NULL   NULL

  Actually it has to be displayed without NULL Values.When i tried to decrypt the above
  data it is adding Newline Character '/n' in place of Null.
  Decrypted Ex:  1      AAA
                  /n    /n
                  2      BBB
                  /n     /n

加密代码:

package Encrypt;

import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;
import java.security.*;
import org.apache.commons.codec.binary.Base64;
import java.io.*;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import javax.swing.JOptionPane;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public final class En1 extends UDF {

public Text evaluate(final Text s) throws Exception {
if (s == null) {
 return null;
}
byte[] sharedvector = {
0x01, 0x02, 0x03, 0x05, 0x07, 0x0B, 0x0D, 0x11
};

String EncText = "";
byte[] keyArray = new byte[24];
byte[] temporaryKey;
String key = "developersnotedotcom";
byte[] toEncryptArray = null;

//try
   // {

    toEncryptArray =  s.toString().getBytes("UTF-8");        
    MessageDigest m = MessageDigest.getInstance("MD5");
    temporaryKey = m.digest(key.getBytes("UTF-8"));

    if(temporaryKey.length < 24) // DESede require 24 byte length key
    {
        int index = 0;
        for(int i=temporaryKey.length;i< 24;i++)
        {                   
            keyArray[i] =  temporaryKey[index];
        }
    }        

    Cipher c = Cipher.getInstance("DESede/CBC/PKCS5Padding");            
    c.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyArray, "DESede"), new IvParameterSpec(sharedvector));            
    byte[] encrypted = c.doFinal(toEncryptArray);            
    EncText = Base64.encodeBase64String(encrypted);


//  }
   /* catch(NoSuchAlgorithmException | UnsupportedEncodingException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException NoEx)
{
    //JOptionPane.showMessageDialog(null, NoEx);
     System.out.println(NoEx);
     System.exit(1);
}*/

return new Text(EncText.toString());        
}

}

输入:

Actual I/p Ex:    1      AAA
                  2      BBB

Encrypted O/p Ex:     1      fdfsvansjw=
                      NULL   NULL
                      2      adf4vandjw=
                      NULL   NULL

解密代码:

package Encrypt;

import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;
import org.apache.commons.codec.binary.Base64;
import org.apache.hadoop.hive.ql.exec.FunctionTask;
import java.security.MessageDigest;

import javax.crypto.Cipher;

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

public final class Dec1 extends UDF {

public Text evaluate(final Text s) {
  if (s == null) {
    return null;
   }
  byte[] sharedvector = {
   0x01, 0x02, 0x03, 0x05, 0x07, 0x0B, 0x0D, 0x11
   };

String RawText = "";
byte[] keyArray = new byte[24];
byte[] temporaryKey;
String key = "developersnotedotcom";
byte[] toEncryptArray = null;

try
  {

    MessageDigest m = MessageDigest.getInstance("MD5");
        temporaryKey = m.digest(key.getBytes("UTF-8"));           

        if(temporaryKey.length < 24) // DESede require 24 byte length key
        {
            int index = 0;
            for(int i=temporaryKey.length;i< 24;i++)
            {                  
                keyArray[i] =  temporaryKey[index];
            }
        }

        Cipher c = Cipher.getInstance("DESede/CBC/PKCS5Padding");
        c.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyArray, "DESede"), new IvParameterSpec(sharedvector));
        byte[] decrypted = c.doFinal(Base64.decodeBase64(s.toString()));    
        RawText = new String(decrypted, "UTF-8"); 
   }
   catch(Exception NoEx)
    {
    //JOptionPane.showMessageDialog(null, NoEx);
     System.out.println(NoEx + "This is Udf error");
     System.exit(1);
    }

   return new Text(RawText.toString());        
}

}

输入:

Decrypted I/p Ex:     1      fdfsvansjw=
                      NULL   NULL
                      2      adf4vandjw=
                      NULL   NULL

Decrypted o/p Ex:    1      AAA
                     /n     /n
                     2      BBB
                     /n     /n

There should'nt be any Null's or /n when encryption and decryption.
Tried to find out the bug. But can't find out.
Please Help me.

Thanks

最佳答案

原因与Hive无关。

加密字符串由 CRLF 分隔,因此您应该删除加密方法末尾的 \r\n:return new Text(EncText.toString().replaceAll( "\r|\n", ""));

关于java - Java中的Hive Udf(加解密),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24226554/

相关文章:

c++ - 解密字符串末尾需要 NULL 终止符是什么?

java - 按下抽屉导航项目时应用程序崩溃

java - Flickr API + JAVA - flickrj

java - 如何在不抛出异常的情况下进入catch block

java - 由于 hive 查询错误导致 hadoop 作业出错

linux - 使用密码加密和解密文件

java - 我应该如何发送 Postgis 几何数据? WKT 还是 WKB?

java - 在 HDFS 上追加文件的推荐方法是什么?

python - 没有特定字符的压缩数据的 Ascii 表示

java - python和java的sha1加密结果不同