秘密鍵を使用した暗号化に関する IBM チュートリアルを行っていました。そして、私は以下のようにコードを書きました
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をファイルに保存できるように、次のように変更したいと思います。次に、別のプログラムがファイルから暗号化されたテキストを読み取り、復号化します。以下は私が今までやってきたことですが、どうすればいいのかわかりません。進め方についてのちょっとしたヒントが役に立ちます。
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
}
}
これで、このプログラムの作業は完了です。暗号化された文字列がファイルに正常に書き込まれました。新しいプログラムは、データを復号化するだけです。ファイルから暗号化されたバイトを読み取るにはどうすればよいですか? 新しいプログラムは明らかに元の文字列が何であったかを認識していませんが、アルゴリズムと同じキーを使用します。助けてください!暗号化は初めてです