如何用Java加密或解密文件?

时间:2011-04-12 08:53:15

标签: java cryptography

我想在java中加密和解密文件,我已经阅读了这个网址http://www-users.york.ac.uk/~mal503/lore/pkencryption.htm,我得到了两个文件,即公共安全证书和私有安全证书文件以及private.pem文件,我复制了这些文件并粘贴了在当前目录和worte java代码如下,当我运行这个没有执行加密或解密时,请看到这个并告诉我哪里出错了

加密代码

File ecryptfile=new File("encrypt data");
                File publickeydata=new File("/publickey");
                File encryptmyfile=new File("/sys_data.db");
                File copycontent =new File("Copy Data");
                secure.makeKey();
                secure.saveKey(ecryptfile, publickeydata);
                secure.encrypt(encryptmyfile, copycontent);

解密代码

File ecryptfile=new File("encrypt data");
                File privateKeyFile=new File("/privatekey");
                File encryptmyfile=new File("/sys_data.db");
                File unencryptedFile =new File("unencryptedFile");
                try {
                                secure.loadKey(encryptmyfile, privateKeyFile);
                                secure.decrypt(encryptmyfile, unencryptedFile);
                        } catch (GeneralSecurityException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }

2 个答案:

答案 0 :(得分:9)

你只是弄乱了你的文件。此代码使用从openssl生成的DER文件,如您链接的文章中所述:

    FileEncryption secure = new FileEncryption();

    // Encrypt code
    {
        File encryptFile = new File("encrypt.data");
        File publicKeyData = new File("public.der");
        File originalFile = new File("sys_data.db");
        File secureFile = new File("secure.data");

        // create AES key
        secure.makeKey();

        // save AES key using public key
        secure.saveKey(encryptFile, publicKeyData);

        // save original file securely 
        secure.encrypt(originalFile, secureFile);
    }

    // Decrypt code
    {
        File encryptFile = new File("encrypt.data");
        File privateKeyFile = new File("private.der");
        File secureFile = new File("secure.data");
        File unencryptedFile = new File("unencryptedFile");

        // load AES key
        secure.loadKey(encryptFile, privateKeyFile);

        // decrypt file
        secure.decrypt(secureFile, unencryptedFile);
    }

答案 1 :(得分:1)

以下是使用任何密钥(此处为随机密钥)加密文档的最简单代码

randomKey = randomKey.substring(0, 16);
        keyBytes = randomKey.getBytes();
        key = new SecretKeySpec(keyBytes, "AES");
        paramSpec = new IvParameterSpec(iv);
        ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
        in = new FileInputStream(srcFile);
        out = new FileOutputStream(encryptedFile);
        out = new CipherOutputStream(out, ecipher);
        int numRead = 0;
        while ((numRead = in.read(buf)) >= 0) {
            out.write(buf, 0, numRead);
        }
        in.close();
        out.flush();
        out.close();