利用Java API对字符串进行加密解密(DES加密算法)
/*Decryptor*/
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
/**
* Decrypt the password get form Xpress GUI
*/
public class Decryptor{
//加密
private static byte[] encrypt(int mode, byte[] password) {
try {
DESKeySpec desKeySpec = new DESKeySpec("KEY".getBytes());
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(mode, secretKey);
return cipher.doFinal(password);
} catch (Exception e) {
return password;
}
}
//解密
public static String decrypt(String[] args) {
int length = args.length;
byte[] b = new byte[length];
for(int i = 0; i < length; i++){
b[i] = new Byte(args[i]);
}
return new String(encrypt(Cipher.DECRYPT_MODE, b));
}
}
相关文档:
1.首先创建一个工程随便命名。
2.创建一个类命名为Reg.
3.执行代码
4.输入注册的姓名,显示栏目会出现please input register name:,只要输入英文名然后回车,注册码就出来了。把姓名和注册码像:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public clas ......
/**
* this关键字用法
*/
public class Flower {
int petalCount = 0;
String s = "initial value";
Flower(int petals){
petalCount = petals;
System.out.println("Constructor with one int arg");
}
Flower(String ss){
System.out.println("Constr ......
本篇旨在帮助准备学习Java以及刚接触Java的朋友认识、掌握和使用static、this、super、final这几个关键字的使用。Java博大精深,我也是一位正在学习和使用Java的爱好者,文中难免有不妥之处,欢迎指正。
一、static
请先看下面这段程序:
public class Hello{
public static void main(String[] args){ ......
synchronized的一个简单例子
public class TextThread
{
/**
* @param args
*/
public static void main(String[] args)
{
// TODO 自动生成方法存根
TxtThread tt = new TxtThread();
  ......
import java.net.URL;
import java.net.URLDecoder;
public class PathUtil
{
/**
* Get the env of windir, such as "C:\WINDOWS".
* @return the env of windir value.
*/
public static String getWindir(){
return System.getenv("windir");
}
......