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
相关文档:
----------接口------------
import java.rmi.*;
public interface HelloIn extends java.rmi.Remote{
String sayHello() throws RemoteException;
}
--------实现类-------------
import java.rmi.*;
import java.net.*;
import java.rmi.registry.*;
import java.rmi.server.*;
public class Hello exten ......
经过上一篇的学习,应该对Java中的Runtime类的exec方法了大致的了解,也知道应该如何去使用了吧。
首先学习下:Process类。
简单地测试一下:
调用Javac命令,并查看执行命令的返回值,并输出到控制台上去。
import java.io.IOException;
class Exec_Javac{
public static void main(String []args)throws IO ......
1java中排序算法的回调
编写通用的排序代码时,面临的一个问题就是必须根据对象的实际类型来执行比较运算,从而实现正确的运算。程序设计的主要目标就是“将发生变化的东西与保持不变的东西分开” ,在这里保持不变的部分就是程序算法,而每次使用时都要变化的是对象的实际比较算法。所以我们采用回调,将 ......
Java
中的transient,volatile和strictfp关键字
如果用transient声明一个实例变量,当对象存储时,它的值不需要维持。例如:
Java
代码
class
T {
transient
int
a;
//不需要维持
int ......
Java中调用C/C++生成的DLL
一、 生成C的头文件
1. 编辑Main.java
public class Main
{
public native static int getStrNum(byte str[], int strLen);
}
2. 生成头文件
按win + r打开“运行”窗口,输入“cmd”,打开 ......