`
lei_1021
  • 浏览: 42405 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

文件加密解密

    博客分类:
  • j2me
阅读更多

对称加密算法,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();
}
分享到:
评论
1 楼 dali0125 2009-11-28  
能不能生成一个固定的128位密码,加密解密文件
你这个程序好像是随机生成的
如果我要逐个加密一个文件夹下所有文件,那岂不是要生成N个密码?
还有你程序解密里面 byte[] k是什么啊?具体解密的时候的值是多少阿?

相关推荐

    java实现文件加密解密

    "java实现文件加密解密" Java 实现文件的加密与解密是指利用 Java 语言将资源文件(包括图片、动画等类型)进行简单的加密和解密。这种策略的原因和好处是将准备好的资源存储在云上,使用时通过网络进行读取即可,...

    C#文件加密解密(完整项目)

    本项目"文件加密解密(完整项目)"专注于利用C#进行文件的安全处理,确保数据在传输和存储时的隐私性。 一、C#加密技术基础 C#中实现文件加密主要依赖于.NET Framework提供的加密类库,如System.Security....

    ENC文件加密解密工具

    《深入理解ENC文件加密解密工具:以PrimaSoft Encryption Utility为例》 在信息安全日益重要的今天,文件加密解密工具成为保护数据隐私的关键工具。"ENC文件加密解密工具",特别是PrimaSoft Encryption Utility,是...

    文件加密解密系统

    文件加密解密系统是一种用于保护数据安全的重要工具,它能够对个人或组织的敏感信息进行加密,确保在传输或存储时不受未经授权的访问。在这个信息化的时代,数据安全已经成为企业和个人都必须关注的问题,因此,了解...

    全能文件加密解密源码.zip

    【标题】"全能文件加密解密源码.zip"是一个包含Visual Studio工程的压缩包,它提供了C#语言编写的源代码,用于实现文件的加密和解密功能。这个项目对于那些想要深入理解加密和解密机制,或者需要在自己的应用程序中...

    文件加密解密软件(简单好用)

    本文将深入探讨“文件加密解密软件”的基础知识、工作原理以及如何选择和使用这类软件。 首先,让我们理解文件加密的基本概念。文件加密是一种通过特定算法将文件中的数据转化为无法直接读取的形式,这一过程称为...

Global site tag (gtag.js) - Google Analytics