亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? aescrypt.java

?? 利用java實現文件的AES加密功能 This Java AES Crypt package contains the Java class es.vocali.util.AESCrypt, whi
?? JAVA
字號:
/* * Copyright 2008 Vócali Sistemas Inteligentes * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package es.vocali.util;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.UnsupportedEncodingException;import java.net.NetworkInterface;import java.security.GeneralSecurityException;import java.security.InvalidKeyException;import java.security.MessageDigest;import java.security.SecureRandom;import java.util.Arrays;import java.util.Enumeration;import javax.crypto.Cipher;import javax.crypto.Mac;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;/** * This class provides methods to encrypt and decrypt files using * <a href="http://www.aescrypt.com/aes_file_format.html">aescrypt file format</a>, * version 1 or 2. * <p> * Requires Java 6 and <a href="http://java.sun.com/javase/downloads/index.jsp">Java * Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files</a>. * <p> * Thread-safety and sharing: this class is not thread-safe.<br> * <tt>AESCrypt</tt> objects can be used as Commands (create, use once and dispose), * or reused to perform multiple operations (not concurrently though). * * @author Vócali Sistemas Inteligentes */public class AESCrypt {	private static final String JCE_EXCEPTION_MESSAGE = "Please make sure "		+ "\"Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files\" "		+ "(http://java.sun.com/javase/downloads/index.jsp) are installed on your JRE.";	private static final String RANDOM_ALG = "SHA1PRNG";	private static final String DIGEST_ALG = "SHA-256";	private static final String HMAC_ALG = "HmacSHA256";	private static final String CRYPT_ALG = "AES";	private static final String CRYPT_TRANS = "AES/CBC/NoPadding";	private static final byte[] DEFAULT_MAC =		{0x01, 0x23, 0x45, 0x67, (byte) 0x89, (byte) 0xab, (byte) 0xcd, (byte) 0xef};	private static final int KEY_SIZE = 32;	private static final int BLOCK_SIZE = 16;	private static final int SHA_SIZE = 32;	private final boolean DEBUG;	private byte[] password;	private Cipher cipher;	private Mac hmac;	private SecureRandom random;	private MessageDigest digest;	private IvParameterSpec ivSpec1;	private SecretKeySpec aesKey1;	private IvParameterSpec ivSpec2;	private SecretKeySpec aesKey2;			/*******************	 * PRIVATE METHODS *	 *******************/			/**	 * Prints a debug message on standard output if DEBUG mode is turned on.	 */	protected void debug(String message) {		if (DEBUG) {			System.out.println("[DEBUG] " + message);		}	}			/**	 * Prints a debug message on standard output if DEBUG mode is turned on.	 */	protected void debug(String message, byte[] bytes) {		if (DEBUG) {			StringBuilder buffer = new StringBuilder("[DEBUG] ");			buffer.append(message);			buffer.append("[");			for (int i = 0; i < bytes.length; i++) {				buffer.append(bytes[i]);				buffer.append(i < bytes.length - 1 ? ", " : "]");			}			System.out.println(buffer.toString());		}	}			/**	 * Generates a pseudo-random byte array.	 * @return pseudo-random byte array of <tt>len</tt> bytes.	 */	protected byte[] generateRandomBytes(int len) {		byte[] bytes = new byte[len];		random.nextBytes(bytes);		return bytes;	}			/**	 * SHA256 digest over given byte array and random bytes.<br>	 * <tt>bytes.length</tt> * <tt>num</tt> random bytes are added to the digest.	 * <p>	 * The generated hash is saved back to the original byte array.<br>	 * Maximum array size is {@link #SHA_SIZE} bytes.	 */	protected void digestRandomBytes(byte[] bytes, int num) {		assert bytes.length <= SHA_SIZE;		digest.reset();		digest.update(bytes);		for (int i = 0; i < num; i++) {			random.nextBytes(bytes);			digest.update(bytes);		}		System.arraycopy(digest.digest(), 0, bytes, 0, bytes.length);	}			/**	 * Generates a pseudo-random IV base on time and this computer's MAC.	 * <p>	 * This IV is used to crypt IV 2 and AES key 2 in the file.	 * @return IV.	 */	protected byte[] generateIv1() {		byte[] iv = new byte[BLOCK_SIZE];		long time = System.currentTimeMillis();		byte[] mac = null;		try {			Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();			while (mac == null && ifaces.hasMoreElements()) {				mac = ifaces.nextElement().getHardwareAddress();			}		} catch (Exception e) {			// Ignore.		}		if (mac == null) {			mac = DEFAULT_MAC;		}				for (int i = 0; i < 8; i++) {			iv[i] = (byte) (time >> (i * 8));		}		System.arraycopy(mac, 0, iv, 8, mac.length);		digestRandomBytes(iv, 256);		return iv;	}			/**	 * Generates an AES key starting with an IV and applying the supplied user password.	 * <p>	 * This AES key is used to crypt IV 2 and AES key 2.	 * @return AES key of {@link #KEY_SIZE} bytes.	 */	protected byte[] generateAESKey1(byte[] iv, byte[] password) {		byte[] aesKey = new byte[KEY_SIZE];		System.arraycopy(iv, 0, aesKey, 0, iv.length);		for (int i = 0; i < 8192; i++) {			digest.reset();			digest.update(aesKey);			digest.update(password);			aesKey = digest.digest();		}		return aesKey;	}		/**	 * Generates the random IV used to crypt file contents.	 * @return IV 2.	 */	protected byte[] generateIV2() {		byte[] iv = generateRandomBytes(BLOCK_SIZE);		digestRandomBytes(iv, 256);		return iv;	}			/**	 * Generates the random AES key used to crypt file contents.	 * @return AES key of {@link #KEY_SIZE} bytes.	 */	protected byte[] generateAESKey2() {		byte[] aesKey = generateRandomBytes(KEY_SIZE);		digestRandomBytes(aesKey, 32);		return aesKey;	}			/**	 * Utility method to read bytes from a stream until the given array is fully filled.	 * @throws IOException if the array can't be filled.	 */	protected void readBytes(InputStream in, byte[] bytes) throws IOException {		if (in.read(bytes) != bytes.length) {			throw new IOException("Unexpected end of file");		}	}		/**************	 * PUBLIC API *	 **************/			/**	 * Builds an object to encrypt or decrypt files with the given password.	 * @throws GeneralSecurityException if the platform does not support the required cryptographic methods.	 * @throws UnsupportedEncodingException if UTF-16 encoding is not supported.	 */	public AESCrypt(String password) throws GeneralSecurityException, UnsupportedEncodingException {		this(false, password);	}			/**	 * Builds an object to encrypt or decrypt files with the given password.	 * @throws GeneralSecurityException if the platform does not support the required cryptographic methods.	 * @throws UnsupportedEncodingException if UTF-16 encoding is not supported.	 */	public AESCrypt(boolean debug, String password) throws GeneralSecurityException, UnsupportedEncodingException {		try {			DEBUG = debug;			setPassword(password);			random = SecureRandom.getInstance(RANDOM_ALG);			digest = MessageDigest.getInstance(DIGEST_ALG);			cipher = Cipher.getInstance(CRYPT_TRANS);			hmac = Mac.getInstance(HMAC_ALG);		} catch (GeneralSecurityException e) {			throw new GeneralSecurityException(JCE_EXCEPTION_MESSAGE, e);		}	}			/**	 * Changes the password this object uses to encrypt and decrypt.	 * @throws UnsupportedEncodingException if UTF-16 encoding is not supported.	 */	public void setPassword(String password) throws UnsupportedEncodingException {		this.password = password.getBytes("UTF-16LE");		debug("Using password: ", this.password);	}			/**	 * The file at <tt>fromPath</tt> is encrypted and saved at <tt>toPath</tt> location.	 * <p>	 * <tt>version</tt> can be either 1 or 2.	 * @throws IOException when there are I/O errors.	 * @throws GeneralSecurityException if the platform does not support the required cryptographic methods.	 */	public void encrypt(int version, String fromPath, String toPath)	throws IOException, GeneralSecurityException {		InputStream in = null;		OutputStream out = null;		byte[] text = null;		try {			ivSpec1 = new IvParameterSpec(generateIv1());			aesKey1 = new SecretKeySpec(generateAESKey1(ivSpec1.getIV(), password), CRYPT_ALG);			ivSpec2 = new IvParameterSpec(generateIV2());			aesKey2 = new SecretKeySpec(generateAESKey2(), CRYPT_ALG);			debug("IV1: ", ivSpec1.getIV());			debug("AES1: ", aesKey1.getEncoded());			debug("IV2: ", ivSpec2.getIV());			debug("AES2: ", aesKey2.getEncoded());			in = new FileInputStream(fromPath);			debug("Opened for reading: " + fromPath);			out = new FileOutputStream(toPath);			debug("Opened for writing: " + toPath);						out.write("AES".getBytes());	// Heading.			out.write(version);	// Version.			out.write(0);	// Reserved.			if (version == 2) {	// No extensions.				out.write(0);				out.write(0);			}			out.write(ivSpec1.getIV());	// Initialization Vector.			text = new byte[BLOCK_SIZE + KEY_SIZE];			cipher.init(Cipher.ENCRYPT_MODE, aesKey1, ivSpec1);			cipher.update(ivSpec2.getIV(), 0, BLOCK_SIZE, text);			cipher.doFinal(aesKey2.getEncoded(), 0, KEY_SIZE, text, BLOCK_SIZE);			out.write(text);	// Crypted IV and key.			debug("IV2 + AES2 ciphertext: ", text);						hmac.init(new SecretKeySpec(aesKey1.getEncoded(), HMAC_ALG));			text = hmac.doFinal(text);			out.write(text);	// HMAC from previous cyphertext.			debug("HMAC1: ", text);			cipher.init(Cipher.ENCRYPT_MODE, aesKey2, ivSpec2);			hmac.init(new SecretKeySpec(aesKey2.getEncoded(), HMAC_ALG));			text = new byte[BLOCK_SIZE];			int len, last = 0;			while ((len = in.read(text)) > 0) {				cipher.update(text, 0, BLOCK_SIZE, text);				hmac.update(text);				out.write(text);	// Crypted file data block.				last = len;			}			last &= 0x0f;			out.write(last);	// 4 bits mod 16.			debug("4 bits size mod 16: " + last);						text = hmac.doFinal();			out.write(text);	// HMAC from previous cyphertext.			debug("HMAC2: ", text);		} catch (InvalidKeyException e) {			throw new GeneralSecurityException(JCE_EXCEPTION_MESSAGE, e);		} finally {			if (in != null) {				in.close();			}			if (out != null) {				out.close();			}		}	}			/**	 * The file at <tt>fromPath</tt> is decrypted and saved at <tt>toPath</tt> location.	 * <p>	 * Source file can be encrypted using version 1 or 2 of aescrypt.	 * @throws IOException when there are I/O errors.	 * @throws GeneralSecurityException if the platform does not support the required cryptographic methods.	 */	public void decrypt(String fromPath, String toPath)	throws IOException, GeneralSecurityException {		InputStream in = null;		OutputStream out = null;		byte[] text = null, backup = null;		long total = 3 + 1 + 1 + BLOCK_SIZE + BLOCK_SIZE + KEY_SIZE + SHA_SIZE + 1 + SHA_SIZE;		int version;		try {			in = new FileInputStream(fromPath);			debug("Opened for reading: " + fromPath);			out = new FileOutputStream(toPath);			debug("Opened for writing: " + toPath);						text = new byte[3];			readBytes(in, text);	// Heading.			if (!new String(text).equals("AES")) {				throw new IOException("Invalid file header");			}						version = in.read();	// Version.			if (version < 1 || version > 2) {				throw new IOException("Unsupported version number: " + version);			}			debug("Version: " + version);						in.read();	// Reserved.						if (version == 2) {	// Extensions.				text = new byte[2];				int len;				do {					readBytes(in, text);					len = ((0xff & (int) text[0]) << 8) | (0xff & (int) text[1]);					if (in.skip(len) != len) {						throw new IOException("Unexpected end of extension");					}					total += 2 + len;					debug("Skipped extension sized: " + len);				} while (len != 0);			}						text = new byte[BLOCK_SIZE];			readBytes(in, text);	// Initialization Vector.			ivSpec1 = new IvParameterSpec(text);			aesKey1 = new SecretKeySpec(generateAESKey1(ivSpec1.getIV(), password), CRYPT_ALG);			debug("IV1: ", ivSpec1.getIV());			debug("AES1: ", aesKey1.getEncoded());						cipher.init(Cipher.DECRYPT_MODE, aesKey1, ivSpec1);			backup = new byte[BLOCK_SIZE + KEY_SIZE];			readBytes(in, backup);	// IV and key to decrypt file contents.			debug("IV2 + AES2 ciphertext: ", backup);			text = cipher.doFinal(backup);			ivSpec2 = new IvParameterSpec(text, 0, BLOCK_SIZE);			aesKey2 = new SecretKeySpec(text, BLOCK_SIZE, KEY_SIZE, CRYPT_ALG);			debug("IV2: ", ivSpec2.getIV());			debug("AES2: ", aesKey2.getEncoded());						hmac.init(new SecretKeySpec(aesKey1.getEncoded(), HMAC_ALG));			backup = hmac.doFinal(backup);			text = new byte[SHA_SIZE];			readBytes(in, text);	// HMAC and authenticity test.			if (!Arrays.equals(backup, text)) {				throw new IOException("Message has been altered or password incorrect");			}			debug("HMAC1: ", text);						total = new File(fromPath).length() - total;	// Payload size.			if (total % BLOCK_SIZE != 0) {				throw new IOException("Input file is corrupt");			}			debug("Payload size: " + total);			cipher.init(Cipher.DECRYPT_MODE, aesKey2, ivSpec2);			hmac.init(new SecretKeySpec(aesKey2.getEncoded(), HMAC_ALG));			backup = new byte[BLOCK_SIZE];			text = new byte[BLOCK_SIZE];			for (int block = (int) (total / BLOCK_SIZE); block > 0; block--) {				int len = BLOCK_SIZE;				if (in.read(backup, 0, len) != len) {	// Cyphertext block.					throw new IOException("Unexpected end of file contents");				}				cipher.update(backup, 0, len, text);				hmac.update(backup, 0, len);				if (block == 1) {					len = in.read();	// 4 bits size mod 16.					debug("4 bits size mod 16: " + len);				}				out.write(text, 0, len);			}			out.write(cipher.doFinal());						backup = hmac.doFinal();			text = new byte[SHA_SIZE];			readBytes(in, text);	// HMAC and authenticity test.			if (!Arrays.equals(backup, text)) {				throw new IOException("Message has been altered or password incorrect");			}			debug("HMAC2: ", text);		} catch (InvalidKeyException e) {			throw new GeneralSecurityException(JCE_EXCEPTION_MESSAGE, e);		} finally {			if (in != null) {				in.close();			}			if (out != null) {				out.close();			}		}	}			public static void main(String[] args) {		try {			if (args.length < 4) {				System.out.println("AESCrypt e|d password fromPath toPath");				return;			}			AESCrypt aes = new AESCrypt(true, args[1]);			switch (args[0].charAt(0)) {			case 'e':				aes.encrypt(2, args[2], args[3]);				break;			case 'd':				aes.decrypt(args[2], args[3]);				break;			default:				System.out.println("Invalid operation: must be (e)ncrypt or (d)ecrypt.");				return;			}		} catch (Exception e) {			e.printStackTrace();		}	}}

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
欧美日韩五月天| 欧美男同性恋视频网站| 欧美bbbbb| 日产欧产美韩系列久久99| 亚洲欧洲中文日韩久久av乱码| 国产日韩欧美精品在线| 久久久久亚洲蜜桃| 国产亚洲欧美色| 国产精品美女久久久久久久 | 色综合中文字幕| 成人97人人超碰人人99| 成人福利电影精品一区二区在线观看| 国产成人午夜视频| youjizz久久| 色狠狠桃花综合| 欧美电影一区二区| 日韩精品一区二区三区老鸭窝| 欧美电影免费观看高清完整版在| 欧美va亚洲va香蕉在线 | 成人免费观看男女羞羞视频| 顶级嫩模精品视频在线看| eeuss鲁片一区二区三区| 在线一区二区观看| 欧美一区二区三区在线| 久久久久久久国产精品影院| 国产精品欧美极品| 亚洲va欧美va国产va天堂影院| 日产国产高清一区二区三区| 国产成人精品免费看| 色婷婷激情综合| 日韩精品一区二区三区视频播放| 欧美国产在线观看| 亚洲bt欧美bt精品777| 精品在线视频一区| 日本国产一区二区| 2021中文字幕一区亚洲| 亚洲欧洲日本在线| 久久www免费人成看片高清| 粉嫩久久99精品久久久久久夜| 在线亚洲一区二区| 26uuu亚洲综合色欧美| 樱桃视频在线观看一区| 精品一区二区三区不卡| 欧美亚洲一区二区三区四区| 国产日产欧美一区| 日韩av中文字幕一区二区三区| 成人性色生活片| 日韩欧美一级片| 亚洲一区自拍偷拍| 成人免费看片app下载| 欧美一二区视频| 亚洲国产另类av| www.在线欧美| 国产女人18毛片水真多成人如厕| 爽好多水快深点欧美视频| 91在线视频观看| 国产亚洲污的网站| 国内久久精品视频| 欧美精品自拍偷拍动漫精品| 亚洲精品免费在线| av激情亚洲男人天堂| 2017欧美狠狠色| 卡一卡二国产精品 | 国产女人aaa级久久久级| 奇米精品一区二区三区在线观看一| 99精品久久久久久| 国产精品女人毛片| 风流少妇一区二区| 国产欧美一区二区精品仙草咪| 免费日本视频一区| 欧美一区永久视频免费观看| 亚洲在线中文字幕| 欧美视频在线一区二区三区 | 久久亚洲一区二区三区明星换脸| 日韩综合一区二区| 欧美乱妇20p| 久久99在线观看| 宅男噜噜噜66一区二区66| 午夜不卡av免费| 欧美美女黄视频| 日本va欧美va欧美va精品| 欧美美女一区二区三区| 日韩精品福利网| 精品国产亚洲在线| 国产成人av影院| 亚洲视频一区在线观看| 色欧美片视频在线观看| 亚洲午夜av在线| 欧美成人高清电影在线| 国产一区二区美女| 国产精品久久久久久亚洲伦| 一本大道av一区二区在线播放| 亚洲综合视频网| 日韩精品一区在线观看| 国产精品资源网| 亚洲天堂免费看| 欧美人与性动xxxx| 狠狠色综合日日| 中文字幕人成不卡一区| 欧美日韩免费高清一区色橹橹 | 一区在线观看免费| 欧美日韩国产精品自在自线| 久久激情综合网| 欧美国产一区二区在线观看| 在线欧美小视频| 久久精品国产网站| 亚洲视频1区2区| 日韩情涩欧美日韩视频| av亚洲精华国产精华精华| 午夜久久久久久电影| 国产亚洲欧洲997久久综合| 色激情天天射综合网| 国模一区二区三区白浆| 亚洲你懂的在线视频| 精品久久久久久久人人人人传媒| 99久久精品久久久久久清纯| 琪琪一区二区三区| 亚洲精品视频在线观看网站| 欧美精品一区二区三区高清aⅴ | 国产欧美一区二区精品忘忧草| 色哟哟欧美精品| 国产精品18久久久久久vr| 一区二区三区欧美久久| 国产视频一区在线观看 | 麻豆精品蜜桃视频网站| 玉米视频成人免费看| 久久蜜桃av一区精品变态类天堂| 欧美三级在线看| 99视频精品免费视频| 国产麻豆视频精品| 亚洲国产美女搞黄色| 中文字幕制服丝袜一区二区三区| 日韩精品一区二区三区视频| 欧美午夜电影在线播放| 97久久精品人人澡人人爽| 国产精品资源在线观看| 精品一区二区三区免费播放| 亚洲国产视频一区二区| 亚洲最新在线观看| 亚洲欧美日韩系列| 亚洲图片激情小说| 中文字幕一区二区日韩精品绯色| xf在线a精品一区二区视频网站| 91精品午夜视频| 欧美日韩国产高清一区二区三区 | 国产精品亚洲人在线观看| 蜜臀va亚洲va欧美va天堂| 午夜精品久久久久久久| 天堂资源在线中文精品| 亚洲午夜精品在线| 亚洲成av人片观看| 日本在线观看不卡视频| 日韩精品成人一区二区在线| 日韩专区欧美专区| 毛片一区二区三区| 激情伊人五月天久久综合| 精品一区二区三区久久| 国产激情一区二区三区桃花岛亚洲| 精品一区二区三区免费播放| 激情偷乱视频一区二区三区| 国产精品资源网站| 成人免费高清在线| 一本到高清视频免费精品| 欧美专区亚洲专区| 欧美疯狂做受xxxx富婆| 日韩欧美国产一区二区在线播放 | 国产偷v国产偷v亚洲高清| 久久精品人人做人人爽97| 国产女主播一区| 亚洲免费观看在线视频| 一区二区在线观看视频| 日本中文字幕一区| 国产成人免费视| 色狠狠综合天天综合综合| 欧美一区二区三级| 国产日韩欧美制服另类| 一区二区三区四区在线播放| 日韩精品色哟哟| 国产成人高清在线| 欧美做爰猛烈大尺度电影无法无天| 欧美日韩国产欧美日美国产精品| 欧美不卡一区二区三区| 亚洲天堂2016| 毛片基地黄久久久久久天堂| 成人综合婷婷国产精品久久免费| 91精品办公室少妇高潮对白| 欧美一区二区三区成人| 国产精品成人网| 精品一区二区三区不卡| 99久久精品国产一区二区三区| 在线综合亚洲欧美在线视频| 久久久精品国产免费观看同学| 亚洲人成精品久久久久久| 日韩av一区二区三区四区| 成a人片国产精品| 日韩欧美视频一区| 一区二区在线观看不卡| 国产超碰在线一区| 正在播放亚洲一区| 一区二区三区在线免费视频|