resultsdecrypter.java

来自「Java version of ABC/HR comparator v0.5. 」· Java 代码 · 共 57 行

JAVA
57
字号
package abchr.gui;

import abchr.crypto.*;

import java.io.*;

public class ResultsDecrypter {
	public static String decrypt(ElGamalPrivateKey key,InputStream input) throws IOException {
		Decryptor decryptor=new ElGamalDecryptor(key);
		byte[] length=new byte[4];
		input.read(length);
		//System.out.println("length="+Util.byteArrayToInt(length,0));
		byte[] encryptedKey=new byte[Util.byteArrayToInt(length,0)];
		input.read(encryptedKey);
		byte[] blowfishKey;
		blowfishKey=decryptor.decrypt(encryptedKey);
		Cipher cipher=new BlowfishCipher(Util.bytesToInts(blowfishKey));
		input.read(length);
		//System.out.println("length="+Util.byteArrayToInt(length,0));
		byte[] encryptedText=new byte[Util.byteArrayToInt(length,0)];
		input.read(encryptedText);
		byte[] plainText;
		try {
			plainText=cipher.decrypt(encryptedText);
		} catch(IllegalArgumentException e) {
			throw new IOException("Data corrupt.");
		}
		return new String(plainText,0,plainText.length,"UTF-8");
	}

	public static String decrypt(ElGamalPrivateKey key,File f) throws IOException {
		InputStream input=new BufferedInputStream(new FileInputStream(f));
		String s=decrypt(key,input);
		input.close();
		return s;
	}

	public static ElGamalPrivateKey getKey(InputStream stream) throws IOException {
		ObjectInputStream keyInput=new ObjectInputStream(stream);
		ElGamalPrivateKey key;
		try {
			key=(ElGamalPrivateKey)keyInput.readObject();
		} catch(ClassNotFoundException e) {
			throw new IOException("Invalid file format");
		}
		keyInput.close();
		return key;
	}

	public static ElGamalPrivateKey getKey(File f) throws IOException {
		InputStream stream=new FileInputStream(f);
		ElGamalPrivateKey key=getKey(stream);
		stream.close();
		return key;
	}
}

⌨️ 快捷键说明

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