文件加密解密


对称加密算法,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();
}

你可能感兴趣的:(算法)