そのため、ファイルを暗号化してから復号化するこのプログラムをコーディングしてきました。疑似ランダムキージェネレーターにユーザー入力をシードとして取り込んで、そこからキーを作成できるようにしたいと考えています。キーを文字列に依存させたいことに注意してください(つまり、シード「hello」を数回入力すると、毎回同じキーでファイルが暗号化されます)。最終的には暗号化と復号化の機能を分割するためです2 つのファイル。
これは、SecureRandom に基づく私の最初の試みです。コードは他にもありますが、関連するのは main だけです。
protected static final String ALGORITHM = "AES";
public static void main(String args[]) {
String stringKey = args[1];
byte[] seedArray = stringKey.getBytes();
SecureRandom sRandom = new SecureRandom(seedArray);
byte[] keyArray = new byte[16];
SecretKey sKey = new SecretKeySpec(keyArray, ALGORITHM);
try
{
Encrypter2 encrypter = new Encrypter2(sKey);
FileInputStream efis = new FileInputStream(args[0]);
FileOutputStream efos = new FileOutputStream("Encrypted");
encrypter.encrypt(efis, efos);
FileInputStream dfis = new FileInputStream("Encrypted");
FileOutputStream dfos = new FileOutputStream("Decrypted.txt");
encrypter.decrypt(dfis, dfos);
} catch (FileNotFoundException e1) {
System.out.println("File not found.");
e1.printStackTrace();
} catch (Exception e) {
System.out.println(e);
}
}
これにより、Java 1.7 では文字列入力に対して一意のキーが作成されますが、Java 1.6 ではランダム化されます。ユーザーが入力した文字列に依存する、ユーザーがシードしたキーを生成する別の方法はありますか? 前もって感謝します!