私は、暗号技術とアルゴリズムをファミリラにするために、小さな安全なストレージサーバシミュレーションを作成しています。私はファイルの内容を解読していました。プロジェクトはここにすべて入れるには少し大きめなので、コードからは例外(私が使用する方法と問題が発生する場所)を入れます。 ファイルを解読しようとすると、BadPaddingExceptionが発生するという問題があります。javax.crypto.BadPaddingException:ファイルを復号化するときに最後のブロックが正しく埋め込まれていない
String test = "Test symmetric file enc/dec";
byte[] tesT = aCrypto.SymmetricFileEncryption(test.getBytes(), sessionKey);
System.out.println("ENC FROM SERVER " + new String(tesT));
String test2 = new String(aCrypto.SymmetricFileDecription(tesT, sessionKey));
System.out.println("DEC FROM SERVER: " + test2);
それがうまく機能:私は他のような後の対称ファイルENCと12月1を使用しようとすると、メインのクリプトロジックは
public class Crypto {
private Cipher asymmCipher;
//one cipher for asymmetric and one for symmetric
private Cipher symmCipher;
public IvParameterSpec iv;
SecureRandom sr;
KeyGenerator kg;
public Crypto() throws NoSuchAlgorithmException, NoSuchPaddingException {
this.asymmCipher = Cipher.getInstance("RSA");
this.symmCipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
}
/*
Method for symmetric encription of file
*/
public byte[] SymmetricFileEncryption(byte[] file, SecretKey key)
throws InvalidKeyException, IllegalBlockSizeException,
BadPaddingException, NoSuchAlgorithmException {
this.symmCipher.init(Cipher.ENCRYPT_MODE, key);
return this.symmCipher.doFinal(file);
}
/*
Method for symmetric decription of file
*/
public byte[] SymmetricFileDecription(byte[] file, SecretKey key)
throws InvalidKeyException, IllegalBlockSizeException,
BadPaddingException, InvalidAlgorithmParameterException {
this.symmCipher.init(Cipher.DECRYPT_MODE, key);
return this.symmCipher.doFinal(file);
}
/*
Method for write encrypted file
*/
public void writeToFile(File output, byte[] data, SecretKey key)
throws IllegalBlockSizeException, BadPaddingException,
IOException, InvalidKeyException, NoSuchAlgorithmException {
FileOutputStream fos = new FileOutputStream(output);
byte[] encContent = SymmetricFileEncryption(data, key);
fos.write(encContent);
fos.flush();
fos.close();
}
public byte[] readFromFile(File input, SecretKey key)
throws IllegalBlockSizeException, BadPaddingException,
IOException, InvalidKeyException, NoSuchAlgorithmException, InvalidAlgorithmParameterException {
byte[] fileContent = new byte[(int) input.length()];
FileInputStream fis = new FileInputStream(input);
fis.read(fileContent);
fis.close();
return SymmetricFileDecription(fileContent, key);
}
}
を実装されている暗号化クラスの exceptsで。新しいファイルを作成するためと編集ファイルのサーバーから除き
(私は読んで解読必要がある場所)
if ("new".equals(option)) {
String fileName = aCrypto.DecryptStringSymmetric((String) ois.readObject(), sessionKey);
System.out.println("FILE NAME SERVER POSLAN IS UPANEL CONTROLLERA : " + fileName);
// String cFileName = aCrypto.EncryptStringSymmetric(fileName, sessionKey);
String formatedEncFileName = aCrypto.encodeWithSHA256(fileName).replaceAll("\\/", "");
File f = new File("src/server/users/" + userName + "/" + formatedEncFileName);
if (!f.exists()) {
f.createNewFile();
System.out.println("FILE CREATED!");
fileNamesMap.put(formatedEncFileName, fileName);
serialize(fileNamesMap);
System.out.println("MAP VALUE FOR " + fileNamesMap.get(formatedEncFileName));
}
byte[] file = aCrypto.SymmetricFileDecription(((byte[]) ois.readObject()), sessionKey);
System.out.println("FILE CONTENT : " + new String(file));
// String encContent = aCrypto.EncryptStringSymmetric(new String(file), sessionKey);
aCrypto.writeToFile(f, file, sessionKey);
oos.writeObject(aCrypto.EncryptStringSymmetric(((f.exists()) ? "true" : "false"), sessionKey));
changeFileWatcher(userName);
}
if (("edit").equals(option)) {
fileName = aCrypto.DecryptStringSymmetric((String) ois.readObject(), sessionKey);
byte[] content = aCrypto.readFromFile(new File(PATH + userName + "/" + (String) getKeyFromValue(fileNamesMap, fileName.split("/")[4])), sessionKey); //Exception in readFromFile
String fileContent = aCrypto.DecryptStringSymmetric(new String(content), sessionKey);
oos.writeObject(aCrypto.EncryptStringSymmetric(fileContent, sessionKey));
System.out.println("NAME FROM SERVER IN EDIT : " + fileName);
}
if (("modify").equals(option)) {
String editedFileContent = aCrypto.DecryptStringSymmetric((String) ois.readObject(), sessionKey);
System.out.println("NAME FROM SERVER IN MODIFY: " + fileName);
String encrytedFileContent = aCrypto.EncryptStringSymmetric(editedFileContent, sessionKey);
File f = new File(PATH + userName + "/" + (String) getKeyFromValue(fileNamesMap, fileName.split("/")[4]));
aCrypto.writeToFile(f, encrytedFileContent.getBytes(), sessionKey);
oos.writeObject(aCrypto.EncryptStringSymmetric("true", sessionKey));
}
別の例 - 私はWRITETOFILE方法とテスト書き込みに印刷して読んで置く:
String test = "Test symmetric file enc/dec";
File fileTest = new File("src/server/testFileEnc");
if(!fileTest.exists()) {
fileTest.createNewFile();
}
aCrypto.writeToFile(fileTest, test.getBytes(), sessionKey);
String decryptedFromFile = new String(aCrypto.readFromFile(fileTest , sessionKey));
System.out.println("DECRYPTED DATA " + decryptedFromFile);
出力:
ENCRYPTED DATA: ×^đzŞĘňmř”›&şH«ťq
DECRYPTED DATA : Test symmetric file enc/dec
BadPaddingExceptionは通常、何らかの原因でデータが混乱しているだけの症状です。何が間違っているのかを特定するには、事を簡素化する必要があります。まずファイルの読み取り/書き込みが行われずに暗号化と復号化が行われていることと、サーバーのさまざまなことなどを確認します。 –
ありがとうございます。私はすべての方法をテストしましたが、うまくいっているようです: -/ 私が追加した例を使って質問を編集したところ、うまくいきました。 しばらくしてからファイルを読み込んだときに問題が発生しました: -/ – MGKP