gq2010 2012-08-15 16:25 采纳率: 0%
浏览 388
已采纳

如何保存密钥文件更安全

下面是一个进行加密的工具类,加密类会产生一个密钥文件,密钥文件保存到了硬盘文件中,程序中要进行对数据的加解密操作。密钥文件保存在文件,别人也就可以读取密钥文件,获得加密数据的内容。有什么可以安全保存密钥文件的方式么?

 

public class EncryptUtil {    
    private static String keyPath = null;

    private static String getKeyPath() {
        keyPath = "c:\\yhb.des";
        return keyPath;
    }
    
    /**
     * 对称加密-产生密钥<br/>
     */
    public static void 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);
        }
    }
    
    /**
     * 对称加密-读取密钥.<br/>
     */
    private static 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;
    }

    /**
     * 加密文本信息.<br/>
     */
    public static 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;
    }
    
    /**
     * 解密文本信息.<br/>
     */
    public static 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条回答 默认 最新

  • iteye_12723 2012-08-15 16:48
    关注

    为什么不用RSA 那样更安全.
    换个角度
    你的DES密钥文件 存储在服务器端 应该很安全了.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题