|
|
本帖最后由 焱妃 于 2023-7-14 22:17 编辑
想必大家最开始接触的编程语言就是C语言了吧,自己无聊写的一个加解密编程(异或),可以用来加密一些文件,异或两次就等于解密,我这里是与0x66做的异或运算(Java是与0x24做异或),本来有其他方法,但是加密后文件变大了很多,解密完后文件大小还是变不回去,所以用这个最基本也是最简单的。下面是图示
- //c语言
- #include<stdio.h>
- int main()
- {
- int r;
- char d;
- char c[64]={0};
- char c1[64]={0};
- printf("输入文件路径\n");
- scanf("%s",c);
- printf("输出文件路径\n");
- scanf("%s",c1);
- FILE* srcfile=fopen(c,"rb");
- FILE* dstfile=fopen(c1,"wb");
- printf("开始加解密\n%s\n",c);
- while(1)
- {
- r=fread(&d,1,1,srcfile);
- if(r==1)
- {
- d=d^0x66;
- fwrite(&d,1,1,dstfile);
- }
- else
- {
- break;
- }
- }
- fclose(srcfile);
- fclose(dstfile);
- printf("加解密完成");
- return 0;
- }
复制代码
- //Java语言
- import java.util.*;
- import java.io.*;
- public class Main
- {
- public static void main(String[] args) throws IOException
- {
- int key;
- final long size;
- System.out.println("输入目标文件");
- Scanner sc = new Scanner(System.in);
- File file = new File(sc.next());
- System.out.println("输出目标文件");
- final File file1 = new File(sc.next());
- System.out.println("输入密钥");
- key = sc.nextInt();
- InputStream inputStream = new FileInputStream(file);
- size=file.length();
- FileOutputStream fileOutputStream = new FileOutputStream(file1);
- BufferedOutputStream out = new BufferedOutputStream(fileOutputStream);
- byte[] b = new byte[8192];
- int r;
- new Thread(new Runnable(){
- public void run(){
- while(true){
- long size1 = file1.length();
- if(size==size1){
- System.out.println("当前加密进度:100%");
- System.out.println("所有任务完成");
- break;
- }
- else
- {
- System.out.println("当前加密进度:"+String.valueOf((size1*100/size)+"%"));
- }
- try
- {
- Thread.sleep(300);
- }
- catch (InterruptedException e)
- {}
- }
-
- }
- }).start();
- while((r=inputStream.read(b))!=-1){
- for(int i=0;i<r;i++){
- out.write(b[i]^key);
- }
- }
- out.flush();
- fileOutputStream.close();
- inputStream.close();
- out.close();
-
- }
- }
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
-
查看全部评分
总评分:好评 +1
金币 +1
|