论坛首页 Java企业应用论坛

文件加密解密

浏览 5555 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-02-21  

对称加密算法,AES 128位加密算法
/**
	 * 加密文件infilename,输出加密后的文件outfilename 返回AES加密密钥
	 */
	public static byte[] AesEnCrypt(String infilename, String outfilename)
			throws Exception {
		KeyGenerator keyGen = KeyGenerator.getInstance("AES");
		keyGen.init(128);
		SecretKey key = keyGen.generateKey();

		Cipher cipher = Cipher.getInstance("AES");

		InputStream in = new FileInputStream(infilename);
		DataOutputStream out = new DataOutputStream(new FileOutputStream(
				outfilename));

		cipher.init(Cipher.ENCRYPT_MODE, key);
		crypt(in, out, cipher);

		in.close();
		out.close();
		return key.getEncoded();
	}

	/**
	 * 用密钥k解密文件infilename,输出明文文件outfilename
	 * 
	 * @param infilename
	 * @param outfilename
	 * @param k
	 */
	public static void AesDeCrypt(String infilename, String outfilename,
			byte[] k) {

		try {

			Cipher cipher = Cipher.getInstance("AES");

			SecretKey key = new javax.crypto.spec.SecretKeySpec(k, "AES");
			OutputStream out = new FileOutputStream(outfilename);
			DataInputStream in = new DataInputStream(new FileInputStream(
					infilename));

			cipher.init(Cipher.DECRYPT_MODE, key);
			crypt(in, out, cipher);

			in.close();
			out.close();

		} catch (GeneralSecurityException exception) {
			exception.printStackTrace();
		} catch (IOException exception) {
			exception.printStackTrace();
		}

	}

	/**
	 * 自己定义的加 密函数
	 * 
	 * @param in
	 * @param out
	 * @param cipher
	 * @throws IOException
	 * @throws GeneralSecurityException
	 */
	public static void crypt(InputStream in, OutputStream out, Cipher cipher)
			throws IOException, GeneralSecurityException {
		int blockSize = cipher.getBlockSize();
		int outputSize = cipher.getOutputSize(blockSize);
		byte[] inBytes = new byte[blockSize];
		byte[] outBytes = new byte[outputSize];
		int inLength = 0;
		boolean more = true;
		while (more) {
			inLength = in.read(inBytes);
			if (inLength == blockSize) {
				int outLength = cipher.update(inBytes, 0, blockSize, outBytes);
				out.write(outBytes, 0, outLength);
			} else {
				more = false;
			}
		}
		if (inLength > 0)
			outBytes = cipher.doFinal(inBytes, 0, inLength);
		else
			outBytes = cipher.doFinal();
		out.write(outBytes);
	}
	
	
	
	public static String crypt(String in,Cipher cipher)
	throws IOException, GeneralSecurityException {
		StringBuffer out = new StringBuffer();
		int blockSize = cipher.getBlockSize();
		int outputSize = cipher.getOutputSize(blockSize);
		byte[] inBytes = new byte[blockSize];
		byte[] outBytes = new byte[outputSize];
		int inLength = 0;
		boolean more = true;
		while (more) {
			for(int i = 0;i<in.getBytes().length;i++)
			{
				System.out.println(in.getBytes().length*8);
				if(in.getBytes().length>=i*blockSize)
				{
					System.arraycopy(in, 0*blockSize, inBytes, 0, blockSize);
					cipher.update(inBytes, 0, blockSize, outBytes);
					out.append(outBytes);
				}
				else
				{
					inLength = in.getBytes().length - i*blockSize;
					more = false;
				}
			}
		}
		if (inLength > 0)
			outBytes = cipher.doFinal(inBytes, 0, inLength);
		else
			outBytes = cipher.doFinal();
		out.append(outBytes);
		return out.toString();
}
   发表时间:2009-11-28  
能不能生成一个固定的128位密码,加密解密文件
你这个程序好像是随机生成的
如果我要逐个加密一个文件夹下所有文件,那岂不是要生成N个密码?
还有你程序解密里面 byte[] k是什么啊?具体解密的时候的值是多少阿?
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics