keycipher.java

来自「java AES RSA encryption and decryption」· Java 代码 · 共 59 行

JAVA
59
字号
package src;

import java.io.*;
import java.nio.*;
import java.nio.channels.*;

public class KeyCipher {

	private static byte[] key;

	public static byte[] readFile(String filename) throws Exception {
		try {
			File f = new File(filename);
			FileInputStream fs = new FileInputStream(f);
			FileChannel fc = fs.getChannel();
			int size = (int) fc.size();
			ByteBuffer buffer = ByteBuffer.allocateDirect(size);
			fc.read(buffer);
			buffer.flip();
			byte[] byteArray = new byte[size];
			buffer.get(byteArray);
			buffer.clear();
			fc.close();
			fs.close();
			return byteArray;
		} catch (Exception e) {
			throw new Exception("File not found!");
		}
	}

	public static void writeFile(String filename, byte[] byteArray)
			throws Exception {
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(filename);
			fos.write(byteArray);
			fos.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fos.close();
			} catch (IOException iex) {
			}
		}

	}

	public static void saveKey(String filename, byte[] key) throws Exception {
		writeFile(filename, key);
	}

	public static byte[] loadKey(String filename) throws Exception {
		key = readFile(filename);
		return key;
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?