谁做过上传文件的加解密啊?进来给点方向好吗 谁做过上传文件的加解密啊?进来给点方向好吗我以前没有做过,感觉没有思路 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 上传的时候无论通过框架的File还是servlet获取输入流 都是二进制 用你需要的加密方式对二进制做一次加密然后输出为文件下载的时候就不要用anchor了 通过servlet读取文件流 解密二进制 response出去 11-1号到12-21号了好悲催 java区越来越冷清了 框架的File的二进制流怎么获取呢 框架的File对象一般是通过一个临时文件来操作的,至于针对不同框架可能有不同的形式反正你如果获取的File对象不是null肯定能通过文件输入流获取到二进制数组的 谢谢大侠。这个二进制数组是这样子的么:byte[] b; 找个算法,搞个证书,直接加密字节数组,或者转为base64加密都可以算法可以用RSA,DES都可以 DES加密 public byte[] toDESByteArray(String key) throws Exception { byte[] midArray = null; byte[] miArray = null; ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream ois = new ObjectOutputStream(bos); ois.writeObject(this); midArray = bos.toByteArray(); miArray = DESEncode.Encode(midArray ,key); return miArray; } CipherInputStreamCipherOutputStream import java.security.Key;import java.security.Security;import javax.crypto.Cipher;/** * DES加密和解密工具,可以对字符串进行加密和解密操作 。 */public class DesUtils { private static String strDefaultKey = "";//字符串默认键值 private Cipher encryptCipher = null;//加密工具 private Cipher decryptCipher = null;//解密工具 /** * 将byte数组转换为表示16进制值的字符串, 如:byte[]{8,18}转换为:0813, 和public static byte[] * hexStr2ByteArr(String strIn) 互为可逆的转换过程 * @param arrB 需要转换的byte数组 * @return 转换后的字符串 * */ public static String byteArr2HexStr(byte[] arrB){ int iLen = arrB.length; StringBuffer sb = new StringBuffer(iLen * 2);// 每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍 for (int i = 0; i < iLen; i++) { int intTmp = arrB[i]; while (intTmp < 0) {// 把负数转换为正数 intTmp = intTmp + 256; } if (intTmp < 16) {// 小于0F的数需要在前面补0 sb.append("0"); } sb.append(Integer.toString(intTmp, 16)); } return sb.toString(); } /** * 将表示16进制值的字符串转换为byte数组, 和public static String byteArr2HexStr(byte[] arrB) * 互为可逆的转换过程 * * @param strIn 需要转换的字符串 * @return 转换后的byte数组 */ public static byte[] hexStr2ByteArr(String strIn){ byte[] arrB = strIn.getBytes(); int iLen = arrB.length; byte[] arrOut = new byte[iLen / 2];// 两个字符表示一个字节,所以字节数组长度是字符串长度除以2 for (int i = 0; i < iLen; i = i + 2) { String strTmp = new String(arrB, i, 2); arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16); } return arrOut; } /** * 默认构造方法,使用默认密钥 * * @throws Exception */ public DesUtils() throws Exception { this(strDefaultKey); } /** * DES字符串加密 * 指定密钥构造方法 * @param strKey 指定的密钥 * @throws Exception */ public DesUtils(String strKey){ Security.addProvider(new com.sun.crypto.provider.SunJCE()); try { Key key = getKey(strKey.getBytes()); encryptCipher = Cipher.getInstance("DES"); encryptCipher.init(Cipher.ENCRYPT_MODE, key); decryptCipher = Cipher.getInstance("DES"); decryptCipher.init(Cipher.DECRYPT_MODE, key); } catch (Exception e) { e.printStackTrace(); } } /** * 加密字节数组 * * @param arrB 需加密的字节数组 * @return 加密后的字节数组 * @throws Exception */ public byte[] encrypt(byte[] arrB){ byte[] encrypt=null; try { encrypt=encryptCipher.doFinal(arrB); } catch (Exception e) { e.printStackTrace(); } return encrypt; } /** * 加密字符串 * * @param strIn 需加密的字符串 * @return 加密后的字符串 * @throws Exception */ public String encrypt(String strIn){ return byteArr2HexStr(encrypt(strIn.getBytes())); } /** * 解密字节数组 * * @param arrB 需解密的字节数组 * @return 解密后的字节数组 * @throws Exception */ public byte[] decrypt(byte[] arrB){ byte[] decrypt=null; try { decrypt= decryptCipher.doFinal(arrB); } catch (Exception e) { e.printStackTrace(); } return decrypt; } /** * 解密字符串 * * @param strIn 需解密的字符串 * @return 解密后的字符串 * @throws Exception */ public String decrypt(String strIn){ return new String(decrypt(hexStr2ByteArr(strIn))); } /** * 从指定字符串生成密钥,密钥所需的字节数组长度为8位 不足8位时后面补0,超出8位只取前8位 * * @param arrBTmp 构成该字符串的字节数组 * @return 生成的密钥 * @throws java.lang.Exception */ private Key getKey(byte[] arrBTmp) throws Exception { byte[] arrB = new byte[8];// 创建一个空的8位字节数组(默认值为0) for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {// 将原始字节数组转换为8位 arrB[i] = arrBTmp[i]; } Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");// 生成密钥 return key; } /** * main方法 */ public static void main(String[] args) { String content = "123456789;Tom"; String password = "123"; DesUtils des = new DesUtils(password);//自定义密钥 System.out.println("加密前的字符:" + content); String desContent=des.encrypt(content); System.out.println("加密后的字符:" + desContent); System.out.println("解密后的字符:" + des.decrypt(desContent));; }} 我想在js函数中把一个字符串去首尾空格怎么去 java编码转哈 连接 oracle10错误问题(no suitable driver) BIRT速度问题 关闭连接的问题 jetty服务器突然停止 web service 客户端创建失败 有无JSP+mysql的流量统计系统下载??? Spring4.0 代码配置的方式(以下代码) 改成 配置文件xml的方式 insert语句变成类似加密字符串 求助Java web项目生成Xml到工作目录 数据库设计表
下载的时候就不要用anchor了 通过servlet读取文件流 解密二进制 response出去
反正你如果获取的File对象不是null肯定能通过文件输入流获取到二进制数组的
这个二进制数组是这样子的么:
byte[] b;
算法可以用RSA,DES都可以
public byte[] toDESByteArray(String key) throws Exception
{
byte[] midArray = null;
byte[] miArray = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream ois = new ObjectOutputStream(bos);
ois.writeObject(this);
midArray = bos.toByteArray();
miArray = DESEncode.Encode(midArray ,key);
return miArray;
}
CipherOutputStream
import java.security.Security;
import javax.crypto.Cipher;/**
* DES加密和解密工具,可以对字符串进行加密和解密操作 。
*/
public class DesUtils { private static String strDefaultKey = "";//字符串默认键值
private Cipher encryptCipher = null;//加密工具
private Cipher decryptCipher = null;//解密工具 /**
* 将byte数组转换为表示16进制值的字符串, 如:byte[]{8,18}转换为:0813, 和public static byte[]
* hexStr2ByteArr(String strIn) 互为可逆的转换过程
* @param arrB 需要转换的byte数组
* @return 转换后的字符串
*
*/
public static String byteArr2HexStr(byte[] arrB){
int iLen = arrB.length;
StringBuffer sb = new StringBuffer(iLen * 2);// 每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍
for (int i = 0; i < iLen; i++) {
int intTmp = arrB[i];
while (intTmp < 0) {// 把负数转换为正数
intTmp = intTmp + 256;
}
if (intTmp < 16) {// 小于0F的数需要在前面补0
sb.append("0");
}
sb.append(Integer.toString(intTmp, 16));
}
return sb.toString();
} /**
* 将表示16进制值的字符串转换为byte数组, 和public static String byteArr2HexStr(byte[] arrB)
* 互为可逆的转换过程
*
* @param strIn 需要转换的字符串
* @return 转换后的byte数组
*/
public static byte[] hexStr2ByteArr(String strIn){
byte[] arrB = strIn.getBytes();
int iLen = arrB.length;
byte[] arrOut = new byte[iLen / 2];// 两个字符表示一个字节,所以字节数组长度是字符串长度除以2
for (int i = 0; i < iLen; i = i + 2) {
String strTmp = new String(arrB, i, 2);
arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
}
return arrOut;
} /**
* 默认构造方法,使用默认密钥
*
* @throws Exception
*/
public DesUtils() throws Exception {
this(strDefaultKey);
} /**
* DES字符串加密
* 指定密钥构造方法
* @param strKey 指定的密钥
* @throws Exception
*/
public DesUtils(String strKey){
Security.addProvider(new com.sun.crypto.provider.SunJCE());
try {
Key key = getKey(strKey.getBytes());
encryptCipher = Cipher.getInstance("DES");
encryptCipher.init(Cipher.ENCRYPT_MODE, key);
decryptCipher = Cipher.getInstance("DES");
decryptCipher.init(Cipher.DECRYPT_MODE, key);
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 加密字节数组
*
* @param arrB 需加密的字节数组
* @return 加密后的字节数组
* @throws Exception
*/
public byte[] encrypt(byte[] arrB){
byte[] encrypt=null;
try {
encrypt=encryptCipher.doFinal(arrB);
} catch (Exception e) {
e.printStackTrace();
}
return encrypt;
} /**
* 加密字符串
*
* @param strIn 需加密的字符串
* @return 加密后的字符串
* @throws Exception
*/
public String encrypt(String strIn){
return byteArr2HexStr(encrypt(strIn.getBytes()));
} /**
* 解密字节数组
*
* @param arrB 需解密的字节数组
* @return 解密后的字节数组
* @throws Exception
*/
public byte[] decrypt(byte[] arrB){
byte[] decrypt=null;
try {
decrypt= decryptCipher.doFinal(arrB);
} catch (Exception e) {
e.printStackTrace();
}
return decrypt;
} /**
* 解密字符串
*
* @param strIn 需解密的字符串
* @return 解密后的字符串
* @throws Exception
*/
public String decrypt(String strIn){
return new String(decrypt(hexStr2ByteArr(strIn)));
} /**
* 从指定字符串生成密钥,密钥所需的字节数组长度为8位 不足8位时后面补0,超出8位只取前8位
*
* @param arrBTmp 构成该字符串的字节数组
* @return 生成的密钥
* @throws java.lang.Exception
*/
private Key getKey(byte[] arrBTmp) throws Exception {
byte[] arrB = new byte[8];// 创建一个空的8位字节数组(默认值为0)
for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {// 将原始字节数组转换为8位
arrB[i] = arrBTmp[i];
}
Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");// 生成密钥
return key;
} /**
* main方法
*/
public static void main(String[] args) {
String content = "123456789;Tom";
String password = "123";
DesUtils des = new DesUtils(password);//自定义密钥
System.out.println("加密前的字符:" + content);
String desContent=des.encrypt(content);
System.out.println("加密后的字符:" + desContent);
System.out.println("解密后的字符:" + des.decrypt(desContent));;
}
}