FTP加密传输SQL或者oracle,dmp文件,用AEC加密解密之后,中文出现乱码,文本文件不会出现,求大神帮忙啊!下面是完整代码!
//上传开始
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
fis = new FileInputStream(local);
bos = new ByteArrayOutputStream(fis.available());
byte[] buffer = new byte[1024];
int count = 0;
while ((count = fis.read(buffer)) != -1) {
bos.write(buffer, 0, count);
}
bos.flush();
//加密上传文件开始
//byte[] bytess = encodeAES(key, bos.toByteArray());
//根据密钥开始进行加密返回得到二进制字节型数组
byte[] bytess = encodeAES(bos.toString(),key);
//由于AES加密算法要求密文是16位的倍数,根据二进制字节型数组转换成16进制
String code = parseByte2HexStr(bytess);
ByteArrayInputStream is = new ByteArrayInputStream(code.getBytes());
//加密上传文件成功
if (ftpClient.storeFile(remoteFileName, is)) {
result = UploadStatus.Upload_New_File_Success;
} else {
result = UploadStatus.Upload_New_File_Failed;
}
fis.close();
bos.close();
/**
* 加密
*
* @param content 需要加密的内容
* @param password 加密密钥
* @return
*/
private static byte[] encodeAES(String content,String password) {
try{
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");// 创建密码器
byte[] byteContent = content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
byte[] result = cipher.doFinal(byteContent);
return result; // 加密
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
/**
* 将二进制转换成16进制
*
* @param buf
* @return
*/
public static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
/**
*解密下载
*/
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
FileOutputStream fos = null;
fos = new FileOutputStream(local);
ftpClient.setBufferSize(1024);
// 设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
InputStream is = ftpClient.retrieveFileStream(remote);
bos = new ByteArrayOutputStream(is.available());
byte[] buffer = new byte[1024*1024];
int count = 0;
while ((count = is.read(buffer)) != -1) {
bos.write(buffer, 0, count);
}
bos.flush();
//bos.toString()得到文件内容里面的16进制加密数据内容,然后将16进制转换为二进制
byte[] decode = parseHexStr2Byte(bos.toString());
//根据密钥开始解密,返回二进制字节数组
byte[] bytes = decodeAES(key, decode);
String a= new String(bytes, "UTF-8");
fos = new FileOutputStream(local);
fos.write(a.getBytes());
/**
* 将16进制转换为二进制
*
* @param hexStr
* @return
*/
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1)
return null;
byte[] result = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; i++) {
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
/**
* 解密
* @param content待解密内容
* @param password解密密钥
* @return
*/
public static byte[] decodeAES(String password,byte[] content) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");// 创建密码器
cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
byte[] result = cipher.doFinal(content);
return result; // 加密
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
myFtp.connect("192.168.1.111", 2121, "zou", "888888");
//下载
System.out.println(myFtp.download2("testzou.dmp","D:\\FTP\\testzou2.dmp"));
//上传
//System.out.println(myFtp.upload("E:\\test\\testzou.dmp","testzou.dmp"));
myFtp.disconnect();
} catch (IOException e) {
System.out.println("连接FTP出错:" + e.getMessage());
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
fis = new FileInputStream(local);
bos = new ByteArrayOutputStream(fis.available());
byte[] buffer = new byte[1024];
int count = 0;
while ((count = fis.read(buffer)) != -1) {
bos.write(buffer, 0, count);
}
bos.flush();
//加密上传文件开始
//byte[] bytess = encodeAES(key, bos.toByteArray());
//根据密钥开始进行加密返回得到二进制字节型数组
byte[] bytess = encodeAES(bos.toString(),key);
//由于AES加密算法要求密文是16位的倍数,根据二进制字节型数组转换成16进制
String code = parseByte2HexStr(bytess);
ByteArrayInputStream is = new ByteArrayInputStream(code.getBytes());
//加密上传文件成功
if (ftpClient.storeFile(remoteFileName, is)) {
result = UploadStatus.Upload_New_File_Success;
} else {
result = UploadStatus.Upload_New_File_Failed;
}
fis.close();
bos.close();
/**
* 加密
*
* @param content 需要加密的内容
* @param password 加密密钥
* @return
*/
private static byte[] encodeAES(String content,String password) {
try{
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");// 创建密码器
byte[] byteContent = content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
byte[] result = cipher.doFinal(byteContent);
return result; // 加密
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
/**
* 将二进制转换成16进制
*
* @param buf
* @return
*/
public static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
/**
*解密下载
*/
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
FileOutputStream fos = null;
fos = new FileOutputStream(local);
ftpClient.setBufferSize(1024);
// 设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
InputStream is = ftpClient.retrieveFileStream(remote);
bos = new ByteArrayOutputStream(is.available());
byte[] buffer = new byte[1024*1024];
int count = 0;
while ((count = is.read(buffer)) != -1) {
bos.write(buffer, 0, count);
}
bos.flush();
//bos.toString()得到文件内容里面的16进制加密数据内容,然后将16进制转换为二进制
byte[] decode = parseHexStr2Byte(bos.toString());
//根据密钥开始解密,返回二进制字节数组
byte[] bytes = decodeAES(key, decode);
String a= new String(bytes, "UTF-8");
fos = new FileOutputStream(local);
fos.write(a.getBytes());
/**
* 将16进制转换为二进制
*
* @param hexStr
* @return
*/
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1)
return null;
byte[] result = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; i++) {
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
/**
* 解密
* @param content待解密内容
* @param password解密密钥
* @return
*/
public static byte[] decodeAES(String password,byte[] content) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");// 创建密码器
cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
byte[] result = cipher.doFinal(content);
return result; // 加密
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
myFtp.connect("192.168.1.111", 2121, "zou", "888888");
//下载
System.out.println(myFtp.download2("testzou.dmp","D:\\FTP\\testzou2.dmp"));
//上传
//System.out.println(myFtp.upload("E:\\test\\testzou.dmp","testzou.dmp"));
myFtp.disconnect();
} catch (IOException e) {
System.out.println("连接FTP出错:" + e.getMessage());
...全文
相关推荐
Oracle P/L SQL实现FTP上传、下载功能(最新版)
Oracle P/L SQL实现FTP上传、下载功能,包体经过打包处理plb,内付包头说明和测试案例(调用例子),感兴趣用户可以下载下来。
通过Kettle进行Oracle数据分页迁移并FTP上传完整实践
Kettle,也就是Pentaho Data Integration,是一个功能强大的开源数据集成工具,广泛用于企业级的数据抽取、转换和加载(ETL)场景。它提供了一个直观的图形界面,允许用户无需深入了解编程知识即可设计和执行复杂的数据处理流程。Kettle的核心优势在于其高度的可扩展性和易用性。它支持多种数据源和目标系统,并能够轻松地与Hadoop、NoSQL数据库和其他大数据技术集成。这使得Kettle不仅适合于传统数据库的数据处理,也非常适合现代的大数据处理需求。
oracle数据导入导出FTP
高性能的oracle文本处理器。java源码放出。sqlload
txt-to-oracle.rar_oracle_oracle ftp
将ftp上的文本文件导入到Oracle数据库中
启动Linux系统FTP、启动oracle、启动Webogic、oracle备份
启动Linux系统FTP命令、启动oracle命令、启动Webogic命令、oracle备份命令,这个是我天天得用的东西!自己亲自整理的!


跨浏览器开发
