本文整理汇总了Java中org.openyu.commons.security.SecurityHelper类的典型用法代码示例。如果您正苦于以下问题:Java SecurityHelper类的具体用法?Java SecurityHelper怎么用?Java SecurityHelper使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SecurityHelper类属于org.openyu.commons.security包,在下文中一共展示了SecurityHelper类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: encrypt
import org.openyu.commons.security.SecurityHelper; //导入依赖的package包/类
@Test
// 1000000 times: 14767 mills.
// 1000000 times: 13543 mills.
// 1000000 times: 11782 mills.
public void encrypt() {
String value = "中文測試abcdef";
String algorithm = "DES";
// 隨機key
SecretKey secretKey = SecurityHelper.randomSecretKey(algorithm);
byte[] result = null;
int count = 1;
long beg = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
result = SecurityHelper.encrypt(value, secretKey, algorithm);
}
long end = System.currentTimeMillis();
System.out.println(count + " times: " + (end - beg) + " mills. ");
SystemHelper.println(result);
assertEquals(24, result.length);
}
示例2: encryptHex
import org.openyu.commons.security.SecurityHelper; //导入依赖的package包/类
@Test
// 1000000 times: 7901 mills.
// 1000000 times: 7812 mills.
// 1000000 times: 8108 mills.
public void encryptHex() {
String value = "中文測試abcdef";
String algorithm = "DES";
// 隨機key
SecretKey secretKey = SecurityHelper.randomSecretKey(algorithm);
String result = null;
int count = 1;
long beg = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
result = SecurityHelper.encryptHex(value, secretKey, algorithm);
}
long end = System.currentTimeMillis();
System.out.println(count + " times: " + (end - beg) + " mills. ");
System.out.println(result);// 986985ea51dca188906abf8b2a1b8a4eed800729efb7b580
assertEquals(48, result.length());
}
示例3: encryptBase64
import org.openyu.commons.security.SecurityHelper; //导入依赖的package包/类
@Test
// 1000000 times: 12067 mills.
// 1000000 times: 12263 mills.
// 1000000 times: 11782 mills.
public void encryptBase64() {
String value = "中文測試abcdef";
String algorithm = "DES";
// 隨機key
SecretKey secretKey = SecurityHelper.randomSecretKey(algorithm);
String result = null;
int count = 1;
long beg = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
result = SecurityHelper.encryptBase64(value, secretKey, algorithm);
}
long end = System.currentTimeMillis();
System.out.println(count + " times: " + (end - beg) + " mills. ");
System.out.println(result);// At4nhIRyIZTbCVXpO+kz7NigWmiuz9Up
assertEquals(32, result.length());
}
示例4: encryptKeyPair
import org.openyu.commons.security.SecurityHelper; //导入依赖的package包/类
@Test
// 隨機key
// 1000000 times: 14767 mills.
// 1000000 times: 13543 mills.
// 1000000 times: 11782 mills.
public void encryptKeyPair() {
String value = "中文測試abcdef";
String algorithm = "RSA";
KeyPair keyPair = SecurityHelper.randomKeyPair(1024, algorithm);
byte[] result = null;
int count = 1;
long beg = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
result = SecurityHelper.encrypt(value, keyPair, algorithm);
}
long end = System.currentTimeMillis();
System.out.println(count + " times: " + (end - beg) + " mills. ");
SystemHelper.println(result);
assertEquals(128, result.length);
}
示例5: md
import org.openyu.commons.security.SecurityHelper; //导入依赖的package包/类
@Test
// 1000000 times: 3981 mills.
// 1000000 times: 3787 mills.
// 1000000 times: 3979 mills.
public void md() {
String value = "中文測試abcdef";
String algorithm = "MD5";
byte[] result = null;
int count = 1000000;
long beg = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
result = SecurityHelper.md(value, algorithm);
}
long end = System.currentTimeMillis();
System.out.println(count + " times: " + (end - beg) + " mills. ");
SystemHelper.println(result);// 51, -46, 64, -103, -104, -110, -80, -9,
// 12, -98, 17, 114, -4, -93, 90, 27
assertEquals(16, result.length);
}
示例6: writeSecretKey
import org.openyu.commons.security.SecurityHelper; //导入依赖的package包/类
@Test
public void writeSecretKey() {
SecretKey value = SecurityHelper.randomSecretKey("DES");
int count = 1;
long beg = System.currentTimeMillis();
String result = null;
for (int i = 0; i < count; i++) {
result = SecurityHelper.writeSecretKey("secretKey", value);
}
//
long end = System.currentTimeMillis();
System.out.println(count + " times: " + (end - beg) + " mills. ");
//
System.out.println(result);
}
示例7: writeKeyPair
import org.openyu.commons.security.SecurityHelper; //导入依赖的package包/类
@Test
public void writeKeyPair() {
KeyPair keyPair = SecurityHelper.randomKeyPair(1024, "RSA");
int count = 1;
long beg = System.currentTimeMillis();
String result = null;
for (int i = 0; i < count; i++) {
result = SecurityHelper.writeKeyPair("keyPair", keyPair);
}
//
long end = System.currentTimeMillis();
System.out.println(count + " times: " + (end - beg) + " mills. ");
//
System.out.println(result);
}
示例8: decryptWithProcessor
import org.openyu.commons.security.SecurityHelper; //导入依赖的package包/类
@BenchmarkOptions(benchmarkRounds = 100, warmupRounds = 1, concurrency = 100)
@Test
// round: 0.61 [+- 0.06], round.block: 0.41 [+- 0.05], round.gc: 0.00 [+-
// 0.00], GC.calls: 3, GC.time: 0.01, time.total: 0.64, time.warmup: 0.00,
// time.bench: 0.63
public void decryptWithProcessor() {
byte[] value = ByteHelper.toByteArray("中文測試abcdef");
byte[] encrypt = SecurityHelper.encryptWithProcessor(value);
byte[] result = null;
//
result = SecurityHelper.decryptWithProcessor(encrypt);
//
System.out.println(result.length + " ," + result);// 18
assertTrue(Arrays.equals(value, result));
}
示例9: ACCOUNT_AUTHORIZE_REQUEST
import org.openyu.commons.security.SecurityHelper; //导入依赖的package包/类
@Test
public void ACCOUNT_AUTHORIZE_REQUEST() {
final String ACCOUNT_ID = "TEST_ACCOUNT_1";
final String ASSIGN_KEY = "FarFarAway";
final String ALGORITHM = "HmacMD5";
// b5f01d3a0898d8016b5633edfe6106b0
SecretKey secretKey = SecurityHelper.createSecretKey(ASSIGN_KEY,
ALGORITHM);
byte[] buff = SecurityHelper.mac("1111", secretKey, ALGORITHM);
final String PASSWORD = EncodingHelper.encodeHex(buff);
System.out.println(PASSWORD);
//
Message message = messageService.createClient(ACCOUNT_ID,
CoreMessageType.ACCOUNT_AUTHORIZE_REQUEST);
message.addString(ACCOUNT_ID);
message.addString(PASSWORD);
accountSocklet.service(message);
}
示例10: ACCOUNT_AUTHORIZE_REQUEST
import org.openyu.commons.security.SecurityHelper; //导入依赖的package包/类
@Test
public void ACCOUNT_AUTHORIZE_REQUEST() {
final String ACCOUNT_ID = "TEST_ACCOUNT_1";
final String ASSIGN_KEY = "FarFarAway";
final String ALGORITHM = "HmacMD5";
// b5f01d3a0898d8016b5633edfe6106b0
SecretKey secretKey = SecurityHelper.createSecretKey(ASSIGN_KEY,
ALGORITHM);
byte[] buff = SecurityHelper.mac("1111", secretKey, ALGORITHM);
final String PASSWORD = EncodingHelper.encodeHex(buff);
System.out.println(PASSWORD);
//
Message message = messageService.createClient(ACCOUNT_ID,
CoreMessageType.ACCOUNT_AUTHORIZE_REQUEST);
message.addString(ACCOUNT_ID);
message.addString(PASSWORD);
//
javaConnector.send(message);
}
示例11: encryptMd
import org.openyu.commons.security.SecurityHelper; //导入依赖的package包/类
/**
* 指定key,加密建目錄
*
* @param file
* @param assignKey
* @param algorithm
* @return
*/
public static String encryptMd(File file, String assignKey, String algorithm) {
StringBuilder result = new StringBuilder();
//
try {
if (file != null) {
String[] names = StringUtils.splitPreserveAllTokens(file.getPath(), File.separator);
// 指定key
SecretKey secretKey = SecurityHelper.createSecretKey(assignKey, algorithm);
// 目錄加密
for (int i = 0; i < names.length; i++) {
String encrypt = SecurityHelper.encryptHex(names[i], secretKey, algorithm);
result.append(encrypt);
//
if (i < names.length - 1) {
result.append(File.separator);
}
}
// 建目錄
if (isNotExist(result.toString())) {
md(result.toString());
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
//
return result.toString();
}
示例12: decryptMd
import org.openyu.commons.security.SecurityHelper; //导入依赖的package包/类
/**
* 指定key,解密建目錄
*
* @param file
* @param assignKey
* @param algorithm
* @return
*/
public static String decryptMd(File file, String assignKey, String algorithm) {
StringBuilder result = new StringBuilder();
if (file != null) {
// 加個後綴,避免覆蓋原始目錄
final String SUFFIX = "-decrypt";
//
String[] names = StringUtils.splitPreserveAllTokens(file.getPath(), File.separator);
StringBuilder dir = new StringBuilder();
// 指定key
SecretKey secretKey = SecurityHelper.createSecretKey(assignKey, algorithm);
// 目錄解密
for (int i = 0; i < names.length; i++) {
byte[] decrypt = SecurityHelper.decryptHex(names[i], secretKey, algorithm);
dir.append(ByteHelper.toString(decrypt));
if (i == 0) {
dir.append(SUFFIX);
}
//
if (i < names.length - 1) {
dir.append(File.separator);
}
}
result.append(dir);
//
// 建目錄
if (isNotExist(dir.toString())) {
md(dir.toString());
}
}
return result.toString();
}
示例13: encryptFile
import org.openyu.commons.security.SecurityHelper; //导入依赖的package包/类
/**
* 指定key,加密檔案,含目錄檔名,內容
*
* @param file
* @param assignKey
* @param algorithm
* @return
*/
public static EncryptFileResult encryptFile(File file, String assignKey, String algorithm) {
EncryptFileResult result = new EncryptFileResult();
if (isExist(file)) {
// 指定key
SecretKey secretKey = SecurityHelper.createSecretKey(assignKey, algorithm);
// 目錄,encryptToHex
String encryptDir = encryptMd(file.getParent(), assignKey, algorithm);
// System.out.println("dir: " + encryptDir);
// 檔名,encryptToHex
String encryptFileName = SecurityHelper.encryptHex(file.getName(), secretKey, algorithm);
// System.out.println("fileName: " + encryptFileName);
// 內容,encryptToBase64
byte[] contents = IoHelper.read(file);
String encryptContent = SecurityHelper.encryptBase64(contents, secretKey, algorithm);
// System.out.println("content: " + encryptContent);
//
Writer writer = IoHelper.createWriter(encryptDir + File.separator + encryptFileName);
try {
IoHelper.write(writer, encryptContent);
//
result.setOrigFile(file);
result.setDestName(encryptDir + File.separator + encryptFileName);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
// 關閉串流
IoHelper.close(writer);
}
}
return result;
}
示例14: decryptFile
import org.openyu.commons.security.SecurityHelper; //导入依赖的package包/类
/**
* 指定key,解密檔案,含目錄檔名,內容
*
* @param file
* @param assignKey
* @param algorithm
* @return
*/
public static DecryptFileResult decryptFile(File file, String assignKey, String algorithm) {
DecryptFileResult result = new DecryptFileResult();
OutputStream out = null;
try {
if (isExist(file)) {
// 指定key
SecretKey secretKey = SecurityHelper.createSecretKey(assignKey, algorithm);
// 目錄,decryptFromHex
String decryptDir = decryptMd(file.getParent(), assignKey, algorithm);
// System.out.println("dir: " + decryptDir);
// 檔名,decryptFromHex
String decryptFileName = ByteHelper
.toString(SecurityHelper.decryptHex(file.getName(), secretKey, algorithm));
// System.out.println("fileName: " + decryptFileName);
// 內容,decryptFromBase64
byte[] contents = IoHelper.read(file);
byte[] decryptContent = SecurityHelper.decryptBase64(contents, secretKey, algorithm);
// System.out.println("content: " +
// ByteHelper.toString(decryptContent));
// System.out.println(decryptDir+" isExist:
// "+isExist(decryptDir));
out = IoHelper.createOutputStream(decryptDir + File.separator + decryptFileName);
IoHelper.write(out, decryptContent);
//
result.setOrigFile(file);
result.setDestName(decryptDir + File.separator + decryptFileName);
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
// 關閉串流
IoHelper.close(out);
}
return result;
}
示例15: randomSecretKey
import org.openyu.commons.security.SecurityHelper; //导入依赖的package包/类
@Test
// 1000000 times: 6007 mills.
// 1000000 times: 5790 mills.
// 1000000 times: 6157 mills.
// verified
public void randomSecretKey() {
SecretKey result = null;
//
int count = 1000;
long beg = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
result = SecurityHelper.randomSecretKey("DES");
System.out.println("[" + i + "] "
+ ByteHelper.toString(result.getEncoded()));
}
long end = System.currentTimeMillis();
System.out.println(count + " times: " + (end - beg) + " mills. ");
System.out.println(result.getAlgorithm() + " :"
+ ByteHelper.toString(result.getEncoded()));
assertNotNull(result);
//
result = SecurityHelper.randomSecretKey("DESede");
System.out.println(result.getAlgorithm() + " :"
+ ByteHelper.toString(result.getEncoded()));
assertNotNull(result);
//
result = SecurityHelper.randomSecretKey("HmacMD5");
System.out.println(result.getAlgorithm() + " :"
+ ByteHelper.toString(result.getEncoded()));
assertNotNull(result);
//
result = SecurityHelper.randomSecretKey("AES");
System.out.println(result.getAlgorithm() + " :"
+ ByteHelper.toString(result.getEncoded()));
assertNotNull(result);
}