在java中加密文本文件的最简单方法 [英] Simplest way to encrypt a text file in java

查看:41
本文介绍了在java中加密文本文件的最简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的 School 项目,我必须证明我可以在程序中使用文件处理.为此,我做了一个非常简单的登录过程,您可以创建一个帐户,将用户名和密码写入位于资源文件夹中的文本文件.显然这根本没有安全性,因为它不是为了展示文件处理而设计的安全,但是我的老师说我应该尝试为文件添加一些加密以取得更好的成绩.

For my School project I had to show that I can utilize file handling within a program. For this I made a very simple login process that you can create an account on that writes a username and password to a text file located in the resource folder. Obviously this has no security at all as it wasn't designed to be secure just to showcase file handling however my teacher has said that I should attempt to add some encryption to the file as well to get a better grade.

我做了一些研究,很多人都在推荐 DES.

I have done some research and many people are recommending DES.

我遇到的问题是我的项目时间不多了,需要尽快完成.使用 DES 似乎需要一段时间才能实现所有额外的代码.

The problem I'm having is I don't have much time left for my project and need to finish it asap. Using DES seems like it would take a while to implement all the extra code.

在我的程序中,我使用一个简单的 lineNumberReader 逐行读取文件.为了写入文件,我使用了 BufferedWriter.

In my program I am using a simple lineNumberReader to read the files line by line. To write to the files I am using a BufferedWriter.

有没有办法非常简单地加密这些数据?它不必非常安全,但我需要证明我至少尝试过加密数据.加密和解密都将在同一个应用程序上完成,因为数据没有被传输.

Is there anyway to encrypt this data very simply? It doesn't have to be very secure but I need to show that I have atleast attempted to encrypt the data. The encryption and decryption would all be completed on the same application as data isn't being transferred.

可能是我自己创建一个非常简单的加密和解密算法的方法吗?

Potentially a way I can create a very simple encryption and decryption algorithm myself?

推荐答案

试试这个,...很简单

Try this,... Its pretty simple

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

public class HelloWorld{
    public static void main(String[] args) {

        try{
            KeyGenerator keygenerator = KeyGenerator.getInstance("DES");
            SecretKey myDesKey = keygenerator.generateKey();

            Cipher desCipher;
            desCipher = Cipher.getInstance("DES");


            byte[] text = "No body can see me.".getBytes("UTF8");


            desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
            byte[] textEncrypted = desCipher.doFinal(text);

            String s = new String(textEncrypted);
            System.out.println(s);

            desCipher.init(Cipher.DECRYPT_MODE, myDesKey);
            byte[] textDecrypted = desCipher.doFinal(textEncrypted);

            s = new String(textDecrypted);
            System.out.println(s);
        }catch(Exception e)
        {
            System.out.println("Exception");
        }
    }
}

所以基本上在写入文件之前你会加密,阅读后你需要解密它.

So basically before writing to file you will encrypt and after reading you will need to decrypt it.

这篇关于在java中加密文本文件的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆