//秘钥算法
private static final String KEY_ALGORITHM = "DES";
//加密算法:algorithm/mode/padding 算法/工作模式/填充模式
private static final String CIPHER_ALGORITHM = "DES/ECB/PKCS5Padding";
//秘钥
private static final String KEY = "12345678";//DES秘钥长度必须是8位
public static void main(String args[]) {
String data = "加密解密";
KLog.d("加密数据:" + data);
byte[] encryptData = encrypt(data.getBytes());
KLog.d("加密后的数据:" + new String(encryptData));
byte[] decryptData = decrypt(encryptData);
KLog.d("解密后的数据:" + new String(decryptData));
}
public static byte[] encrypt(byte[] data) {
//初始化秘钥
SecretKey secretKey = new SecretKeySpec(KEY.getBytes(), KEY_ALGORITHM);
try {
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] result = cipher.doFinal(data);
return Base64.getEncoder().encode(result);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static byte[] decrypt(byte[] data) {
byte[] resultBase64 = Base64.getDecoder().decode(data);
SecretKey secretKey = new SecretKeySpec(KEY.getBytes(), KEY_ALGORITHM);
try {
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] result = cipher.doFinal(resultBase64);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Socket socket = new Socket(ApiConstants.HOST, ApiConstants.PORT);
OutputStream outStream = socket.getOutputStream();
InputStream inStream = socket.getInputStream();
RandomAccessFile fileOutStream = new RandomAccessFile(file, "r");
fileOutStream.seek(0);
byte[] buffer = new byte[1024];
int len = -1;
while (((len = fileOutStream.read(buffer)) != -1)) {
outStream.write(buffer, 0, len); // 这里进行字节流的传输
}
fileOutStream.close();
outStream.close();
inStream.close();
socket.close();
Socket socket = server.accept();
InputStream inStream = socket.getInputStream();
OutputStream outStream = socket.getOutputStream();
outStream.write(response.getBytes("UTF-8"));
RandomAccessFile fileOutStream = new RandomAccessFile(file, "rwd");
fileOutStream.seek(0);
byte[] buffer = new byte[1024];
int len;
while ((len = inStream.read(buffer)) != -1) { // 从字节输入流中读取数据写入到文件中
fileOutStream.write(buffer, 0, len);
}
fileOutStream.close();
inStream.close();
outStream.close();
socket.close();
while (((len = fileOutStream.read(buffer)) != -1)) {
outStream.write(DesUtil.encrypt(buffer) ,0, len); // 对字节数组进行加密
}
while ((len = inStream.read(buffer)) != -1) {
fileOutStream.write(DesUtil.decrypt(buffer), 0, len); // 对字节数组进行解密
}
public class FileDesUtil {
//秘钥算法
private static final String KEY_ALGORITHM = "DES";
//加密算法:algorithm/mode/padding 算法/工作模式/填充模式
private static final String CIPHER_ALGORITHM = "DES/ECB/PKCS5Padding";
private static final byte[] KEY = {56, 57, 58, 59, 60, 61, 62, 63};//DES 秘钥长度必须是8 位或以上
/**
* 文件进行加密并保存加密后的文件到指定目录
*
* @param fromFile 要加密的文件 如c:/test/待加密文件.txt
* @param toFile 加密后存放的文件 如c:/加密后文件.txt
*/
public static void encrypt(String fromFilePath, String toFilePath) {
KLog.i("encrypting...");
File fromFile = new File(fromFilePath);
if (!fromFile.exists()) {
KLog.e("to be encrypt file no exist!");
return;
}
File toFile = getFile(toFilePath);
SecretKey secretKey = new SecretKeySpec(KEY, KEY_ALGORITHM);
InputStream is = null;
OutputStream out = null;
CipherInputStream cis = null;
try {
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
is = new FileInputStream(fromFile);
out = new FileOutputStream(toFile);
cis = new CipherInputStream(is, cipher);
byte[] buffer = new byte[1024];
int r;
while ((r = cis.read(buffer)) > 0) {
out.write(buffer, 0, r);
}
} catch (Exception e) {
KLog.e(e.toString());
} finally {
try {
if (cis != null) {
cis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
KLog.i("encrypt completed");
}
@NonNull
private static File getFile(String filePath) {
File fromFile = new File(filePath);
if (!fromFile.getParentFile().exists()) {
fromFile.getParentFile().mkdirs();
}
return fromFile;
}
/**
* 文件进行解密并保存解密后的文件到指定目录
*
* @param fromFilePath 已加密的文件 如c:/加密后文件.txt
* @param toFilePath 解密后存放的文件 如c:/ test/解密后文件.txt
*/
public static void decrypt(String fromFilePath, String toFilePath) {
KLog.i("decrypting...");
File fromFile = new File(fromFilePath);
if (!fromFile.exists()) {
KLog.e("to be decrypt file no exist!");
return;
}
File toFile = getFile(toFilePath);
SecretKey secretKey = new SecretKeySpec(KEY, KEY_ALGORITHM);
InputStream is = null;
OutputStream out = null;
CipherOutputStream cos = null;
try {
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
is = new FileInputStream(fromFile);
out = new FileOutputStream(toFile);
cos = new CipherOutputStream(out, cipher);
byte[] buffer = new byte[1024];
int r;
while ((r = is.read(buffer)) >= 0) {
cos.write(buffer, 0, r);
}
} catch (Exception e) {
KLog.e(e.toString());
} finally {
try {
if (cos != null) {
cos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
KLog.i("decrypt completed");
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有