java开发文件夹加密解密程序_Java应用开发:文件加密解密

本人记性不是很好,总是忘记些重要信息,特别是密码,很多时间登录都要试好几个密码才成功。所以想到了把

这些容易忘记的重要信息记录到一个文本文件上,再对其加密(不易忘记的密码)。对文件的加密,自己写了个小工具。

下面看下工具的主要设计:

主界面(只注重实用性,所以界面写得很简单)

0818b9ca8b590ca3270a3433284dd417.png

功能也很简单,就只有加密和解密,选择要文件,输入口令,即可对文件实行加密/解密。

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

加密原理:

采用字节流读取原文件数据,然后异或一个int值,即用户输入的口令,再写入新的文件。这样新的文件中的数据

就是原文件中的数据通过与口令异或运算后的值,打开查看就显示为乱码。

加密前文件内容:

0818b9ca8b590ca3270a3433284dd417.png

加密后:

0818b9ca8b590ca3270a3433284dd417.png

解密原理:

数据A异或B再异或B(即A^B^B),结果还是A,所以可以通过再次异或口令还原文件。

主要代码:

加密方法:

传入一个File对象和加密口令,每读取一个字节,通过异或运算后写入到一个新文件中。

/**

* 把文件按照输入的口令加密

*

* @param file

* 加密的目标文件

* @param code

* 加密口令

*/

public static boolean encrypt(File file, int code) {

if (!validate(file)) {

return false;

}

File tempFile = new File(file.getAbsoluteFile() + SUFFIX);

FileInputStream fis = null;

FileOutputStream fos = null;

try {

fis = new FileInputStream(file);

fos = new FileOutputStream(tempFile);

while (fis.available() > 0) {

fos.write(fis.read() ^ code);

}

} catch (Exception e) {

e.printStackTrace();

return false;

} finally {

closeStream(fis, fos);

}

return true;

}

解密方法:

传入需要解决的文件和口令,同样每读取一个字节,通过异或运算后写入到一个新文件中,得到的新文件

的内容即为加密前文件的内容。

/**

* 把文件按照输入的口令解密

*

* @param file

* 解密的目标文件

* @param code

* 解密口令

*/

public static boolean decrypt(File file, int code) {

if (!validate(file)) {

return false;

}

FileInputStream fis = null;

FileOutputStream fos = null;

try {

String path = file.getAbsolutePath();

if (!path.endsWith(SUFFIX)) {

return false;

}

File newFile = new File(path.substring(0, path.lastIndexOf(SUFFIX)));

fis = new FileInputStream(file);

fos = new FileOutputStream(newFile);

while (fis.available() > 0) {

fos.write(fis.read() ^ code);

}

} catch (Exception e) {

e.printStackTrace();

return false;

} finally {

closeStream(fis, fos);

}

return true;

}         本程序只是对文件的一般加密,还有待改进,如果大家有什么意见或建议,欢迎提出。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java提供了很多加密算法,可以用于加密解密jar文件。常用的加密算法有AES、DES、RSA等。 下面是一个使用AES算法对jar文件进行加密解密的示例代码: ```java import java.io.*; import java.security.*; import javax.crypto.*; import javax.crypto.spec.*; public class JarEncryptor { private static final String ALGO = "AES"; private static final String TRANSFORMATION = "AES/CBC/PKCS5Padding"; private static final byte[] IV = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }; public static void encrypt(String key, File inputFile, File outputFile) throws Exception { doCrypto(Cipher.ENCRYPT_MODE, key, inputFile, outputFile); } public static void decrypt(String key, File inputFile, File outputFile) throws Exception { doCrypto(Cipher.DECRYPT_MODE, key, inputFile, outputFile); } private static void doCrypto(int cipherMode, String key, File inputFile, File outputFile) throws Exception { Key secretKey = new SecretKeySpec(key.getBytes(), ALGO); Cipher cipher = Cipher.getInstance(TRANSFORMATION); IvParameterSpec iv = new IvParameterSpec(IV); cipher.init(cipherMode, secretKey, iv); FileInputStream inputStream = new FileInputStream(inputFile); byte[] inputBytes = new byte[(int) inputFile.length()]; inputStream.read(inputBytes); byte[] outputBytes = cipher.doFinal(inputBytes); FileOutputStream outputStream = new FileOutputStream(outputFile); outputStream.write(outputBytes); inputStream.close(); outputStream.close(); } } ``` 在上面的代码中,我们使用AES算法和CBC模式对jar文件进行加密解密加密解密的关键是密钥,我们需要提供一个密钥来进行加密解密操作。 以下是使用示例: ```java public static void main(String[] args) throws Exception { String key = "mysecretkey"; File inputFile = new File("test.jar"); File encryptedFile = new File("test_encrypted.jar"); File decryptedFile = new File("test_decrypted.jar"); JarEncryptor.encrypt(key, inputFile, encryptedFile); JarEncryptor.decrypt(key, encryptedFile, decryptedFile); } ``` 在上面的示例中,我们使用“mysecretkey”作为密钥对“test.jar”进行加密,然后再对加密后的文件进行解密,最终得到的文件名为“test_decrypted.jar”。 需要注意的是,加密解密过程中需要使用相同的密钥,否则无法正确解密。同时,密钥应该足够复杂和安全,以保证加密后的文件不会被破解。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值