Home > Net >  For the following Java 3 des encryption to into c # code
For the following Java 3 des encryption to into c # code

Time:09-23

Public class DESHelper {

//vector
Private final static String iv="01234567";
//encryption uniform encoding
Private final static String encoding="utf-8";

Private static final String hexDigIts []={" 0 ", "1", "2", "3", "4", "5", "6", "7", "eight" and "9", "a", "b", "c", "d", "e", "f"};


/* *
* 3 des encryption
*
* @ param plain text
* @ return
* @ throws the Exception
*/
Public static String desEncode (String plainText, String pk) throws the Exception {
The Key deskey=null;
DESedeKeySpec spec=new DESedeKeySpec (pk) getBytes ());
SecretKeyFactory keyfactory=SecretKeyFactory. GetInstance (" desede ");
Deskey=keyfactory. GenerateSecret (spec);

Cipher Cipher=Cipher. GetInstance (" desede/CBC/PKCS5Padding ");
IvParameterSpec ips=new IvParameterSpec (iv. GetBytes ());
Cipher. The init (cipher ENCRYPT_MODE, deskey, ips);
Byte [] encryptData=https://bbs.csdn.net/topics/cipher.doFinal (plainText getBytes (encoding));
Return Bytes2HexString (encryptData);
}


Public static String Bytes2HexString (byte [] b) {
String ret="";
for (int i=0; i String hex=Integer. ToHexString (b [I] & amp; 0 XFF);
If (hex. Length ()==1) {
Hex='0' + hex;
}
Ret +=hex. ToUpperCase ();
}
return ret;
}

Public static byte [] hexStringToBytes (String hexString) {
If (hexString==null | | hexString. Equals (" ")) {
return null;
}
HexString=hexString. ToUpperCase ();
Int length=hexString. Length ()/2;
Char [] hexChars=hexString. ToCharArray ();
Byte [] d=new byte (length),
for (int i=0; i Int pos=I * 2;
[I]=d (byte) (charToByte (hexChars (pos)) & lt; <4 | charToByte (hexChars/pos + 1));
}
Return d;
}

Private static byte charToByte (char) c {
Return (byte) 0123456789 "abcdef". IndexOf (c);
}

Public static void main (String [] args) {
The String key="";//write a
String STR="";//write a
Try {
String strencode=desEncode (STR, key);
String strdecode=desDecode (strencode, key);
System. The out. Println (" encryption: "+ strencode);
System. The out. Println (" decrypted: "+ strdecode);
} the catch (Exception e) {
e.printStackTrace();
}
}

/* *
* the MD5 encryption
*
* @ param origin character

* @ param charsetname coding* @ return
*/
Public static String MD5Encode (String origin, String charsetname) {
String resultString=null;
Try {
ResultString=new String (origin);
The MessageDigest md=MessageDigest. GetInstance (" MD5 ");
If (null==charsetname | | "" equals (charsetname)) {
ResultString=byteArrayToHexString (md. Digest (resultString. GetBytes ()));
} else {
ResultString=byteArrayToHexString (md. Digest (resultString. GetBytes (charsetname)));
}
} the catch (Exception e) {
}
Return resultString;
}


Public static String byteArrayToHexString (byte [] b) {
StringBuffer resultSb=new StringBuffer ();
for (int i=0; i ResultSb. Append (byteToHexString [I] (b));
}
Return resultSb. ToString ();
}

Public static String byteToHexString (byte b) {
Int n=b;
If (n & lt; 0 {
N +=256;
}
Int d1=n/16;
Int 16 d2=n %;
Return hexDigIts (d1) + hexDigIts (d2);
}
}
  •  Tags:  
  • C#
  • Related