从文件读取加密数据 [英] Reading encrypted data from a file

查看:173
本文介绍了从文件读取加密数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过IBM关于使用私钥加密的教程。我写了如下代码

I was going through an IBM tutorial on encrypting using Private key. And I wrote the code as below

import java.security.*;
import javax.crypto.*;

// encrypt and decrypt using the DES private key algorithm

public class PrivateExample {

  public static void main (String[] args) throws Exception {
    String text=new String();
     text="THIS IS AN ENCRYPTION TEST";
     byte[] plainText = text.getBytes("UTF8");

    // get a DES private key
    System.out.println( "\nStart generating DES key" );
    KeyGenerator keyGen = KeyGenerator.getInstance("DES");
    keyGen.init(56);
    Key key = keyGen.generateKey();
    System.out.println( "Finish generating DES key" );

    // get a DES cipher object and print the provider
    Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
    System.out.println( "\n" + cipher.getProvider().getInfo() );
    //
    // encrypt using the key and the plaintext
    System.out.println( "\nStart encryption" );
    cipher.init(Cipher.ENCRYPT_MODE, key);
    byte[] cipherText = cipher.doFinal(plainText);
    System.out.println( "Finish encryption: " );
    System.out.println( new String(cipherText, "UTF8") );

    //
    // decrypt the ciphertext using the same key
    System.out.println( "\nStart decryption" );
    cipher.init(Cipher.DECRYPT_MODE, key);
    byte[] newPlainText = cipher.doFinal(cipherText);
    System.out.println( "Finish decryption: " );

    System.out.println( new String(newPlainText, "UTF8") );
  }
}

上面的代码工作得很好。我能看到结果和一切。但是我想修改它,如下所示,以便我可以将cipherText存储在文件中。然后另一个程序从文件读取加密的文本并将其解密。以下是我现在所做的,但我不明白如何进行。只是一点点提示如何进行将有所帮助。

The above code works great. I am able to see the result and everything. But I want to modify it as follows so that i can store the cipherText in a file. Then another program reads the encrypted text from the file and decrypts it. Below is what I have done till now, but I cannot understand how to proceed. Just a little hint on how to proceed will help.

import java.security.*;
import javax.crypto.*;

// encrypt and decrypt using the DES private key algorithm
public class PrivateExample {

  public static void main (String[] args) throws Exception {
    String text=new String();
    text="This is an encryption test";

    byte[] plainText = text.getBytes("UTF8");

    // get a DES private key
    System.out.println( "\nStart generating DES key" );
    KeyGenerator keyGen = KeyGenerator.getInstance("DES");
    keyGen.init(56);
    Key key = keyGen.generateKey();
    System.out.println( "Finish generating DES key" );
    //
    // get a DES cipher object and print the provider
    Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
    System.out.println( "\n" + cipher.getProvider().getInfo() );
    //
   // encrypt using the key and the plaintext
    System.out.println( "\nStart encryption" );
    cipher.init(Cipher.ENCRYPT_MODE, key);
    byte[] cipherText = cipher.doFinal(plainText);
    System.out.println( "Finish encryption: " );
    System.out.println( new String(cipherText, "UTF8") );

   //Now writing to an ouput file the cipherText
   try{
       FileOutputStream fs=new FileOutputStream("c:/test.txt");
      fs.write(cipherText);
     }catch(Exception e){
       e.printStackTrace();
     }
//How to proceed from here

}
}

现在这个程序的工作完成了。它已经将加密的字符串成功写入文件。新程序只需解密数据。如何从文件中读取加密的字节?新程序显然不了解原始字符串是什么,但是我将使用与算法中相同的键。请帮忙!我刚刚加密

Now this program's job is complete. It has successfully written the encrypted string into a file. The new program only has to decrypt the data. How do I read the encrypted bytes from the file? The new program obviously has no idea regarding what the original string was, but I will be using the same key as in the algorith. Please help! I am new to encryption

推荐答案

以下是将密钥写入文件的方式:

Here's how you write your key to a file:

        //Write your key to an output file.
        byte[] keyAsByte = key.getEncoded();
        FileOutputStream keyfos = new FileOutputStream("key.txt");
        keyfos.write(keyAsByte);
        keyfos.close();

我不建议将加密文本的密钥放在同一个文件中。

I wouldn't recommend putting the key with the encrypted text in the same file.

以下是您阅读加密文本和密钥的解密方式:

Here's how you read the ecrypted text and the key back and decrypt:

    //Read your key
    FileInputStream keyFis = new FileInputStream("key.txt");
    byte[] encKey = new byte[keyFis.available()];
    keyFis.read(encKey);
    keyFis.close();
    Key keyFromFile = new SecretKeySpec(encKey, "DES");
    //Read your text
    FileInputStream encryptedTextFis = new FileInputStream("test.txt");
    byte[] encText = new byte[encryptedTextFis.available()];
    encryptedTextFis.read(encText);
    encryptedTextFis.close();
    //Decrypt
    Cipher decrypter = Cipher.getInstance("DES/ECB/PKCS5Padding");
    decrypter.init(Cipher.DECRYPT_MODE, keyFromFile);
    byte[] decryptedText = decrypter.doFinal(encText);
    //Print result
    System.out.println("Decrypted Text: " + new String(decryptedText));

注意:我没有使用与写作相同的路径信息。

Note: I didn't use the same path as you for writing the information.

这篇关于从文件读取加密数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆