gpt4 book ai didi

Java实现文件的加密解密功能示例

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 26 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Java实现文件的加密解密功能示例由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

本文实例讲述了Java实现文件的加密解密功能分享给大家供大家参考,具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package com.copy.encrypt;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
public class FileEncryptAndDecrypt {
   /**
    * 文件file进行加密
    * @param fileUrl 文件路径
    * @param key 密码
    * @throws Exception
    */
   public static void encrypt(String fileUrl, String key) throws Exception {
     File file = new File(fileUrl);
     String path = file.getPath();
     if (!file.exists()){
       return ;
     }
     int index = path.lastIndexOf( "\\" );
     String destFile = path.substring( 0 , index)+ "\\" + "abc" ;
     File dest = new File(destFile);
     InputStream in = new FileInputStream(fileUrl);
     OutputStream out = new FileOutputStream(destFile);
     byte [] buffer = new byte [ 1024 ];
     int r;
     byte [] buffer2= new byte [ 1024 ];
     while (( r= in.read(buffer)) > 0 ) {
         for ( int i= 0 ;i<r;i++)
         {
           byte b=buffer[i];
           buffer2[i]=b== 255 ? 0 :++b;
         }
         out.write(buffer2, 0 , r);
         out.flush();
     }
     in.close();
     out.close();
     file.delete();
     dest.renameTo( new File(fileUrl));
     appendMethodA(fileUrl, key);
     System.out.println( "加密成功" );
   }
   /**
    *
    * @param fileName
    * @param content 密钥
    */
    public static void appendMethodA(String fileName, String content) {
       try {
         // 打开一个随机访问文件流,按读写方式
         RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw" );
         // 文件长度,字节数
         long fileLength = randomFile.length();
         //将写文件指针移到文件尾。
         randomFile.seek(fileLength);
         randomFile.writeBytes(content);
         randomFile.close();
       } catch (IOException e) {
         e.printStackTrace();
       }
    }
    /**
    * 解密
    * @param fileUrl 源文件
    * @param tempUrl 临时文件
    * @param ketLength 密码长度
    * @return
    * @throws Exception
    */
    public static String decrypt(String fileUrl, String tempUrl, int keyLength) throws Exception{
       File file = new File(fileUrl);
       if (!file.exists()) {
         return null ;
       }
       File dest = new File(tempUrl);
       if (!dest.getParentFile().exists()) {
         dest.getParentFile().mkdirs();
       }
       InputStream is = new FileInputStream(fileUrl);
       OutputStream out = new FileOutputStream(tempUrl);
       byte [] buffer = new byte [ 1024 ];
       byte [] buffer2= new byte [ 1024 ];
       byte bMax=( byte ) 255 ;
       long size = file.length() - keyLength;
       int mod = ( int ) (size% 1024 );
       int div = ( int ) (size>> 10 );
       int count = mod== 0 ?div:(div+ 1 );
       int k = 1 , r;
       while ((k <= count && ( r = is.read(buffer)) > 0 )) {
         if (mod != 0 && k==count) {
           r = mod;
         }
         for ( int i = 0 ;i < r;i++)
         {
           byte b=buffer[i];
           buffer2[i]=b== 0 ?bMax:--b;
         }
         out.write(buffer2, 0 , r);
         k++;
       }
       out.close();
       is.close();
       return tempUrl;
     }
    /**
    * 判断文件是否加密
    * @param fileName
    * @return
    */
    public static String readFileLastByte(String fileName, int keyLength) {
      File file = new File(fileName);
      if (!file.exists()) return null ;
      StringBuffer str = new StringBuffer();
       try {
         // 打开一个随机访问文件流,按读写方式
         RandomAccessFile randomFile = new RandomAccessFile(fileName, "r" );
         // 文件长度,字节数
         long fileLength = randomFile.length();
         //将写文件指针移到文件尾。
         for ( int i = keyLength ; i>= 1 ; i--){
           randomFile.seek(fileLength-i);
           str.append(( char )randomFile.read());
         }
         randomFile.close();
         return str.toString();
       } catch (IOException e) {
         e.printStackTrace();
       }
       return null ;
      }
}

希望本文所述对大家java程序设计有所帮助.

原文链接:http://www.cnblogs.com/FCWORLD/archive/2012/10/11/2720306.html 。

最后此篇关于Java实现文件的加密解密功能示例的文章就讲到这里了,如果你想了解更多关于Java实现文件的加密解密功能示例的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com