ありがとう、私を助けてくれてありがとう!(AES暗号化)コードの脆弱性、どうすればよいですか? [コード提供] [Java]
THIS POSTがLESS情報については、編集された は、編集部分を見る
まあ..私はこの問題について研究を私たちが過ごす持っていると私は、コードの作業を一枚になってしまった
しかし、暗号化間違いを犯す場所ではなく、自分のコードが実際に安全であるかどうか尋ねたいと思っていました。 JFPicard
- 私は私のコードが...import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
public class EncryptFile{
private static final String FILE_IN = "./EncryptFile.java";
private static final String FILE_ENCR = "./EncryptFile_encr.java";
private static final String FILE_DECR = "./EncryptFile_decr.java";
public static void main(String []args){
try
{
Encryption("passwordisnottheactual", Files.readAllBytes(Paths.get(FILE_IN)));
Decryption("passwordisnottheactual");
}catch(Exception e){
System.out.println(e.getMessage());
}
}
private static void Encryption(String Key, byte[] byteArray) throws Exception
{
// Decode the base64 encoded Key
byte[] decodedKey = Base64.getDecoder().decode(Key);
// Rebuild the key using SecretKeySpec
SecretKey secretKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES");
// Cipher gets AES Algorithm instance
Cipher AesCipher = Cipher.getInstance("AES");
//Initialize AesCipher with Encryption Mode, Our Key and A ?SecureRandom?
AesCipher.init(Cipher.ENCRYPT_MODE, secretKey, new SecureRandom());
byte[] byteCipherText = AesCipher.doFinal(byteArray);
//Write Bytes To File
Files.write(Paths.get(FILE_ENCR), byteCipherText);
}
private static void Decryption(String Key) throws Exception
{
//Ddecode the base64 encoded string
byte[] decodedKey = Base64.getDecoder().decode(Key);
//Rebuild key using SecretKeySpec
SecretKey secretKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES");
//Read All The Bytes From The File
byte[] cipherText = Files.readAllBytes(Paths.get(FILE_ENCR));
//Cipher gets AES Algorithm Instance
Cipher AesCipher = Cipher.getInstance("AES");
//Initialize it in Decrypt mode, with our Key, and a ?SecureRandom?
AesCipher.init(Cipher.DECRYPT_MODE, secretKey, new SecureRandom());
byte[] bytePlainText = AesCipher.doFinal(cipherText);
Files.write(Paths.get(FILE_DECR), bytePlainText);
}
}
EDIT
単純なJava AESの暗号化/復号化の例の可能な複製であるので、プログラムにそれを実装したいので、それは私にとって本当に重要です
この回答はIVParameterSpecを使用してください。 このコード行が実際に安全かどうか、悪い場合は
AesCipher.init(Cipher.DECRYPT_MODE, secretKey, new SecureRandom());
私はnew SecureRandom()
毎回、 を使用しているため、私は誰もがこのようなSecureRandomオブジェクトを使用して見ていません。
あなたのコードを見直すつもりはないので、http://codereview.stackexchange.com/を試してみてください。 –
可能性の重複[単純なJava AESの暗号化/復号化の例を](http://stackoverflow.com/questions/15554296/simple-java-aes-encrypt-decrypt-example) – JFPicard