Perform DES encryption and decryption on 16-bit hexadecimal strings. -Des encryption tool is successfully used.
Java and C # code. Mutual encryption and decryption,
Java code: (two methods)
First:
Package COM. FHK. decrypt; import Java. security. spec. keyspec; import javax. crypto. cipher; import javax. crypto. secretkey; import javax. crypto. secretkeyfactory; import javax. crypto. spec. deskeyspec; import COM. FHK. util. convertutil; /*** DES encryption and decryption ** @ classname des * @ author Zhang Yue * @ Date February */public class des {/*** DES encryption * @ Param hexstring (16-bit hexadecimal string) * @ Param keystr KEY 16 1 * @ throws exception */public static byte [] sencrypt_des (byte [] hexstring, byte [] keystr) throws exception {try {byte [] thecph = new byte [8]; try {byte [] thekey = NULL; byte [] themsg = NULL; themsg = hexstring; thekey = keystr; keyspec Ks = new deskeyspec (thekey); secretkeyfactory kf = secretkeyfactory. getinstance ("des"); secretkey Ky = KF. generatesecret (KS); cipher cf = cipher. getinstance ("des/ECB/nopadding"); cf. init (cipher. encrypt_mode, KY); thecph = cf. dofinal (themsg); // system. out. println ("************** DES encryption ****************"); // system. out. println ("key:" + bytestohex (keystr); // system. out. println ("string:" + bytestohex (themsg); // system. out. println ("encrypted:" + bytestohex (thecph);} catch (exception e) {e. printstacktrace () ;}return thecph;} catch (exception e) {Throw e ;}// perform DES encryption in ECB mode, public static void main (string [] ARGs) throws exception {byte [] S = sencrypt_des (pbocdesconvertutil. hexstringtobyte ("0123456789 abcdef"), pbocdesconvertutil. hexstringtobyte ("1111111111111111"); system. out. println (S. length);}/*** des decryption ** @ Param hexstr 16-bit hexadecimal string * @ Param keystr KEY 16 1 * @ Param modestr decryption mode: ECB * @ throws exception */public static byte [] sdecrypt_des (byte [] hexstr, byte [] keystr) throws exception {try {string algorithm = "des/ECB/nopadding "; byte [] thecph = new byte [8]; byte [] thekey = NULL; byte [] themsg = NULL; themsg = hexstr; thekey = keystr; keyspec Ks = new deskeyspec (thekey); secretkeyfactory kf = secretkeyfactory. getinstance ("des"); secretkey Ky = KF. generatesecret (KS); cipher cf = cipher. getinstance (algorithm); cf. init (cipher. decrypt_mode, KY); thecph = cf. dofinal (themsg); // system. out. println ("*************** des decryption ****************"); // system. out. println ("key:" + bytestohex (thekey); // system. out. println ("string:" + bytestohex (themsg); // system. out. println ("decrypted:" + bytestohex (thecph); Return thecph;} catch (exception e) {Throw E;} public static byte [] hextobytes (string Str) {try {If (STR = NULL) {return NULL;} else if (Str. length () <2) {return NULL;} else {int Len = Str. length ()/2; byte [] buffer = new byte [Len]; for (INT I = 0; I <Len; I ++) {buffer [I] = (byte) integer. parseint (Str. substring (I * 2, I * 2 + 2), 16);} return buffer ;}} catch (exception e) {Throw e ;}} public static string bytestohex (byte [] data) {try {If (Data = NULL) {return NULL;} else {int Len = data. length; string STR = ""; for (INT I = 0; I <Len; I ++) {If (data [I] & 0xff) <16) STR = STR + "0" + Java. lang. integer. tohexstring (data [I] & 0xff); else STR = STR + Java. lang. integer. tohexstring (data [I] & 0xff);} return Str. touppercase () ;}} catch (exception e) {Throw e ;}}}
Java: DES algorithm, second code:
Package COM. FHK. decrypt; import Java. security. key; import javax. crypto. cipher; import javax. crypto. spec. secretkeyspec; import COM. FHK. util. convertutil; // ECB mode DES encryption and decryption algorithm public class desecbencrypt {/***** encrypt data * @ Param encryptstring note: here, the Data Length can only be multiple of 8 * @ Param encryptkey * @ return * @ throws exception */public static byte [] encryptdes (byte [] encryptbyte, byte [] encryptkey) throws exception {secretkeyspec key = new secretkeyspec (getkey (encryptkey), "des"); cipher = cipher. getinstance ("des/ECB/nopadding"); cipher. init (cipher. encrypt_mode, key); byte [] encrypteddata = cipher. dofinal (encryptbyte); Return encrypteddata;} // implements DES encryption in ECB mode. Public static void main (string [] ARGs) has been verified) throws exception {byte [] S = encryptdes (pbocdesconvertutil. hexstringtobyte ("0123456789 abcdef"), pbocdesconvertutil. hexstringtobyte ("1111111111111111"); system. out. println (DES. bytestohex (s);}/*** customize a key * @ Param string */public static byte [] getkey (byte [] keyrule) {key = NULL; byte [] keybyte = keyrule; // create an empty eight-digit group. The default value is 0 byte [] bytetemp = new byte [8]. // convert the specified rule into an eight-digit group for (INT I = 0; I <bytetemp. length & I <keybyte. length; I ++) {bytetemp [I] = keybyte [I];} key = new secretkeyspec (bytetemp, "des"); Return key. getencoded ();}/***** decrypt data * @ Param decryptstring * @ Param decryptkey * @ return * @ throws exception */public static string decryptdes (string decryptstring, byte [] decryptkey) throws exception {secretkeyspec key = new secretkeyspec (getkey (decryptkey), "des"); cipher = cipher. getinstance ("des/ECB/nopadding"); cipher. init (cipher. decrypt_mode, key); byte decrypteddata [] = cipher. dofinal (pbocdesconvertutil. hexstringtobyte (decryptstring); return new string (decrypteddata );}}
C # The Code is as follows:
// Perform DES encryption on Mac public static string encrypt_des16 (string str_in_data, string str_des_key) // The data is in hexadecimal format {try {byte [] shuju = new byte [8]; byte [] keys = new byte [8]; for (INT I = 0; I <8; I ++) {shuju [I] = convert. tobyte (str_in_data.substring (I * 2, 2), 16); keys [I] = convert. tobyte (str_des_key.substring (I * 2, 2), 16);} des desencrypt = new descryptoserviceprovider (); desencrypt. mode = ciphermode. ECB; // desencrypt. key = asciiencoding. ASCII. getbytes (str_des_key); desencrypt. key = keys; byte [] buffer; buffer = shuju; // asciiencoding. ASCII. getbytes (str_in_data); icryptotransform transform = desencrypt. createencryptor (); byte [] r; r = transform. transformfinalblock (buffer, 0, buffer. length); string return_str = ""; foreach (byte B in R) {return_str + = B. tostring ("X2");} return_str = return_str.substring (0, 16); Return return_str;} catch (exception e) {scaleconvertion. writelog ("pbocdesmac. the encrypt_des16 () method in CS has an exception: "+ E. message); throw e ;}}