Dobry den
Trochu si ted hraju s sifrovanim a narazil jsem na jeden problem:
Mam instanci Properties, kterou pomoci metody store() ukladam do souboru.
Dale mam metody pro sifrovani a desifrovani pomoci DESu, ktere de/sifruji
vstupni/vystupni proud.
Problem je, ze pokud Properties zasifruji, ulozim a zpetne nahraji a
desifruji, tak se ztrati konec.
S sifrovanim teprve zacinam, poradite mi kde delam chybu?
Takhle vypada vysledny desifrovany soubor:
-----
#Sat Feb 17 12:10:29 CET 2007
aaaaaa=aaaaaa
bbbbb
-----
Jak je videt, část zaznamu u konce uplne chybi
Zde uvadim, priklady zdrojaku:
Cast vytvoreni a nahrani properties:
-----------------------------------------------------
Properties properties = new Properties();
properties.setProperty("aaaaaa","aaaaaa");
properties.setProperty("bbbbbb","bbbbbb");
File file = new File("soubor.txt");
if( !file.exists() ) {
file.createNewFile();
}
properties.store(DESCipher.encryptFile(file), null);
try {
properties.load(DESCipher.decryptFile(licenceFile));
} catch (IOException ex) {}
Cela trida pro de/sifrovani:
-----------------------------------------------------
public class DESCipher {
private static byte[] iv = new byte[] { (byte) 0x8E, 0x12, 0x39,
(byte) 0x9C, 0x07, 0x72, 0x6F, 0x5A };
public static OutputStream encryptFile(File inFile) throws
NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, FileNotFoundException,
InvalidAlgorithmParameterException {
AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, new Klic(), paramSpec);
CipherOutputStream cos = new CipherOutputStream(new
FileOutputStream(inFile), cipher);
return cos;
}
public static InputStream decryptFile(File inFile) throws
NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
InvalidAlgorithmParameterException, FileNotFoundException {
AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, new Klic(), paramSpec);
CipherInputStream cis = new CipherInputStream(new
FileInputStream(inFile), cipher);
return cis;
}
static class Klic implements Key {
public String getFormat() {
return "RAW";
};
public String getAlgorithm() {
return "DES";
};
public byte[] getEncoded() {
return byte[] r = { 0, 6, 12, 21, 48, 96, 48, 24 };
}
};
}