これは私の最初のJavaプロジェクトです。私はAESを使って簡単なテキスト暗号化ツールを作ることにしました。JavaのシンプルなAES暗号化ツール
私は取得していますエラーは次のとおりです。事前に
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.Key;
import javax.crypto.*;
import java.util.*;
public class Encryptor {
public static void main(String[] args) throws Exception {
String FileName = "encryptedtext.txt";
String FileName2 = "decryptedtext.txt";
Scanner input = new Scanner(System.in);
System.out.println("Enter your 16 character key here:");
String EncryptionKey = input.next();
KeyGenerator KeyGen = KeyGenerator.getInstance("AES");
KeyGen.init(128);
Cipher AesCipher = Cipher.getInstance("AES");
System.out.println("Enter text to encrypt or decrypt:");
String Text = input.next();
System.out.println("Do you want to encrypt or decrypt (e/d)");
String answer = input.next();
if (answer.equalsIgnoreCase("e")){
byte[] byteKey = (EncryptionKey.getBytes());
byte[] byteText = (Text).getBytes();
AesCipher.init(Cipher.ENCRYPT_MODE, byteKey); // ERROR LINE
byte[] byteCipherText = AesCipher.doFinal(byteText);
Files.write(Paths.get(FileName), byteCipherText);
}
else if (answer.equalsIgnoreCase("d")){
byte[] byteKey = (EncryptionKey.getBytes());
byte[] byteText = (Text).getBytes();
byte[] cipherText = Files.readAllBytes(Paths.get(FileName));
AesCipher.init(Cipher.DECRYPT_MODE, byteKey); // ERROR LINE
byte[] bytePlainText = AesCipher.doFinal(cipherText);
Files.write(Paths.get(FileName2), bytePlainText);
}
}
}
ありがとう:暗号引数(int, byte[])
コードには適用されませんタイプのメソッドinit(int, Key)
! :)
farily良いサンプルがここにありますhttp://stackoverflow.com/questions/15554296/simple-java-aes-encrypt-decrypt-example 。 命名規則にはキャメルケースを使用してください。:) – GPI
あなたの最初のJavaプロジェクトですが、このプロジェクトの最初の単体テストを書くようにしてください:) –
一般的なアドバイス:**完全な暗号文字列を使用してください** 'Cipher.getInstance(" AES ");' mayデフォルトのセキュリティプロバイダに応じて異なる暗号が生成されます。おそらく '' AES/ECB/PKCS5Padding ''という結果になるでしょうが、必ずしもそうである必要はありません。変更すると、異なるJVM間の互換性が失われます。 –