java php DES 加密解密
import java.io.IOException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class DES {
private byte[] desKey;
public DES(String desKey) {
this.desKey = desKey.getBytes();
}
public byte[] desEncrypt(byte[] plainText) throws Exception {
SecureRandom sr = new SecureRandom();
byte rawKeyData[] = desKey;
DESKeySpec dks = new DESKeySpec(rawKeyData);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key, sr);
byte data[] = plainText;
byte encryptedData[] = cipher.doFinal(data);
return encryptedData;
}
public byte[] desDecrypt(byte[] encryptText) throws Exception {
SecureRandom sr = new SecureRandom();
byte rawKeyData[] = desKey;
DESKeySpec dks = new DESKeySpec(rawKeyData);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key, sr);
byte encryptedData[] = encryptText;
byte decryptedData[] = cipher.doFinal(encryptedData);
return decryptedData;
}
public String encrypt(String input) throws Exception {
return base64Encode(desEncrypt(input.getBytes()));
}
public String decrypt(String input) throws Exception {
byte[] result = base64Decode(input);
return new String(desDecrypt(result));
}
public static String base64Encode(byte[] s) {
if (s == null)
return null;
BASE64Encoder b = new sun.misc.BASE64Encoder();
return b.encode(s);
}
public static byte[] base64Decode(String s) throws IOException {
if (s == null)
return null;
BASE64Decoder decoder = new BASE64Decoder();
byte[] b = decoder.de
相关文档:
Java杂谈(十一)??ORM
这是最后一篇Java杂谈了,以ORM框架的谈论收尾,也算是把J2ee的最后一方面给涵盖到了,之所以这么晚才总结出ORM这方面,一是笔者这两周比较忙,另一方面也想善始善终,仔细的先自己好好研究一下ORM框架技术,不想草率的敷衍了事。 &n ......
凭借JNBridge
技术,Java和.NET代码无需交叉编译
器就可以实现对象共享。所有Java代码运行在JVM上,而.NET代码则运行在CLR上。在该方案下,JVM和CLR可以运行在不同的机器上,也可以运
行在一台机器的不同进程上,甚至还能运行在相同的进程上。从开发者的角度来看,运行在其他虚拟机上的对象仅仅是代理的远程对象而已。JN ......
老
师让下载安装jdk1.4、jdk1.5和jdk1.6三个版本的Java开发包,并且熟练切换各个环境,其实就是配置不同的环境变量。以前只是在环境变
量中设置了用户变量,Java运行也很正常。一旦需要在几个版本中切换,用户变量就失效了。经过尝试才知道必须设置系统变量,为了方便可以设置
JAVA_HOME变量为jdk的安装路径,再把 %JA ......
Java
中的transient,volatile和strictfp关键字
如果用transient声明一个实例变量,当对象存储时,它的值不需要维持。例如:
Java
代码
class
T {
transient
int
a;
//不需要维持
int ......
ed2k://|file|[ReadFree]_C#.开发人员指南——ASP_NET、XML、web服务与.ADO_NET_11030729.rar|6563070|B1CBF4D0032C8D78FB10F5F2AE6C3882|/
ed2k://|file|[ReadFree]_C#网络应用编程_11041620.rar|8063056|7044A65903D86BF88BE8AF26D8E02B61|/
ed2k://|file|[ReadFree]_Java.P2P程序设计_11052086.rar|8742852| ......