JAVA文件加密解密

JAVA文件加密解密


2024年1月4日发(作者:)

import ;

import putStream;

import tputStream;

/**

* 加密解密类

*

* @author shaohl

* @version 1.00

*/

public class filekey {

// static boolean debug =false ;

// 加密KEY不能随便改动

static final byte[] KEYVALUE

"6^)(9-p35@%3#4S!4S0)$Y%%^&5(j.&^&o(*0)$Y%!#O@*GpG@=+@j.&6^)(0-=+"

.getBytes();

static final int BUFFERLEN = 512;

public filekey() {

}

/**

* 对文件进行加密

*

* @param String

* oldFile 原始要加密的文件

* @param String

* newFile 加密后的文件

* @return

*/

public static void encryptFile(String oldFile, String newFile)

throws Exception {

FileInputStream in = new FileInputStream(oldFile);

File file = new File(newFile);

if (!())

NewFile();

FileOutputStream out = new FileOutputStream(file);

int c, pos, keylen;

pos = 0;

keylen = ;

=

byte buffer[] = new byte[BUFFERLEN];

while ((c = (buffer)) != -1) {

for (int i = 0; i < c; i++) {

buffer[i] ^= KEYVALUE[pos];

(buffer[i]);

pos++;

if (pos == keylen)

pos = 0;

}

}

();

();

}

/**

* 对文件进行解密

*

* @param String

* oldFile 原始要解密的文件

* @param String

* newFile 解密后的文件

* @return

*/

public static void decryptFile(String oldFile, String newFile)

throws Exception {

FileInputStream in = new FileInputStream(oldFile);

File file = new File(newFile);

if (!())

NewFile();

FileOutputStream out = new FileOutputStream(file);

int c, pos, keylen;

pos = 0;

keylen = ;

byte buffer[] = new byte[BUFFERLEN];

while ((c = (buffer)) != -1) {

for (int i = 0; i < c; i++) {

buffer[i] ^= KEYVALUE[pos];

(buffer[i]);

pos++;

if (pos == keylen)

pos = 0;

}

}

();

();

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

try {

// debug =false ;

String oldFile = new String("C:UsersAdministratorDesktopjava课设需要加密的文件.txt");

String newFile = new String("C:UsersAdministratorDesktopjava课设加密之后.txt");

String oldFile1= new String("C:UsersAdministratorDesktopjava课设加密之后.txt");

String newFile1= new String("C:UsersAdministratorDesktopjava课设解密之后.txt");

encryptFile(oldFile, newFile);

decryptFile(oldFile1, newFile1);

n("ok");

} catch (Exception e) {

tackTrace();

}

}

}

import yout;

import Event;

import Listener;

import n;

import ;

import ield;

//————————————窗体、、、——————————————————————、、

public class SimpleFrame extends JFrame implements ActionListener{

JButton jButton1,jButton2;

int i=0;

private Object oldfile;

private Object newfile;

private Object oldfile1;

private Object newfile1;

private filekey f;

public SimpleFrame (String title){

super(title);

filekey f1=new filekey();

jButton1=new JButton("加密");

jButton2=new JButton("解密");

JTextField tf=new JTextField(15);

setLayout(new FlowLayout());

ionListener(this);

ionListener(this);

add(jButton1);

add(jButton2);

add(tf);

setSize(400,300);

ible(true);

aultCloseOperation(_ON_CLOSE);

}

//______窗体-------

public static void main(String[] args) {

String oldfile = new String("C:UsersAdministratorDesktopjava课设需要加密的文件.txt");

String newfile = new String("C:UsersAdministratorDesktopjava课设加密之后的文件.txt");

String oldfile1 = new String("C:UsersAdministratorDesktopjava课设加密之后的文件.txt");

String newfile1 = new String("C:UsersAdministratorDesktopjava课设解密之后的文件.txt");

SimpleFrame frame =new SimpleFrame ("文件加密解密");

();

if(frame.i<0){ filekey f=new filekey();

{try {

f. encryptFile(oldfile, newfile);

} catch (Exception e) {

// TODO Auto-generated catch block

tackTrace();

}}

}

}

public void jiemi(){

i--;

("加密成功");

}

// 事件处理--------- ↓ ↓↓↓↓ ↓ ↓↓↓↓ ↓ ↓↓↓↓

↓↓ ↓ ↓↓↓↓ ↓ ↓↓↓↓

public void actionPerformed(ActionEvent ae)

{

if(rce()==jButton1)

{

encryptFile(oldfile, newfile);

("加密成功");

}

else if(rce()==jButton2){

decryptFile(oldfile1, newfile1);

("解密成功");

}

}

private void decryptFile(Object oldfile1, Object newfile1) {}

private void encryptFile(Object oldfile, Object newfile) {}

}

! ↓↓!!


发布者:admin,转转请注明出处:http://www.yc00.com/web/1704333644a1342188.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信