📄 pbecrypt.java
字号:
**/
public static void encryptFile(String file
, String newfile
, StringBuffer passphrase
, byte[] seed
, String algorithm) throws CryptoException, IOException {
FileInputStream fis = null;
FileOutputStream fos = null;
DataOutputStream dao = null;
try {
fis = new FileInputStream(file);
fos = new FileOutputStream(newfile);
dao = new DataOutputStream(fos);
// encrypt file
encrypt(fis, dao, seed, passphrase, algorithm, BUFFERSIZE_FILE);
} catch (IOException ioe) {
ioe.printStackTrace();
throw new IOException(ioe.getMessage());
} finally {
if (dao != null) {
// close outputstream
try {
dao.close();
} catch (IOException e) {
;
}
}
if (fis != null) {
// close outputstream
try {
fis.close();
} catch (IOException e) {
;
}
}
}
}
/**
* Decrypts a ciphertext with PBE
*
* @param text the ciphertext
* @param passphrase password or passphrase
* @param algorithm encryption algorithm (e.g. "PBEWithSHAAndIDEA-CBC")
* @return the decrypted plaintext
* @throws net.sourceforge.jcetaglib.exceptions.CryptoException for all encryption errors
*/
public static StringBuffer decrypt(StringBuffer text
, StringBuffer passphrase
, String algorithm) throws CryptoException {
ByteArrayOutputStream bao = null;
DataOutputStream dao = null;
try {
bao = new ByteArrayOutputStream();
dao = new DataOutputStream(bao);
// decrypt
decrypt(new ByteArrayInputStream(Base64.decode(text.toString())), dao, passphrase, algorithm, BUFFERSIZE_TEXT);
return new StringBuffer(new String(bao.toByteArray()));
} catch (IOException ioe) {
ioe.printStackTrace();
throw new CryptoException(ioe.getMessage());
} finally {
if (dao != null) {
// close outputstream
try {
dao.close();
} catch (IOException e) {
;
}
}
}
}
/**
* Decrypts any inputstream with PBE
*
* @param is the ciphered inputstream
* @param daos deciphered outputstream
* @param passphrase password or passphrase
* @param algorithm encryption algorithm (e.g. "PBEWithSHAAndIDEA-CBC")
* @param bufferlength buffer length in bytes
* @throws net.sourceforge.jcetaglib.exceptions.CryptoException encryption errors
* @throws IOException I/O errors
*/
public static void decrypt(InputStream is
, DataOutputStream daos
, StringBuffer passphrase
, String algorithm
, int bufferlength)
throws CryptoException, IOException {
CipherInputStream ciStr = null;
PBEKeySpec pbeKeySpec;
PBEParameterSpec pbeParamSpec;
SecretKeyFactory keyFac;
SecretKey pbeKey;
Cipher pbeCipher;
try {
// Add Bouncy Castle provider
Security.addProvider(new BouncyCastleProvider());
// read the salt
byte[] randomsalt = new byte[8];
is.read(randomsalt);
// Create PBE parameter set
pbeParamSpec = new PBEParameterSpec(randomsalt, PBE_COUNT);
pbeKeySpec = new PBEKeySpec(passphrase.toString().toCharArray());
keyFac = SecretKeyFactory.getInstance(algorithm);
pbeKey = keyFac.generateSecret(pbeKeySpec);
// Create PBE Cipher
pbeCipher = Cipher.getInstance(algorithm);
// Initialize PBE Cipher with key and parameters
pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);
// Initialize cipher inputstream
ciStr = new CipherInputStream(is, pbeCipher);
// Read bytes and run them through cipher
byte[] buffer = new byte[bufferlength];
int length = 0;
while ((length = ciStr.read(buffer)) != -1) {
daos.write(buffer, 0, length);
}
} catch (IOException ioe) {
ioe.printStackTrace();
throw new IOException(ioe.getMessage());
} catch (Exception ex) {
ex.printStackTrace();
throw new CryptoException(ex.getMessage());
} finally {
if (ciStr != null) {
try {
ciStr.close();
} catch (IOException ioe) {
;
}
}
}
}
/**
* Decrypts a ciphered file with PBE
*
* @param file the file to decrypt
* @param file the deciphered file
* @param passphrase the password or passphrase
* @param algorithm encryption algorithm (e.g. "PBEWithSHAAndIDEA-CBC")
* @exception net.sourceforge.jcetaglib.exceptions.CryptoException for all encryption errors
* @exception IOException I/O errors
**/
public static void decryptFile(String file
, String newfile
, StringBuffer passphrase
, String algorithm) throws CryptoException, IOException {
FileInputStream fis = null;
FileOutputStream fos = null;
DataOutputStream dao = null;
try {
fis = new FileInputStream(file);
fos = new FileOutputStream(newfile);
dao = new DataOutputStream(fos);
// decrypt file
decrypt(fis, dao, passphrase, algorithm, BUFFERSIZE_FILE);
} catch (IOException ioe) {
ioe.printStackTrace();
throw new IOException(ioe.getMessage());
} finally {
if (dao != null) {
// close outputstream
try {
dao.close();
} catch (IOException e) {
;
}
}
if (fis != null) {
// close inputstream
try {
fis.close();
} catch (IOException e) {
;
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -