阅读nifi源码,加密nifi连接数据库密码

前言:前面已经写了nifi数据库解密,我们也可以使用nifi这种加密方法去进行加密,方便以后使用.nifi数据库连接密码解密

加密:

package com.company;

import org.apache.commons.codec.binary.Hex;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.*;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.spec.InvalidKeySpecException;

public class EncryptionTest2 {

    private static final String DEFAULT_FLOW_ALGORITHM = "PBEWITHMD5AND256BITAES-CBC-OPENSSL";
    private static final String DEFAULT_PROVIDER = BouncyCastleProvider.PROVIDER_NAME;


    public static void main(String[] args) throws NoSuchPaddingException, NoSuchAlgorithmException, NoSuchProviderException, BadPaddingException, IOException, IllegalBlockSizeException, InvalidAlgorithmParameterException, InvalidKeySpecException, InvalidKeyException {
        //随机生成16位的盐
        byte[] encryptionSalt = new byte[16];
        new SecureRandom().nextBytes(encryptionSalt);
        //nififtw!为nifi定义的口令,可以自己修改
        Cipher cipher= getCipher("nififtw!",encryptionSalt);
        //test为需要加密的密码
        String pass = encryptFlowElement("test",encryptionSalt,cipher);
        System.out.println(pass);
    }


    private static String encryptFlowElement(String plaintext, byte[] saltBytes, Cipher encryptCipher) throws BadPaddingException, IllegalBlockSizeException, IOException {
        byte[] plainBytes = plaintext.getBytes(StandardCharsets.UTF_8);
        byte[] cipherBytes = encryptCipher.doFinal(plainBytes);
        byte[] saltAndCipherBytes = conByteArray(saltBytes, cipherBytes);
        String hexEncodedCipherText = Hex.encodeHexString(saltAndCipherBytes);
        return "enc{"+hexEncodedCipherText+"}";
    }

    public static byte [] conByteArray(byte [] arr1,byte [] arr2) throws IOException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        outputStream.write(arr1);
        outputStream.write(arr2);
        return outputStream.toByteArray();
    }

    //这个password 是口令
    private static Cipher getCipher(String password,byte [] saltBytes) throws NoSuchPaddingException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException, InvalidAlgorithmParameterException, InvalidKeyException {
        Cipher encryptCipher = Cipher.getInstance(DEFAULT_FLOW_ALGORITHM, DEFAULT_PROVIDER);
        PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DEFAULT_FLOW_ALGORITHM, DEFAULT_PROVIDER);
        SecretKey pbeKey = keyFactory.generateSecret(keySpec);
        PBEParameterSpec parameterSpec = new PBEParameterSpec(saltBytes,1000);
        encryptCipher.init(Cipher.ENCRYPT_MODE, pbeKey, parameterSpec);
        return encryptCipher;
    }
}

有兴趣的同学可以看我的微信公众号,后面会陆续开发

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值