【问题标题】:Cryptography - Java密码学 - Java
【发布时间】:2014-11-19 04:26:18
【问题描述】:

我是网站的新手。我关注了这个帖子How to Encrypt or Decrypt a File in Java?

并让一切功能正常(还关注:http://www-users.york.ac.uk/~mal503/lore/pkencryption.htm 和之前的 stackoverflow 帖子)。

这可能是一个愚蠢的问题,但是编译后的加密文件在哪里?我找不到它了。如何在 java eclipse 中将其输出到我的桌面或 src 文件夹?我试图最终将其变成一个应用程序,您可以在其中在命令行输入文件进行加密,然后在您喜欢解密时输入(严格教育,对于课程)。感谢您的时间和帮助。

我的代码:

/**
 * Utility class for encrypting/decrypting files.
 * 
 */
public class CryptoFunction {
    public static final int AES_Key_Size = 256;

    Cipher pkCipher, aesCipher;
    byte[] aesKey;
    SecretKeySpec aeskeySpec;

    /**
     * Constructor: creates ciphers
     */
    public CryptoFunction() throws GeneralSecurityException {
        // create RSA public key cipher
        pkCipher = Cipher.getInstance("RSA");
        // create AES shared key cipher
        aesCipher = Cipher.getInstance("AES");
    }

    /**
     * Creates a new AES key
     * A random AES key is generated to encrypt files.
     * A key size (AES_Key_Size) of 256 bits is standard for AES
     */
    public void makeKey() throws NoSuchAlgorithmException {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(AES_Key_Size);
        SecretKey key = kgen.generateKey();
        aesKey = key.getEncoded();
        aeskeySpec = new SecretKeySpec(aesKey, "AES");
    }

    /**
     * Decrypts an AES key from a file using an RSA private key
     */
    @SuppressWarnings("resource")
    public void loadKey(File in, File privateKeyFile) throws GeneralSecurityException, IOException {
        // read private key to be used to decrypt the AES key
        byte[] encodedKey = new byte[(int)privateKeyFile.length()];
        new FileInputStream(privateKeyFile).read(encodedKey);

        // create private key
        PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedKey);
        KeyFactory kf = KeyFactory.getInstance("RSA");
        PrivateKey pk = kf.generatePrivate(privateKeySpec);

        // read AES key
        pkCipher.init(Cipher.DECRYPT_MODE, pk);
        aesKey = new byte[AES_Key_Size/8];
        CipherInputStream is = new CipherInputStream(new FileInputStream(in), pkCipher);
        is.read(aesKey);
        aeskeySpec = new SecretKeySpec(aesKey, "AES");
    }

    /**
     * Encrypts the AES key to a file using an RSA public key
     */
    @SuppressWarnings("resource")
    public void saveKey(File out, File publicKeyFile) throws IOException, GeneralSecurityException {
        // read public key to be used to encrypt the AES key
        byte[] encodedKey = new byte[(int)publicKeyFile.length()];
        new FileInputStream(publicKeyFile).read(encodedKey);

        // create public key
        X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedKey);
        KeyFactory kf = KeyFactory.getInstance("RSA");
        PublicKey pk = kf.generatePublic(publicKeySpec);

        // write AES key
        pkCipher.init(Cipher.ENCRYPT_MODE, pk);
        CipherOutputStream os = new CipherOutputStream(new FileOutputStream(out), pkCipher);
        os.write(aesKey);
        os.close();
    }

    /**
     * Encrypts and then copies the contents of a given file.
     */
    public void encrypt(File in, File out) throws IOException, InvalidKeyException {
        aesCipher.init(Cipher.ENCRYPT_MODE, aeskeySpec);

        FileInputStream is = new FileInputStream(in);
        CipherOutputStream os = new CipherOutputStream(new FileOutputStream(out), aesCipher);

        copy(is, os);

        os.close();
    }

    /**
     * Decrypts and then copies the contents of a given file.
     */
    public void decrypt(File in, File out) throws IOException, InvalidKeyException {
        aesCipher.init(Cipher.DECRYPT_MODE, aeskeySpec);

        CipherInputStream is = new CipherInputStream(new FileInputStream(in), aesCipher);
        FileOutputStream os = new FileOutputStream(out);

        copy(is, os);

        is.close();
        os.close();
    }

    /**
     * Copies a stream.
     */
    private void copy(InputStream is, OutputStream os) throws IOException {
        int i;
        byte[] b = new byte[1024];
        while((i=is.read(b))!=-1) {
            os.write(b, 0, i);
            System.out.println("I is copying!");

             // read what we wrote
             for (int j = 0; j < b.length; j++) {
                System.out.print("" + (char) is.read() + '\n');
             }
        }
    }


    public void encryption(){

    }

    public void decryption(){

    }

    public static void main(String[] args) throws GeneralSecurityException, IOException {
        System.out.println("Begin Encyption!"); // Display the string.

        CryptoFunction secure = new CryptoFunction();

        // Encrypt code
        File encryptFile = new File("encrypt.data");
        File publicKeyData = new File("src/publickey.der");
        File originalFile = new File("src/stufftoencrypt.txt");
        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("privatekey");
        File secureFile = new File("secure.data");
        File unencryptedFile = new File("unencryptedFile");

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

        // decrypt file
        secure.decrypt(secureFile, unencryptedFile);
        System.out.println("End Encryption!"); // Display the string.
      }
}

【问题讨论】:

    标签: java eclipse file encryption output


    【解决方案1】:

    假设代码有效(我没有测试它),您可能只需要刷新您的 eclipse 项目。如果在其中创建了secure.data 文件,eclipse 不会自动刷新以显示它。如果要在特定位置创建文件,请将绝对路径放入 new File(...) 例如要在 C:\temp 中创建,请使用:

    File secureFile = new File("c:\\temp\\secure.data");
    File unencryptedFile = new File("c:\\temp\\unencryptedFile");
    

    您可以使用以下命令检查这些文件的绝对路径:

    System.out.println(unencryptedFile.getAbsolutePath());
    System.out.println(secureFile.getAbsolutePath());
    

    【讨论】:

    • 刷新是正确的,但我还有第二个问题。现在我的 src 中有 secure.data 文件(加密),并且我在 src 文件夹中获得了未加密的文件(解密),解密的文件没有被解密。有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-26
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多