永发信息网

如何保存密钥文件更安全

答案:2  悬赏:40  手机版
解决时间 2021-03-07 05:20
  • 提问者网友:几叶到寒
  • 2021-03-06 23:26
如何保存密钥文件更安全
最佳答案
  • 五星知识达人网友:行雁书
  • 2021-03-07 00:27
下面是一个进行加密的工具类,加密类会产生一个密钥文件,密钥文件保存到了硬盘文件中,程序中要进行对数据的加解密操作。密钥文件保存在文件,别人也就可以读取密钥文件,获得加密数据的内容。publicclass EncryptUtil { privatestatic String keyPath = null; privatestatic String getKeyPath() { keyPath = "c:\\yhb.des"; return keyPath; } publicstaticvoid generatorKey() { SecretKey key = null; try { // 指定算法,这里为DES;如果想用Blowfish算法,则用getInstance("Blowfish") // BouncyCastle基本上支持所有通用标准算法 KeyGenerator keygen = KeyGenerator.getInstance("DES"); // 指定密钥长度,长度越高,加密强度越大 keygen.init(56); // 产生密钥 key = keygen.generateKey(); // 构造输出文件,这里的目录是动态的,根据用户名称来构造目录 ObjectOutputStream keyFile = new ObjectOutputStream( new FileOutputStream(getKeyPath())); keyFile.writeObject(key); keyFile.close(); } catch (NoSuchAlgorithmException e5) { e5.printStackTrace(); System.exit(0); } catch (IOException e4) { e4.printStackTrace(); System.exit(0); } } privatestatic SecretKey getSecretKey() { // 从密钥文件中读密钥 SecretKey key = null; try { ObjectInputStream keyFile = new ObjectInputStream( new FileInputStream(getKeyPath())); key = (SecretKey) keyFile.readObject(); keyFile.close(); } catch (FileNotFoundException ey1) { e1.printStackTrace(); System.exit(0); } catch (Exception ey2) { e2.printStackTrace(); } return key; } publicstatic String encrypt(String encryptStr) { SecretKey key = getSecretKey(); Cipher cipher = null; try { // 设置算法,应该与加密时的设置一样 cipher = Cipher.getInstance("DES"); // 设置解密模式 cipher.init(Cipher.ENCRYPT_MODE, key); } catch (Exception ey3) { ey3.printStackTrace(); } byte[] data = null; try { data = cipher.doFinal(encryptStr.getBytes()); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } encryptStr = Base64.encodeBase64String(data); return encryptStr; } publicstatic String decrypt(String decryptStr) { SecretKey key = getSecretKey(); // 用key产生Cipher Cipher cipher = null; try { // 设置算法,应该与加密时的设置一样 cipher = Cipher.getInstance("DES"); // 设置解密模式 cipher.init(Cipher.DECRYPT_MODE, key); } catch (Exception ey3) { ey3.printStackTrace(); System.exit(0); } byte[] data = Base64.decodeBase64(decryptStr); try { data = cipher.doFinal(data); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } decryptStr = new String(data); return decryptStr; } }
全部回答
  • 1楼网友:摆渡翁
  • 2021-03-07 01:57
你没有办法半岛的,一次性输入连接密码,只要连接,就是自动连接
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯