You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This feature will keep file secured from outside accessing.
I tried but different platforms works differently.
But still I think we can consider to merge this feature inside the addressbook.
code :
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.OutputStream;
importjavax.crypto.Cipher;
importjavax.crypto.CipherInputStream;
importjavax.crypto.CipherOutputStream;
importjavax.crypto.SecretKey;
importjavax.crypto.SecretKeyFactory;
importjavax.crypto.spec.DESKeySpec;
/** * method for encrypt / decrypt the files */publicclassCipherUnit {
privatestaticStringkey = "squirrel123"; // needs to be at least 8 characters for DESprivatestaticStringdest;
/** * class for encrypting files */publicstaticvoidencrypt(Stringpath) {
try {
dest = path;
FileInputStreamfis = newFileInputStream(path);
FileOutputStreamfos = newFileOutputStream("data/temp.xml");
encryptOrDecrypt(key, Cipher.ENCRYPT_MODE, fis, fos);
swapName("data/temp.xml");
} catch (Throwablee) {
e.printStackTrace();
}
}
/** * class for decrypting files */publicstaticvoiddecrypt(Stringpath) {
try {
dest = path;
FileInputStreamfis = newFileInputStream(path);
FileOutputStreamfos = newFileOutputStream("data/temp.xml");
encryptOrDecrypt(key, Cipher.DECRYPT_MODE, fis, fos);
swapName("data/temp.xml");
} catch (Throwablee) {
e.printStackTrace();
}
}
/** * translating method */publicstaticvoidencryptOrDecrypt(Stringkey, intmode, InputStreamis, OutputStreamos) throwsThrowable {
DESKeySpecdks = newDESKeySpec(key.getBytes());
SecretKeyFactoryskf = SecretKeyFactory.getInstance("DES");
SecretKeydesKey = skf.generateSecret(dks);
Ciphercipher = Cipher.getInstance("DES"); // DES/ECB/PKCS5Padding for SunJCEif (mode == Cipher.ENCRYPT_MODE) {
cipher.init(Cipher.ENCRYPT_MODE, desKey);
CipherInputStreamcis = newCipherInputStream(is, cipher);
doCopy(cis, os);
} elseif (mode == Cipher.DECRYPT_MODE) {
cipher.init(Cipher.DECRYPT_MODE, desKey);
CipherOutputStreamcos = newCipherOutputStream(os, cipher);
doCopy(is, cos);
}
}
/** * do a copy */publicstaticvoiddoCopy(InputStreamis, OutputStreamos) throwsIOException {
byte[] bytes = newbyte[64];
intnumBytes;
while ((numBytes = is.read(bytes)) != -1) {
os.write(bytes, 0, numBytes);
}
os.flush();
os.close();
is.close();
}
/** * used to replace files encypted/decrypted with the dectypted/encrypted one. */publicstaticbooleanswapName(Stringsource) {
Filetmp = newFile(source);
FileswapFile1 = newFile(dest);
returntmp.renameTo(swapFile1);
}
}
With the cipher unit, we can encrypt / decrypt the file whenever we want.
The text was updated successfully, but these errors were encountered:
This feature will keep file secured from outside accessing.
I tried but different platforms works differently.
But still I think we can consider to merge this feature inside the addressbook.
code :
With the cipher unit, we can encrypt / decrypt the file whenever we want.
The text was updated successfully, but these errors were encountered: