Compatible with PHP and Java des Encryption and decryption code sharing _php instance

Source: Internet
Author: User
Tags base64 chr crypt decrypt php class sha1 encryption strlen

PHP Code:

<?php class DES {var $key; var $iv;
		Offset function DES ($key, $iv =0) {$this->key = $key;
		if ($iv = = 0) {$this->iv = $key;
		else {$this->iv = $iv;
		}//Cryptographic function Encrypt ($STR) {$size = Mcrypt_get_block_size (Mcrypt_des, MCRYPT_MODE_CBC);
 
		$str = $this->pkcs5pad ($str, $size);
		$data =MCRYPT_CBC (mcrypt_des, $this->key, $str, Mcrypt_encrypt, $this->iv); $data =strtoupper (Bin2Hex ($data));
	Returns the uppercase hexadecimal string return Base64_encode ($data);
		}//Decryption function decrypt ($str) {$str = Base64_decode ($STR);
		$strBin = $this->hex2bin (strtolower ($STR));
		$str = MCRYPT_CBC (mcrypt_des, $this->key, $str, Mcrypt_decrypt, $this->iv);
		$str = $this->pkcs5unpad ($STR);
	return $str;
		function Hex2bin ($hexData) {$binData = "";
		for ($i = 0; $i < strlen ($hexData); $i + + 2) {$binData. = Chr (Hexdec (substr ($hexData, $i, 2));
	return $binData;
function Pkcs5pad ($text, $blocksize) {		$pad = $blocksize-(strlen ($text)% $blocksize); Return $text.
	Str_repeat (Chr ($pad), $pad);
		The function Pkcs5unpad ($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);
}} $str = ' ABCD ';
$key = ' ASDFWEF5 ';
$crypt = new DES ($key);
$mstr = $crypt->encrypt ($STR);
 
$str = $crypt->decrypt ($MSTR);
 
echo $str. ' <=> ' $mstr; ?>

Java code:

Package com.test;
 
Import it.sauronsoftware.base64.Base64;
Import Java.security.Key;
Import Java.security.SecureRandom;
 
Import Java.security.spec.AlgorithmParameterSpec;
Import Javax.crypto.Cipher;
Import Javax.crypto.SecretKeyFactory;
Import Javax.crypto.spec.DESKeySpec;
 
Import Javax.crypto.spec.IvParameterSpec;
 public class Main {public static final String algorithm_des = "des/cbc/pkcs5padding"; /** * des algorithm, encryption * @param data to be encrypted String * @param key to encrypt the private key, the length can not be less than 8 bits * @return encrypted byte array, generally combined with BASE64 encoding use * @throws Cryptexception exception/public static string encode (string key,string data) throws Exception {return encode (key, data.
 GetBytes ()); /** * des algorithm, encryption * * @param data to be encrypted String * @param key Encryption private key, length can not be less than 8 bits * @return encrypted byte array, generally combined with BASE64 encoding use * @thro  WS Cryptexception Exception */public static String encode (String key,byte[] data) throws Exception {try {Deskeyspec
 
	 	DKs = new Deskeyspec (Key.getbytes ()); Secretkeyfactory keyfactory = SecretkeyfacTory.getinstance ("DES");
   The length of the key cannot be less than 8-bit byte key Secretkey = Keyfactory.generatesecret (DKS);
   Cipher Cipher = cipher.getinstance (algorithm_des);
   Ivparameterspec IV = new Ivparameterspec (Key.getbytes ());
   Algorithmparameterspec Paramspec = IV;
 
   Cipher.init (Cipher.encrypt_mode, Secretkey,paramspec);
 
 
byte[] bytes = cipher.dofinal (data);
   Return Byte2hex (bytes);
  return new String (Base64.encode (bytes));
  catch (Exception e) {throw new Exception (e); }/** * des algorithm, decryption * @param data to decrypt the string * @param key to decrypt the private key, length can not be less than 8 bits * @return decrypted byte array * @throws Excepti On exception */public static byte[] Decode (String key,byte[] data) throws Exception {try {securerandom sr = new Sec
	 	Urerandom ();
	 	Deskeyspec DKs = new Deskeyspec (Key.getbytes ());
   Secretkeyfactory keyfactory = secretkeyfactory.getinstance ("DES");
   The length of the key cannot be less than 8-bit byte key Secretkey = Keyfactory.generatesecret (DKS);
   Cipher Cipher = cipher.getinstance (algorithm_des); IvpaRameterspec IV = new Ivparameterspec (Key.getbytes ());
   Algorithmparameterspec Paramspec = IV;
   Cipher.init (Cipher.decrypt_mode, Secretkey,paramspec);
  return cipher.dofinal (data);
  catch (Exception e) {throw new Exception (e); /** * Gets the encoded value * @param key * @param data * @return * @throws Exception/public static String decode
 	Value (String key,string data) {byte[] datas;
		String value = null;
 
			try {datas = decode (key, Base64.decode (Data.getbytes ()));
		Value = new String (datas);
		catch (Exception e) {value = "";
 return value; public static void Main (string[] args) throws Exception {System.out.println ("Ming: ABCD; dense:" + main.encode ("Asdfwef5"
 , "ABCD")); }
}

PS: About encryption technology, the site also provides the following encryption tools for your reference to use:

MD5 Online encryption tool: Http://tools.jb51.net/password/CreateMD5Password

Escape Encryption/decryption tool: http://tools.jb51.net/password/escapepwd

Online SHA1 encryption tool: Http://tools.jb51.net/password/sha1encode

short link (short URL) online generation tool: http://tools.jb51.net/password/dwzcreate

short link (short URL) online Restore tool: Http://tools.jb51.net/password/unshorturl

High Strength password generator: Http://tools.jb51.net/password/CreateStrongPassword

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.