用java加密文本文件的最简单方法
对于我的学校项目,我必须证明我可以在程序中使用文件处理。为此,我做了一个非常简单的登录过程,您可以创建一个帐户,将用户名和密码写入位于资源文件夹中的文本文件。显然,这根本没有安全性,因为它不是为了展示文件处理而设计的,但是我的老师说,我应该尝试给文件添加一些加密,以获得更好的分数 我做了一些研究,很多人推荐DES 我的问题是我没有太多的时间来完成我的项目,需要尽快完成它。使用DES似乎需要一段时间来实现所有额外的代码 在我的程序中,我使用一个简单的行号阅读器逐行读取文件。要写入文件,我正在使用BufferedWriter 是否有任何方法可以非常简单地加密这些数据?它不必非常安全,但我需要证明我至少尝试过加密数据。加密和解密都将在同一个应用程序上完成,因为数据不会被传输用java加密文本文件的最简单方法,java,security,encryption,text-files,Java,Security,Encryption,Text Files,对于我的学校项目,我必须证明我可以在程序中使用文件处理。为此,我做了一个非常简单的登录过程,您可以创建一个帐户,将用户名和密码写入位于资源文件夹中的文本文件。显然,这根本没有安全性,因为它不是为了展示文件处理而设计的,但是我的老师说,我应该尝试给文件添加一些加密,以获得更好的分数 我做了一些研究,很多人推荐DES 我的问题是我没有太多的时间来完成我的项目,需要尽快完成它。使用DES似乎需要一段时间来实现所有额外的代码 在我的程序中,我使用一个简单的行号阅读器逐行读取文件。要写入文件,我正在使用B
我可以自己创建一个非常简单的加密和解密算法吗?您可以使用一个简单的ceasar密码() 发现于
请注意,Java有用于加密的本机解决方案,当涉及到密码时,最好只对它们进行散列并比较散列,因为通常不需要对它们进行解密。一种简单有趣的置乱算法将是最好的。不是真正的安全加密,但说真的,这是一个学校的工作,这是可怕的。使用简单的替代加密算法,将每个字符更改为数字或其他字符
Bouncy Castle Crypto API是Java中的轻量级加密API
import org.bouncycastle.crypto.*;
import org.bouncycastle.crypto.engines.*;
import org.bouncycastle.crypto.modes.*;
import org.bouncycastle.crypto.params.*;
// A simple example that uses the Bouncy Castle
// lightweight cryptography API to perform DES
// encryption of arbitrary data.
public class Encryptor {
private BufferedBlockCipher cipher;
private KeyParameter key;
// Initialize the cryptographic engine.
// The key array should be at least 8 bytes long.
public Encryptor( byte[] key ){
/*
cipher = new PaddedBlockCipher(
new CBCBlockCipher(new DESEngine()));
*/
cipher = new PaddedBlockCipher(
new CBCBlockCipher(new BlowfishEngine()));
this.key = new KeyParameter( key );
}
// Initialize the cryptographic engine.
// The string should be at least 8 chars long.
public Encryptor( String key ){
this( key.getBytes());
}
// Private routine that does the gritty work.
private byte[] callCipher( byte[] data )
throws CryptoException {
int size = cipher.getOutputSize( data.length );
byte[] result = new byte[ size ];
int olen = cipher.processBytes(data,0,data.length result, 0);
olen += cipher.doFinal( result, olen );
if( olen < size ){
byte[] tmp = new byte[ olen ];
System.arraycopy(
result, 0, tmp, 0, olen );
result = tmp;
}
return result;
}
// Encrypt arbitrary byte array, returning the
// encrypted data in a different byte array.
public synchronized byte[] encrypt( byte[] data )
throws CryptoException {
if( data == null || data.length == 0 ){
return new byte[0];
}
cipher.init( true, key );
return callCipher( data );
}
// Encrypts a string.
public byte[] encryptString( String data )
throws CryptoException {
if( data == null || data.length() == 0 ){
return new byte[0];
}
return encrypt( data.getBytes() );
}
// Decrypts arbitrary data.
public synchronized byte[] decrypt( byte[] data )
throws CryptoException {
if( data == null || data.length == 0 ){
return new byte[0];
}
cipher.init( false, key );
return callCipher( data );
}
// Decrypts a string that was previously encoded
// using encryptString.
public String decryptString( byte[] data )
throws CryptoException {
if( data == null || data.length == 0 ){
return "";
}
return new String( decrypt( data ) );
}
}
import org.bouncycastle.crypto.*;
导入org.bouncycastle.crypto.engines.*;
导入org.bouncycastle.crypto.modes.*;
导入org.bouncycastle.crypto.params.*;
//一个使用Bouncy Castle的简单示例
//用于执行DES的轻量级加密API
//任意数据的加密。
公共类加密机{
私有缓冲分组密码;
私钥参数密钥;
//初始化加密引擎。
//密钥数组的长度应至少为8字节。
公共加密机(字节[]密钥){
/*
cipher=新的填充块密码(
新的CBCBlockCipher(新的DESEngine());
*/
cipher=新的填充块密码(
新的CBCBlockCipher(新的BlowfishEngine());
this.key=新的key参数(key);
}
//初始化加密引擎。
//字符串长度应至少为8个字符。
公共加密机(字符串密钥){
这个(key.getBytes());
}
//做繁重工作的私人例行公事。
专用字节[]调用密码(字节[]数据)
抛出加密异常{
int size=cipher.getOutputSize(data.length);
字节[]结果=新字节[大小];
int-olen=cipher.processBytes(数据,0,数据长度结果,0);
olen+=cipher.doFinal(结果,olen);
如果(油份<尺寸){
字节[]tmp=新字节[olen];
System.arraycopy(
结果:0,tmp,0,olen);
结果=tmp;
}
返回结果;
}
//加密任意字节数组,返回
//不同字节数组中的加密数据。
公共同步字节[]加密(字节[]数据)
抛出加密异常{
if(data==null | | data.length==0){
返回新字节[0];
}
cipher.init(true,key);
返回callCipher(数据);
}
//加密字符串。
公共字节[]加密字符串(字符串数据)
抛出加密异常{
if(data==null | | data.length()==0){
返回新字节[0];
}
返回encrypt(data.getBytes());
}
//解密任意数据。
公共同步字节[]解密(字节[]数据)
抛出加密异常{
if(data==null | | data.length==0){
返回新字节[0];
}
cipher.init(false,key);
返回callCipher(数据);
}
//解密以前编码的字符串
//使用加密字符串。
公共字符串解密字符串(字节[]数据)
抛出加密异常{
if(data==null | | data.length==0){
返回“”;
}
返回新字符串(解密(数据));
}
}
一个非常基本的方法是用一个键对数据进行异或运算。此方法是对称的,即您可以使用与编码相同的密钥进行解码
如果我们选择一个1字节的密钥,它很好而且简单,足以让它不可读(但一点也不安全!)
private void encode(字节[]字节,字节键){
对于(inti=0;i试试这个,…它非常简单
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");
}
}
}
所以基本上,在写入文件之前,您需要加密,而在读取之后,您需要解密它。我不知道谁建议DES加密密码。
如果你想给老师留下深刻印象,我建议你遵循以下步骤:
- 引用您的引用作为加密解决方案的理论支持。我建议您这样做
- 解释你的代码符合规范的地方。对于一个好的示例代码教程,我建议你
此解决方案使您的项目成为现实
private void encodeDecode(byte[] bytes, byte key) {
for(int i=0; i<bytes.length; i++)
bytes[i] = (byte) (bytes[i]^key);
}
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");
}
}
}
public class CryptoUtils {
public static void encrypt(String key, File inputFile, File outputFile)
throws CryptoException {
doCrypto(Cipher.ENCRYPT_MODE, key, inputFile, outputFile);
}
public static void decrypt(String key, File inputFile, File outputFile)
throws CryptoException {
doCrypto(Cipher.DECRYPT_MODE, key, inputFile, outputFile);
}
private static void doCrypto(int cipherMode, String key, File inputFile,
File outputFile) throws CryptoException {
try {
Key secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(cipherMode, secretKey);
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();
} catch (NoSuchPaddingException | NoSuchAlgorithmException
| InvalidKeyException | BadPaddingException
| IllegalBlockSizeException | IOException ex) {
throw new CryptoException("Error encrypting/decrypting file", ex);
}
}
}
package net.codejava.crypto;
import java.io.File;
public class CryptoException extends Exception {
public CryptoException() {
}
public CryptoException(String message, Throwable throwable) {
super(message, throwable);
}
public static void main(String[] args) {
String key = "Mary has one cat1";
File inputFile = new File("document.txt");
File encryptedFile = new File("document.encrypted");
File decryptedFile = new File("document.decrypted");
try {
CryptoUtils.encrypt(key, inputFile, encryptedFile);
CryptoUtils.decrypt(key, encryptedFile, decryptedFile);
} catch (CryptoException ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
}
}
}
Scanner sc=new Scanner(System.in);
String name=sc.next();
//for inputting user name
File f= new File("d://"+name+".txt");
if(f.exists())
{
if(f.lastModified()!=0)
{
System.out.println("Account data tampered...cannot be accessed");
}
else{
String data="";
System.out.println(data); //data should contain
//data from file read using BufferedReader
f.setLastModified(0);
}
}
else
{
f.createNewFile();//Write whatever you want to to the file
f.setLastModified(0);
}
//Encrypt simple text
public String EncryptSimpleText (String text2Encrypt) throws Exception {
byte[] encryptArray = Base64.getEncoder().encode(text2Encrypt.getBytes());
return new String(encryptArray,"UTF-8");
}
//Decrypt simple text
public String Decrypt2SimpleText(String textEncrypted) throws Exception {
byte[] dectryptArray = textEncrypted.getBytes();
byte[] decarray = Base64.getDecoder().decode(dectryptArray);
return new String(decarray,"UTF-8");
}