java DES加密文件上传数据库,下载解密文件

1、获取固定秘钥:8位的

/**
	 * 固定key
	 * 
	 * @return
	 */
	public Key getKey() {
		Key deskey = null;
		try {
			// 固定密钥
			byte[] buffer = new byte[] { 0x47, 0x33, 0x43, 0x4D, 0x4F, 0x50,0x31, 0x32 };
			deskey = (Key) new SecretKeySpec(buffer, "DES");
		} catch (Exception e) {
			throw new RuntimeException("Error initializing SqlMap class. Cause: " + e);
		}


		return deskey;
	}

2、加密方法:


/**
	 * 加密01
	 * @param src
	 * @param deskey
	 * @return
	 * @throws InvalidKeyException
	 * @throws IllegalBlockSizeException
	 * @throws BadPaddingException
	 * @throws NoSuchAlgorithmException
	 * @throws NoSuchPaddingException
	 */
	public byte[] Encrytor(byte[] src, Key deskey)
			throws InvalidKeyException, IllegalBlockSizeException,
			BadPaddingException, NoSuchAlgorithmException,
			NoSuchPaddingException {
		Cipher c = Cipher.getInstance("DES/ECB/NoPadding");
		// 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式
		c.init(Cipher.ENCRYPT_MODE, deskey);
		byte[] doFinal = c.doFinal(src);
		return doFinal;
	}
	/**
	 * 加密方法02
	 * @param is
	 * @param deskey
	 * @return
	 * @throws InvalidKeyException
	 * @throws IllegalBlockSizeException
	 * @throws BadPaddingException
	 * @throws NoSuchAlgorithmException
	 * @throws NoSuchPaddingException
	 */
	public CipherInputStream Encrytor02(InputStream is, Key deskey)
			throws InvalidKeyException, IllegalBlockSizeException,
			BadPaddingException, NoSuchAlgorithmException,
			NoSuchPaddingException {
		Cipher c = Cipher.getInstance("DES/ECB/NoPadding");
		// 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式
		c.init(Cipher.ENCRYPT_MODE, deskey);
		CipherInputStream cis = new CipherInputStream(is, c); 
		return cis;
	}

3、解密方法


/**
	 * 对字符串解密
	 * 
	 * @param buff
	 * @return
	 * @throws InvalidKeyException
	 * @throws IllegalBlockSizeException
	 * @throws BadPaddingException
	 * @throws NoSuchPaddingException
	 * @throws NoSuchAlgorithmException
	 */
	public byte[] Decryptor(byte[] buff, Key deskey)
			throws InvalidKeyException, IllegalBlockSizeException,
			BadPaddingException, NoSuchAlgorithmException,
			NoSuchPaddingException {
		Cipher c = Cipher.getInstance("DES/ECB/NoPadding");
		// 根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示加密模式
		c.init(Cipher.DECRYPT_MODE, deskey);
		byte[] doFinal = c.doFinal(buff);
		return doFinal;
	}

4、InputStream 转 byte[]

      

/**
	 * InputStream 转 byte[]
	 * @param iStrm
	 * @return
	 * @throws IOException
	 */
	public byte[] InputStreamToByte(InputStream iStrm) throws IOException {
		ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
		int ch;
		byte[] buf = new byte[1024];
		while ((ch = iStrm.read()) != -1)
		{
		bytestream.write(buf, 0, ch);
		}
		byte imgdata[]=bytestream.toByteArray();
		bytestream.close();
		return imgdata;
		}


5、加密文件导入数据库

                EncrypDes encrypDes = new EncrypDes();
		Key key = encrypDes.getKey();
		System.out.println(key);
		long sqlstart = System.currentTimeMillis();// 开始时间

		File file = new File("D:/Icon.txt");
		FileInputStream in = new FileInputStream(file);
		CipherInputStream cin = encrypDes.Encrytor02(in, key);
		Connection con = null;// 创建一个数据库连接
		PreparedStatement pre = null;// 创建预编译语句对象,一般都是用这个而不用Statement
		ResultSet result = null;// 创建一个结果集对象
		// /*
		try {
			con = JdbcOracleUtil.getConn02("");
			System.out.println(con != null ? "成功" : "失败" + "连接成功!");
			String sql = "Insert INTO  FI_FILE (ID,FILECONTENT) values ( FIFILE_SEQ.nextVal ,? ) ";
			pre = con.prepareStatement(sql);

			ByteArrayOutputStream bAOutputStream = new ByteArrayOutputStream();
			int ch;
			while ((ch = cin.read()) != -1) {
				bAOutputStream.write(ch);
			}
			byte data[] = bAOutputStream.toByteArray();
			System.out.println(data);
			bAOutputStream.close();

			pre.setBytes(1, data);
			// 4.执行语句
			int i = pre.executeUpdate();
			System.out.println(i);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				// 逐一将上面的几个对象关闭,因为不关闭的话会影响性能、并且占用资源
				// 注意关闭的顺序,最后使用的最先关闭
				if (result != null)
					result.close();
				if (pre != null)
					pre.close();
				if (con != null)
					con.close();
				System.out.println("数据库连接已关闭!");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		long sqlend = System.currentTimeMillis(); // 结束时间
		System.out.println("上传文件用时:" + (sqlend - sqlstart) / 1000 + " s");
		System.out.println("上传文件用时:" + (sqlend - sqlstart) + "  ms ");

6、解密文件展示

                EncrypDes encrypDes = new EncrypDes();
		Key key = encrypDes.getKey();
		System.out.println(key);
		long sqlstart = System.currentTimeMillis();// 开始时间

		File file = new File("D:/Icon.txt");
		FileInputStream in = new FileInputStream(file);
		CipherInputStream cin = encrypDes.Encrytor02(in, key);
		Connection con = null;// 创建一个数据库连接
		PreparedStatement pre = null;// 创建预编译语句对象,一般都是用这个而不用Statement
		ResultSet result = null;// 创建一个结果集对象
		
		try {
			con = JdbcOracleUtil.getConn02("");
			System.out.println(con != null ? "成功" : "失败" + "连接成功!");
			String sql = "select * from Fi_File where ID=16";
			// String sql="select * from Fi_Fileinfo where fileid=97 ";
			pre = con.prepareStatement(sql);
			// 4.执行语句
			ResultSet reset = pre.executeQuery();
			byte[] bis = null;
			while (reset.next()) {
				bis = reset.getBytes("FILECONTENT");
			}
			byte[] doFinal = encrypDes.Decryptor(bis, key);
			// Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
			// cipher.init(Cipher.DECRYPT_MODE, key);
			// byte[] doFinal = cipher.doFinal(bis);
			System.out.println(doFinal);
			System.out.println(new String(doFinal, "GB2312"));

		} catch (Exception e) {
			System.out.println(e);
		} finally {
			try {
				// 逐一将上面的几个对象关闭,因为不关闭的话会影响性能、并且占用资源
				// 注意关闭的顺序,最后使用的最先关闭
				if (result != null)
					result.close();
				if (pre != null)
					pre.close();
				if (con != null)
					con.close();
				System.out.println("数据库连接已关闭!");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		long sqlend02 = System.currentTimeMillis(); // 结束时间
		System.out.println("上传文件用时:" + (sqlend02 - sqlstart) / 1000 + " s");
		System.out.println("上传文件用时:" + (sqlend02 - sqlstart) + "  ms ");




题目如下: 编写程序实现文件上传功能。 有能力的同学可以自行设计文件下载功能,但是本次作业不要求。 服务器端: 要求能运行如下程序: java FileServer port foldername 实际的运行命令可能为 java FileServer 1234 d:\\share 解释如下: port为服务器开放的网络连接端口 foldername为服务器上某个文件夹,该文件夹存放客户端上传的文件。 客户端: 要求能运行如下程序: java FileClient server_ip port afile java FileClient server_ip port afolder 实际的运行命令可能为 java FileClient 127.0.0.1 1234 d:\\abc\\a.docx java FileClient 127.0.0.1 1234 d:\\abc 上面的程序运行完毕后,在服务器的文件下将看到客户端上传的文件a.docx 能看到客户端上传的文件夹abc,并且abc文件夹下的所有文件和子文件夹都上传到了服务器。 假设客户端有文件夹d:\\abc,该文件夹有子文件若干,有子文件夹若干。 客户端能够将某个文件上传到服务器,也可以将某个文件夹连同文件夹下的所有子文件和子文件夹 全部上传到服务器,服务器端保存客户端的文件夹结构和文件信息。 文件传输过程要求实现内容加密加密算法任意,可以是DES,AES,RSA之类的算法都可以。 注意:是文件在传输过程中加密,到服务器保存的时候,要求和客户端的文件内容一样, 服务器端的文件并不需要加密。 要求支持多客户端并发上传文件,不考虑文件名冲突,假设多个客户端同时上传的文件没有重名现象。 必须使用多线程编程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值