FTP加密传输SQL或者oracle,dmp文件,用AEC加密解密之后,中文出现乱码,文本文件不会出现,求大神帮忙啊!下面是完整代码!

zoujun826 2016-11-26 08:46:04
//上传开始
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());
...全文
374 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
爱上无聊 2016-12-09
  • 打赏
  • 举报
回复
新建立密钥必须16位,不用转成二进制
zoujun826 2016-12-09
  • 打赏
  • 举报
回复
顶顶顶,其实我知道
zoujun826 2016-11-27
  • 打赏
  • 举报
回复
没大神帮忙的吗?

5,006

社区成员

发帖
与我相关
我的任务
社区描述
解读Web 标准、分析和讨论实际问题、推动网络标准化发展和跨浏览器开发进程,解决各种兼容性问题。
社区管理员
  • 跨浏览器开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧