📄 pbetest.java
字号:
/*
Name: PBETest.java
Licensing: LGPL
API: Sun (http://java.sun.com) JCE 1.2.2 API (cleanroom implementation by Bouncy Castle)
Provider: Bouncy Castle (http://www.bouncycastle.org)
Disclaimer:
COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES THAT THE COVERED CODE
IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR NON-INFRINGING. THE ENTIRE
RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED CODE IS WITH YOU. SHOULD ANY COVERED CODE
PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR)
ASSUME THE COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY
CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED
HEREUNDER EXCEPT UNDER THIS DISCLAIMER.
(C) Copyright 2003 Gert Van Ham
*/
package net.sourceforge.jcetaglib.test;
import junit.framework.Assert;
import junit.framework.TestCase;
import net.sourceforge.jcetaglib.lib.PBECrypt;
import java.io.*;
/**
* A collection of PBE encryption/decryption tests
* <P>
* These tests can be run using JUnit (http://www.junit.org)
*
* @author Gert Van Ham
* @author hamgert@users.sourceforge.net
* @author http://jcetaglib.sourceforge.net
* @version $Id: PBETest.java,v 1.1 2003/12/28 19:06:57 hamgert Exp $
*/
public class PBETest extends TestCase {
private StringBuffer ciphertext = null;
private StringBuffer plaintext = null;
/**
* setup test
*
* @throws java.io.IOException
*/
protected void setUp() throws IOException {
// create text file
FileOutputStream outStr = new FileOutputStream(RunTest.TEMPFOLDER + "readable.txt");
DataOutputStream dataStr = new DataOutputStream(outStr);
dataStr.writeBytes("This is a readable string inside a file");
dataStr.flush();
dataStr.close();
outStr.close();
}
/**
* test PBE encryption
*
* @throws Exception
*/
public void testPBE() throws Exception {
for (int i = 0; i < RunTest.pbealg.length; i++) {
for (int j = 0; j < RunTest.text.length; j++) {
ciphertext = PBECrypt.encrypt(RunTest.text[j], new StringBuffer("password"), RunTest.pbealg[i]);
plaintext = PBECrypt.decrypt(ciphertext, new StringBuffer("password"), RunTest.pbealg[i]);
Assert.assertEquals(plaintext.toString(), RunTest.text[j].toString());
}
}
}
/**
* test file PBE encryption
*
* @throws Exception
*/
public void testFilePBE() throws Exception {
for (int i = 0; i < RunTest.pbealg.length; i++) {
PBECrypt.encryptFile(RunTest.TEMPFOLDER + "readable.txt", RunTest.TEMPFOLDER + "readable.txt.encrypted", new StringBuffer("password"), RunTest.pbealg[i]);
PBECrypt.decryptFile(RunTest.TEMPFOLDER + "readable.txt.encrypted", RunTest.TEMPFOLDER + "readable.txt.decrypted", new StringBuffer("password"), RunTest.pbealg[i]);
// read the file
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(RunTest.TEMPFOLDER + "readable.txt.decrypted")));
StringBuffer line = new StringBuffer();
int c;
while ((c = reader.read()) != -1) {
line.append((char) c);
}
reader.close();
String t = line.toString();
Assert.assertEquals("This is a readable string inside a file", t);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -