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 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛