关于java生成SecureRandom随机秘钥的问题

  前两天在做一个项目时需要对文本内容进行3DES加密,利用一个33位自定义的种子秘钥,使用秘钥工厂生成一个新的秘钥去做加密处理.直接上代码.

加密过程:`
/**
* @param Str 加密前原文
* @param keyStr 秘钥
* @return signStr 加密后字符串
* */

public static String getTripleDES(String Str,String keyStr){
	  Key key = null;
        try {
            KeyGenerator _generator = KeyGenerator.getInstance("DES");//获取DES算法key的工厂
            System.out.println(_generator);
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");  //产生一个加密的随机数,getInstance方法里还可以指定用哪个公司的算法,SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG","SUN");用sun公司的jdk,我们公司产品是支持ibm的jdk,调试的时候产生的秘钥一直和eclipse里测试里产生的不一样
            System.out.println(secureRandom);
            secureRandom.setSeed(keyStr.getBytes()); //使用种子秘钥可以生成个固定的加密秘钥.
            _generator.init(secureRandom);	            
            key = _generator.generateKey();
            System.out.println(key);
            _generator = null;
        } catch (NoSuchAlgorithmException e) {
        	System.out.println("key生成失败");
            e.printStackTrace();
            return null;
        }
        byte[] byteMi = null;
        byte[] byteMing = null;
        String strMi = "";
        try {
            byteMing = Str.getBytes("utf-8");	            
            System.out.println(byteMing);
            byteMi = getEncCode(byteMing, key);
            strMi = new String(Base64.encodeBase64URLSafe(byteMi),"UTF-8");//base64URL主要用于链接的加密,将base64编码后的字符串中"+"替换为"-","/"替换为_"",
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("3DES加密失败");
        } finally {
            byteMi = null;
            byteMing = null;
        }
        return strMi;
}`
   
   /**
 * 获得一次3DES加密后的密文
 * 
 * @param byts
 * @return
 */
private static byte[] getEncCode(byte[] byts, Key key) {
    byte[] byteFina = null;
    Cipher cipher;
    try {
        cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.ENCRYPT_MODE, key);//加密模式为CBC
        System.out.println(cipher);
        byteFina = cipher.doFinal(byts);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        cipher = null;
    }
    return byteFina;
}

接下来是解密过程,解密时同样使用自定义的种子秘钥生成一个加密的秘钥,此秘钥和上面加密过程中产生的秘钥是一样的:

	/**
     * 获得3DES解密后的明文
     * 
     * @param sign 密文
     * @param keyStr 自定义秘钥
     * @return 明文
     */
    public static String getDecString(String sign, String keyStr) {
    	Key key = null;
        try {
            KeyGenerator _generator = KeyGenerator.getInstance("DES");
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");  
            secureRandom.setSeed(keyStr.getBytes());  
            _generator.init(secureRandom);
            key = _generator.generateKey();  //生成加密字符串的秘钥
            _generator = null;
        } catch (NoSuchAlgorithmException e) {
        	System.out.println("key生成失败");
            e.printStackTrace();
            return null;
        }
    	
        byte[] byteMing = null;
        byte[] byteMi = null;
        String strMing = "";//解密后的明文
        try {
            byteMi =Base64.decodeBase64(sign);  //解密的方法jdk源码中已经兼容了.decodeBase64URLSaf()的方法,有兴趣的可以研究研究
            byteMing = getDecCode(byteMi, key);  //解密操作
            strMing = new String(byteMing, "utf-8");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("解密失败");
        } finally {
            byteMing = null;
            byteMi = null;
        }
        return strMing;
    }
	
	/**
     * 获得一次3DES加密后的密文
     * 
     * @param byts
     * @return
     */
    private static byte[] getEncCode(byte[] byts, Key key) {
        byte[] byteFina = null;
        Cipher cipher;
        try {
            cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.ENCRYPT_MODE, key);//key-->通过key工厂随机产生的解密秘钥
            System.out.println(cipher);
            byteFina = cipher.doFinal(byts);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cipher = null;
        }
        return byteFina;
    }

最后是调试过程:

	public static void main(String[] args) {
				
		try{
			String Str = "1|000000109514|D201811191542590446642|20181119|09:20|210.00|交易成功";
			String keyStr = "MIIBIjANBgkqhkiG9w0BAQEFAQ8AMaAwQ";   //种子密钥
			
			
			String  strMi = chkFile.getTripleDES(Str,keyStr);
			System.out.println("加密后字符串:"+strMi);
			
			String strMing  = chkFile.getDecString(strMi,keyStr);
			System.out.println("解密后字符串:"+strMing);
			
			System.out.print("写入加密内容成功");
		}catch(Exception e){
			e.printStackTrace();
		}

	}

第一次写博客,还请各位大佬捧个场,多多指教.

你可能感兴趣的:(java)