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

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

问题描述

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

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.

我遇到的问题是我没有多少时间留给我的项目,需要完成它的asap。使用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天全站免登陆