有关文件加密。。access操作的问题
来源:互联网 发布:ps4安装centos 编辑:程序博客网 时间:2024/05/19 16:36
近日在做有关access文件生成的功能。但是sun公司的ojdbc包自带的acceess操作方法不支持跨平台。于是找了第三方的 Access_JDBC30.jar 包。这个包可以跨平台使用。但是是要收费的。免费版本加载一次包只能有50次的链接。用到的insert语句一多就报错。于是改用文本文件加密方式导数据。加密代码如下
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
;
}
}
- 有关文件加密。。access操作的问题
- 关于lua加密luac的有关问题
- 有关SVN操作的问题
- VC的有关操作问题
- 有关exe文件的问题
- 有关ASP向ACCESS数据库写入的问题?求助!
- java swing jtable 跟access库的有关问题
- Delphi对Access文件加密
- Delphi对Access文件加密
- 谈VC中的有关文件的操作
- 有关对文件的操作(System.IO)
- 与文件操作有关的命令
- 有关读写文件的操作 (个人总结)
- C语言有关文件操作的函数
- linux有关文件共享的操作
- Python 有关文件的操作示例
- 有关文件操作的几个函数
- 有关文件操作的几个函数
- OGRE引擎优化的几点建议
- web缓存—机制
- jQuery乱谈(二)
- Java Media Framework 基础教程
- 页面框架布局
- 有关文件加密。。access操作的问题
- ogre中的 z-filghting看到的z-bias及其getIterationDepthBias
- jQuery乱谈(三)
- servlet验证码的设置
- Ogre换装系统
- Media types
- 页面跳转
- jQuery乱谈(四)
- Dismissing MPMoviePlayerViewController the right way