1

ここで私のプログラムの助けを探しています。このプログラムは、操作の CBC モードに基づいて、DES 対称暗号を使用してフレーズを暗号化および復号化できます。

私が今やろうとしているのは、 CBC モードの操作を使用して、DES 対称暗号を使用してテキスト ファイルのコンテンツを暗号化および復号化できるように変更することです。

誰でもこれで私を助けてくれますか?

ありがとうございました!

import java.security.*;

import javax.crypto.*;

import java.security.spec.*;

import javax.crypto.spec.*;

import javax.crypto.spec.IvParameterSpec;



public class myProgram
{
  public static void main (String[] args) throws Exception
  {

    String text = "Hello World;

    SecureRandom sr = new SecureRandom();
    byte [] iv = new byte[8];
    sr.nextBytes(iv);
    IvParameterSpec IV = new IvParameterSpec(iv);
    KeyGenerator kg = KeyGenerator.getInstance("DES");
    Key mykey = kg.generateKey();
    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, mykey,IV);

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



    byte[] ciphertext = cipher.doFinal(plaintext);

    System.out.println("\n\nCiphertext: ");
    for (int i=0;i<ciphertext.length;i++) {

        if (chkEight(i)) {
            System.out.print("\n");
        }
        System.out.print(ciphertext[i]+" ");
    }
4

1 に答える 1

0

ファイルの読み書きの方法を知りたいだけですか?

apache.commons.io.FileUtilsを見てください。ファイルから文字列を読み書きするための両方の方法を提供します。ここではいくつかの例を示します。

// info.txt represents the path to the file
File someFile = new File("info.txt");

// Create String from File
try {
  String filecontent = FileUtils.readFileToString(someFile, Charset.defaultCharset());
  System.out.println(filecontent);

} catch (IOException e) {
  e.printStackTrace();
}

// Encode / Decode string here

// Write String to file     
try {
  FileUtils.writeStringToFile(someFile, "your new file content string", Charset.defaultCharset());
  System.out.println("Success!");

} catch (IOException e) {
  e.printStackTrace();
}

テキスト ファイルの文字セットに応じて、別のCharsetを使用したい場合があります。

于 2014-01-10T12:25:50.407 に答える