1、生成RSA密钥对(公钥和私钥)
import sun.misc.BASE64Encoder;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
private static final String ALGORITHM = "RSA";
private static final int KEYSIZE = 1024;
private static void generateKeyPair() throws Exception {
//RSA算法要求有一个可信任的随机数源
//SecureRandom secureRandom = new SecureRandom();
// 为RSA算法创建一个KeyPairGenerator对象
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);
// 利用上面的随机数据源初始化这个KeyPairGenerator对象
//keyPairGenerator.initialize(KEYSIZE, secureRandom);
keyPairGenerator.initialize(KEYSIZE);
// 生成密匙对
KeyPair keyPair = keyPairGenerator.generateKeyPair();
// 公钥
Key publicKey = keyPair.getPublic();
// 私钥
Key privateKey = keyPair.getPrivate();
String publicKeyBase64 = new BASE64Encoder().encode(publicKey.getEncoded());
String privateKeyBase64 = new BASE64Encoder().encode(privateKey.getEncoded());
System.out.println(publicKeyBase64);
System.out.println(privateKeyBase64);
}
2、RSA加密
import org.apache.commons.codec.binary.Base64;
import org.apache.hadoop.hive.ql.exec.MapredContext;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* RSA加密
*/
public class RsaEncoder extends GenericUDF {
private static final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//非对称密钥算法
public static final String KEY_ALGORITHM = "RSA";
// RSA 公钥
private static String RasPublicKey = "RSA公钥用于加密";
private Cipher cipher = null;
@Override
public void configure(MapredContext context) {
System.out.println(new Date() + "######## configure");
if(null != context) {
//从jobConf中获取公钥
String confPublicKey = context.getJobConf().get("com.howe.udf.rsa.publickey");
if( confPublicKey != null && confPublicKey.length() > 0 ) {
RasPublicKey = confPublicKey;
}
}
}
@Override
public ObjectInspector initialize(ObjectInspector[] arguments) {
//System.out.println(formatter.format(new Date()) + "######## initialize");
try {
//实例化密钥工厂
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
//初始化公钥
//密钥材料转换
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(Base64.decodeBase64(RasPublicKey));
//产生公钥
PublicKey pubKey = keyFactory.generatePublic(x509KeySpec);
//数据加密
cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
System.out.println(formatter.format(new Date()) + "######## The Initialization Of Cipher Has Completed Successfully");
} catch (Exception e) {
e.printStackTrace();
cipher = null;
System.out.println(formatter.format(new Date()) + "######## Error: Initialization Failed!");
}
return PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveObjectInspector.PrimitiveCategory.STRING);
}
@Override
public Object evaluate(DeferredObject[] args) throws HiveException {
if( cipher == null ) return null;
try {
final String text = args[0].get().toString();
if ( text != null && text.length() > 0)
return Base64.encodeBase64String(cipher.doFinal(text.getBytes()));
else
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
public String getDisplayString(String[] strings) {
return "Usage: FUNC_(String encrypted_text)";
}
}
3、RSA解密
import org.apache.commons.codec.binary.Base64;
import org.apache.hadoop.hive.ql.exec.MapredContext;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* RSA 解密
*/
public class RsaDecoder extends GenericUDF {
private static final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//非对称密钥算法
public static final String KEY_ALGORITHM = "RSA";
// RSA 私钥
private static String RasPrivateKey = "RSA私钥用于解密";
private Cipher cipher = null;
@Override
public void configure(MapredContext context) {
System.out.println(new Date() + "######## configure");
if(null != context) {
//从jobConf中获取私钥
String confPrivateKey = context.getJobConf().get("com.howe.udf.rsa.privatekey");
if( confPrivateKey != null && confPrivateKey.length() > 0 ) {
RasPrivateKey = confPrivateKey;
}
}
}
@Override
public ObjectInspector initialize(ObjectInspector[] arguments) {
//System.out.println(formatter.format(new Date()) + "######## initialize");
try {
//取得私钥
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(RasPrivateKey));
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
//生成私钥
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
//数据解密
cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, privateKey);
System.out.println(formatter.format(new Date()) + "######## The Initialization Of Cipher Has Completed Successfully");
} catch (Exception e) {
e.printStackTrace();
cipher = null;
System.out.println(formatter.format(new Date()) + "######## Error: Initialization Failed!");
}
return PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveObjectInspector.PrimitiveCategory.STRING);
}
@Override
public Object evaluate(DeferredObject[] args) throws HiveException {
if( cipher == null ) return null;
try {
final String text = args[0].get().toString();
if ( text != null && text.length() > 0)
return new String(cipher.doFinal(Base64.decodeBase64(text)));
else
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
public String getDisplayString(String[] strings) {
return "Usage: FUNC_(String encrypted_text)";
}
}
4、创建函数
将打包后的jar包上传到hdfs
hive
DROP FUNCTION IF EXISTS default.rsa_encoder;
CREATE function default.rsa_encoder as 'com.howe.hive.udf.encoder.RsaEncoder' using jar 'hdfs://hacluster/user/hive/udf/MyUdf.jar';
DROP FUNCTION IF EXISTS default.rsa_decoder;
CREATE function default.rsa_decoder as 'com.howe.hive.udf.encoder.RsaDecoder' using jar 'hdfs://hacluster/user/hive/udf/MyUdf.jar';