Java 加密解密
来源:互联网 发布:smali文件批量转java 编辑:程序博客网 时间:2024/06/03 23:03
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 临时文件 即解密后的文件位置 注意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
;
}
}
- ArcFour加密解密,java RC4加密解密,java 加密解密
- java加密解密,c#加密解密
- Java加密解密算法-AES加密解密
- Java加密解密算法-Base64加密解密
- Java加密解密算法-MD5加密解密
- Java加密解密算法-DES加密解密
- Java加密解密算法-PBE加密解密
- java加密解密--对称加密
- 加密解密。(aes, java)
- JAVA MD5ENCODE加密解密
- java加密解密
- java 加密解密
- java--des加密解密
- java 加密 解密
- java加密解密算法
- Java加密和解密
- JAVA加密解密代码
- java加密与解密
- window.parent 与 window.opener
- sscanf用法小结
- 第三课 类的继承
- Linux下如何查看当前进程?
- EditPlus正则表达式替换字符串
- Java 加密解密
- yum使用总结
- Unicore xuudb启动报错:java.security.InvalidKeyException: Illegal key size 解决方案
- Single Number I & II
- uClinux下,一种合并uzImage.bin和cramfs.bin的方法
- Extjs在火狐浏览器下提示ext-all.js 中出现TypeError: b is null的错误
- 一道百度校招笔试题
- 什么是JavaBean
- UVa 592 Island of Logic (有趣的枚举题)