暗号化復号化アプリケーションを作成しようとしています。私は2つのクラスを持っています - 一つはキーを生成する関数、もう一つは暗号化と復号化、もう一つはJavaFX GUIです。 GUIクラスでは、私は4つのテキストエリアを持っています:1番目は暗号化するテキストを書き、2番目は暗号化されたテキスト、3番目はキー()、4番目は復号化されたテキストです。文字列からSecretKeyを正しく再作成する方法
問題は、私はテキストを復号化できません。ここに私は[email protected]
と私はjavax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
を取得しています:私はこのような重要なルックス暗号化すると
String encodedKey = textAreaKey.getText();
byte[] decodedKey = Base64.getDecoder().decode(encodedKey);
SecretKey klucz = new SecretKeySpec(decodedKey, "DESede");
:[email protected]
を、私はそれを再作成しようとすると、私はこのようなのSecretKeyを再作成しようとしていますファーストクラス:
public class Encryption {
public static SecretKey generateKey() throws NoSuchAlgorithmException {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
KeyGenerator keygen = KeyGenerator.getInstance("DESede");
keygen.init(168);
SecretKey klucz = keygen.generateKey();
return klucz;
}
static byte[] encrypt(byte[] plainTextByte, SecretKey klucz)
throws Exception {
Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, klucz);
byte[] encryptedBytes = cipher.doFinal(plainTextByte);
return encryptedBytes;
}
static byte[] decrypt(byte[] encryptedBytes, SecretKey klucz)
throws Exception {
Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, klucz);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
return decryptedBytes;
}
}
編集
btnEncrypt.setOnAction((ActionEvent event) -> {
try {
String plainText = textAreaToEncrypt.getText();
SecretKey klucz = Encryption.generateKey();
byte[] plainTextByte = plainText.getBytes();
byte[] encryptedBytes = Encryption.encrypt(plainTextByte, klucz);
String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes);
textAreaEncryptedText.setText(encryptedText);
byte[] byteKey = klucz.getEncoded();
String stringKey = Base64.getEncoder().encodeToString(byteKey);
textAreaKey.setTextstringKey
} catch (Exception ex) {
ex.printStackTrace();
}
});
btnDecrypt.setOnAction((ActionEvent event) -> {
try {
String stringKey = textAreaKey.getText();
byte[] decodedKey = Base64.getDecoder().decode(encodedKey);
SecretKey klucz2 = new SecretKeySpec(decodedKey, "DESede");
String encryptedText = textAreaEncryptedText.getText();
byte[] encryptedBytes = Base64.getDecoder().decode(encryptedText.getBytes());
byte[] decryptedBytes = Encryption.decrypt(encryptedBytes, klucz2;
String decryptedText = Base64.getEncoder().encodeToString(decryptedBytes);
textAreaDecryptedText.setText(decryptedText);
} catch (Exception ex) {
ex.printStackTrace();
}
});
あなたが投稿したコードを逐語的に使用しましたが、再現できませんでした。だから問題はあなたが投稿していないコードになければならない。 https://gist.github.com/jnizet/5ce2971a94fb6f88a746607243613ca7 –
1. 3DESが必要な新しいプロジェクト、学校プロジェクトには使用すべきではない3DES(DESede)を使用する理由は何ですか? 2. 3DESキーは168ビットのキーで24バイト、 "[email protected]"は3DESキーではありません。 3DESキーは24バイトですが、一部の実装では短いキーを受け入れ、24バイトのキーを作成する部分を複製します。 3.なぜ、Base64エンコードの3番目のフィールドは、テキストではありませんか? 4.パディングは言及されていませんが、すべてのパラメータを明示的に指定することをお勧めします。 5. 'Cipher.ENCRYPT_MODE'の値は何ですか? – zaph
1.はい、それは学校のプロジェクト です。2.正しい3DESキーを提供する方法を理解することに問題があったため、その部分がインターネットのどこかに見つかりました。私はこれの代わりに何を使うべきですか? 3.最初に、暗号化によって、私はSecretKeyをStringに変換するので、 'textAreaKey'にそれを表示することができます。次に、解読することによって、 'textAreaKey'のテキストをSecretKey(質問に追加されたコード)に変換しています。 4.ここで何を意味するのか分かりません。私はむしろ初心者です。 5.「System.out.println( "Value of Cipher.ENCRYPT_MODE:" + Cipher.ENCRYPT_MODE); "これはちょうど' 1'を返します。 – zygmunt