私は、2 つの別々のソフトウェアを使用して、ネットワーク接続の両端で情報の暗号化と復号化を行う Java ソフトウェアに取り組んでいます。これを簡単にするために、データの暗号化を処理する Cryptographer クラスを 1 つ用意しています。現時点では、Controller (接続の一方の側) と Agent (もう一方の側) の両方がこのクラスを使用して、2 つのプログラム間で共有されるパスワードに基づいて SecretKey を生成します。
キーは、Cryptographer クラスの次の関数で生成されます。
public SecretKey generateKey(String key) {
this._paramSpec = new PBEParameterSpec(this.SALT, this.ITERATION_COUNT);
PBEKeySpec spec = new PBEKeySpec(key.toCharArray());
SecretKeyFactory fac = null;
try {
fac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
} catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
System.err.println("[ERR] Cryptographer could not create a SecretKeyFactory due to an unsupported algorithm.");
}
try {
if (fac == null)
return null;
return fac.generateSecret(spec);
} catch (InvalidKeySpecException ex) {
System.err.println("[ERR] Cryptographer could not generate a SecretKey due to an invalid Key Specification.");
ex.printStackTrace();
return null;
}
}
暗号化自体は、encrypt 関数で行われます。
public byte[] encrypt(byte[] message) {
try {
this._cipher.init(Cipher.ENCRYPT_MODE, this._key, this._paramSpec);
} catch (InvalidKeyException ex) {
System.err.println("[ERR] Cryptographer could not encrypt a message because the provided key is invalid.");
ex.printStackTrace();
return new byte[0];
} catch (InvalidAlgorithmParameterException ex) {
System.err.println("[ERR] Cryptographer could not encrypt a message because the parameters are invalid.");
ex.printStackTrace();
return new byte[0];
}
try {
return this._cipher.doFinal(message);
} catch (IllegalBlockSizeException ex) {
System.err.println("[ERR] Cryptographer could not encrypt a message due to an illegal block size.");
ex.printStackTrace();
return new byte[0];
} catch (BadPaddingException ex) {
System.err.println("[ERR] Cryptographer could not encrypt a message due to bad padding.");
ex.printStackTrace();
return new byte[0];
}
}
そして、decrypt 関数によって復号化されます。
public byte[] decrypt(byte[] message) {
try {
this._cipher.init(Cipher.DECRYPT_MODE, this._key, this._paramSpec);
} catch (InvalidKeyException ex) {
System.err.println("[ERR] Cryptographer could not decrypt a message because the provided key is invalid.");
return new byte[0];
} catch (InvalidAlgorithmParameterException ex) {
System.err.println("[ERR] Cryptographer could not decrypt a message because the parameters are invalid.");
}
try {
return this._cipher.doFinal(message);
} catch (IllegalBlockSizeException ex) {
System.err.println("[ERR] Cryptographer could not decrypt a message due to an illegal block size.");
return new byte[0];
} catch (BadPaddingException ex) {
System.err.println("[ERR] Cryptographer could not decrypt a message due to bad padding.");
return new byte[0];
}
}
暗号化は正常に機能しているようですが、受信側でシリアル化されたオブジェクトを復号化しようとすると、InvalidKeyException がスローされます。コントローラとエージェントで個別に生成されたキーを比較すると、同じパスワードをソースとしているにもかかわらず、同一のキーを生成していないことがわかります。
さて、私は Java での暗号化は初めてなので、ここで何か間違ったことをしている可能性は十分にあります。これには、私が見逃しているランダムな要素があるようです。目標は、接続の両側で同一のパスワードから同一のキーを生成することです。それで、明らかに間違っていることはありますか?さらにコードが必要な場合は、お知らせください。投稿させていただきます。