这里是java版本的加密解密求php下实现加密解密...为了实现双方交互..
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.security.SecureRandom;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.SecretKeyFactory;
/**
* DES加密的,文件中共有两个方法,加密、解密
*/
public class DES {
private String Algorithm = "DES";
private KeyGenerator keygen;
private SecretKey deskey;
private Cipher c;
private byte[] cipherByte;
/**
* 初始化 DES 实例
*/
public DES() {
init();
} public void init() {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
try {
keygen = KeyGenerator.getInstance(Algorithm);
deskey = keygen.generateKey();
c = Cipher.getInstance(Algorithm);
} catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
} catch (NoSuchPaddingException ex) {
ex.printStackTrace();
}
}
/**
* 对 String 进行加密
* @param str 要加密的数据
* @return 返回加密后的 byte 数组
*/
public byte[] createEncryptor(String str) {
try {
c.init(Cipher.ENCRYPT_MODE, deskey);
cipherByte = c.doFinal(str.getBytes());
} catch (java.security.InvalidKeyException ex) {
ex.printStackTrace();
} catch (javax.crypto.BadPaddingException ex) {
ex.printStackTrace();
} catch (javax.crypto.IllegalBlockSizeException ex) {
ex.printStackTrace();
}
return cipherByte;
}
/**
* 对 Byte 数组进行解密
* @param buff 要解密的数据
* @return 返回加密后的 String
*/
public String createDecryptor(byte[] buff) {
try {
c.init(Cipher.DECRYPT_MODE, deskey);
cipherByte = c.doFinal(buff);
} catch (java.security.InvalidKeyException ex) {
ex.printStackTrace();
} catch (javax.crypto.BadPaddingException ex) {
ex.printStackTrace();
} catch (javax.crypto.IllegalBlockSizeException ex) {
ex.printStackTrace();
}
return (new String(cipherByte));
}
/**
* 已知密钥的情况下加密
*/
public static String encode(String str, String key) throws Exception { SecureRandom sr = new SecureRandom();
byte[] rawKey = (new sun.misc.BASE64Decoder()).decodeBuffer(key);//Base64.decode(key); System.out.println(rawKey.length);
DESKeySpec dks = new DESKeySpec(rawKey);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(dks); javax.crypto.Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, sr); byte data[] = str.getBytes("UTF8");
byte encryptedData[] = cipher.doFinal(data);
String sRes = (new sun.misc.BASE64Encoder()).encodeBuffer(encryptedData);
sRes = sRes.replaceAll("\r","");
sRes = sRes.replaceAll("\n","");
return sRes; }
/**
* 已知密钥的情况下解密
*/
public static String decode(String str, String key) throws Exception {
SecureRandom sr = new SecureRandom();
byte[] rawKey = (new sun.misc.BASE64Decoder()).decodeBuffer(key);//Base64.decode(key);
DESKeySpec dks = new DESKeySpec(rawKey);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, secretKey, sr);
byte encryptedData[] = (new sun.misc.BASE64Decoder()).decodeBuffer(str);//Base64.decode(str);
byte decryptedData[] = cipher.doFinal(encryptedData);
return new String(decryptedData, "UTF8");
}
public static void main(String[] args)
{
DES des = new DES();
try{
String en = des.encode("100@08027011016920551@013088253262274338", "BxAE3xDfrdY=");
System.out.println("加密结果:");
System.out.println(en);
String de = des.decode(en,"BxAE3xDfrdY=");
System.out.println(de);
}catch(Exception e){
System.out.println("Test1 Error!");
}
}}
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.security.SecureRandom;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.SecretKeyFactory;
/**
* DES加密的,文件中共有两个方法,加密、解密
*/
public class DES {
private String Algorithm = "DES";
private KeyGenerator keygen;
private SecretKey deskey;
private Cipher c;
private byte[] cipherByte;
/**
* 初始化 DES 实例
*/
public DES() {
init();
} public void init() {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
try {
keygen = KeyGenerator.getInstance(Algorithm);
deskey = keygen.generateKey();
c = Cipher.getInstance(Algorithm);
} catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
} catch (NoSuchPaddingException ex) {
ex.printStackTrace();
}
}
/**
* 对 String 进行加密
* @param str 要加密的数据
* @return 返回加密后的 byte 数组
*/
public byte[] createEncryptor(String str) {
try {
c.init(Cipher.ENCRYPT_MODE, deskey);
cipherByte = c.doFinal(str.getBytes());
} catch (java.security.InvalidKeyException ex) {
ex.printStackTrace();
} catch (javax.crypto.BadPaddingException ex) {
ex.printStackTrace();
} catch (javax.crypto.IllegalBlockSizeException ex) {
ex.printStackTrace();
}
return cipherByte;
}
/**
* 对 Byte 数组进行解密
* @param buff 要解密的数据
* @return 返回加密后的 String
*/
public String createDecryptor(byte[] buff) {
try {
c.init(Cipher.DECRYPT_MODE, deskey);
cipherByte = c.doFinal(buff);
} catch (java.security.InvalidKeyException ex) {
ex.printStackTrace();
} catch (javax.crypto.BadPaddingException ex) {
ex.printStackTrace();
} catch (javax.crypto.IllegalBlockSizeException ex) {
ex.printStackTrace();
}
return (new String(cipherByte));
}
/**
* 已知密钥的情况下加密
*/
public static String encode(String str, String key) throws Exception { SecureRandom sr = new SecureRandom();
byte[] rawKey = (new sun.misc.BASE64Decoder()).decodeBuffer(key);//Base64.decode(key); System.out.println(rawKey.length);
DESKeySpec dks = new DESKeySpec(rawKey);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(dks); javax.crypto.Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, sr); byte data[] = str.getBytes("UTF8");
byte encryptedData[] = cipher.doFinal(data);
String sRes = (new sun.misc.BASE64Encoder()).encodeBuffer(encryptedData);
sRes = sRes.replaceAll("\r","");
sRes = sRes.replaceAll("\n","");
return sRes; }
/**
* 已知密钥的情况下解密
*/
public static String decode(String str, String key) throws Exception {
SecureRandom sr = new SecureRandom();
byte[] rawKey = (new sun.misc.BASE64Decoder()).decodeBuffer(key);//Base64.decode(key);
DESKeySpec dks = new DESKeySpec(rawKey);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, secretKey, sr);
byte encryptedData[] = (new sun.misc.BASE64Decoder()).decodeBuffer(str);//Base64.decode(str);
byte decryptedData[] = cipher.doFinal(encryptedData);
return new String(decryptedData, "UTF8");
}
public static void main(String[] args)
{
DES des = new DES();
try{
String en = des.encode("100@08027011016920551@013088253262274338", "BxAE3xDfrdY=");
System.out.println("加密结果:");
System.out.println(en);
String de = des.decode(en,"BxAE3xDfrdY=");
System.out.println(de);
}catch(Exception e){
System.out.println("Test1 Error!");
}
}}
class FCrypt
{
var $key;
var $modes = MCRYPT_MODE_ECB;
var $cipher = MCRYPT_DES;
function FCrypt($key)
{
$this->key = $key;
}
function encrypt($input)
{
$size = mcrypt_get_block_size($this->cipher, $this->modes);
$input = $this->pkcs5_pad($input, $size);
$key = $this->key;
$td = mcrypt_module_open($this->cipher, '', $this->modes, '');
$iv = @mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
@mcrypt_generic_init($td, $key, $iv);
$data = mcrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$data = base64_encode($data);
return $data;
}
function decrypt($encrypted)
{
$encrypted = base64_decode($encrypted);
$key =$this->key;
$td = mcrypt_module_open($this->cipher,'',$this->modes,'');
//使用MCRYPT_DES算法,cbc模式
$iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
@mcrypt_generic_init($td, $key, $iv);
//初始处理
$decrypted = mdecrypt_generic($td, $encrypted);
//解密
mcrypt_generic_deinit($td);
//结束
mcrypt_module_close($td);
$y=$this->pkcs5_unpad($decrypted);
return $y;
}
function pkcs5_pad ($text, $blocksize)
{
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
function pkcs5_unpad($text)
{
$pad = ord($text{strlen($text)-1});
if ($pad > strlen($text))
{
return false;
}
if (strspn($text, chr($pad), strlen($text) - $pad) != $pad)
{
return false;
}
return substr($text, 0, -1 * $pad);
}
}$crypt = new FCrypt($key);
echo "Encode:".$crypt->encrypt($input);
echo "\n";
echo "Decode:".$crypt->decrypt($input);