懂java的看看, 求个.net 的DES加密方法
懂java的看看, 求个.net 的DES加密方法import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Date;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
public class test {
private static final char[] kHexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
private static String pingKey="21218cca77804d2b";
public test(String Key)
{
pingKey=Key;
}
public String DES(String payPass)
{
payPass = getMd5Hex(payPass.getBytes());
//支付密码加密
String payPassEncrypt = desEncrypt(payPass);
return payPassEncrypt;
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String payPass = "123321";
payPass = getMd5Hex(payPass.getBytes());
System.out.println("Before:"+payPass);
//支付密码加密
String payPassEncrypt = desEncrypt(payPass);
System.out.println("After:"+payPassEncrypt);
}
public static String desEncrypt(String hexData)
{
byte[] encryptBytes = encryptDES(hexToBuffer(hexData),
hexToBuffer( pingKey));
return bufferToHex(encryptBytes);
}
public static byte[] encryptDES(byte[] data, byte[] key)
{
byte[] encryptedData = (byte[])null;
try
{
DESKeySpec dks = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey sk = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
cipher.init(1, sk);
encryptedData = cipher.doFinal(data);
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return encryptedData;
}
public static String bufferToHex(byte[] buffer)
{
return bufferToHex(buffer, 0, buffer.length).toUpperCase();
}
public static String bufferToHex(byte[] buffer, int startOffset, int length)
{
StringBuffer hexString = new StringBuffer(2 * length);
int endOffset = startOffset + length;
for (int i = startOffset; i < endOffset; i++)
appendHexPair(buffer[i], hexString);
return hexString.toString();
}
public static byte[] md5Encrypt(byte[] data)
{
try
{
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
byte[] plainText = data;
messageDigest.update(plainText);
return messageDigest.digest();
} catch (NoSuchAlgorithmException e1) {
e1.printStackTrace();
}
return null;
}
public static String getMd5Hex(byte[] data)
{
String md5 = bufferToHex(md5Encrypt(data));
while (md5.length() < 32) {
md5 = "0" + md5;
}
return md5;
}
private static void appendHexPair(byte b, StringBuffer hexString)
{
char highNibble = kHexChars[((b & 0xF0) >> 4)];
char lowNibble = kHexChars[(b & 0xF)];
hexString.append(highNibble);
hexString.append(lowNibble);
}
public static String hexToString(String hexString) throws NumberFormatException
{
byte[] bytes = hexToBuffer(hexString);
return new String(bytes);
}
public static byte[] hexToBuffer(String hexString) throws NumberFormatException
{
int length = hexString.length();
byte[] buffer = new byte[(length + 1) / 2];
boolean evenByte = true;
byte nextByte = 0;
int bufferOffset = 0;
if (length % 2 == 1)
evenByte = false;
for (int i = 0; i < length; i++) {
char c = hexString.charAt(i);
int nibble;
if ((c >= '0') && (c <= '9')) {
nibble = c - '0';
}
else
{
if ((c >= 'A') && (c <= 'F')) {
nibble = c - 'A' + 10;
}
else
{
if ((c >= 'a') && (c <= 'f'))
nibble = c - 'a' + 10;
else throw new NumberFormatException("Invalid hex digit '" + c + "'.");
}
}
if (evenByte) {
nextByte = (byte)(nibble << 4);
} else {
nextByte = (byte)(nextByte + (byte)nibble);
buffer[(bufferOffset++)] = nextByte;
}
evenByte = !evenByte;
}
return buffer;
}
}
DES java C# asp.net DES/ECB/NoPadding --------------------编程问答-------------------- .net类库本来就带des 3des aes cng等对称加密算法,你只需要提供key和初始化向量就可以了 --------------------编程问答-------------------- http://blog.csdn.net/mr_qu/article/details/8433370
补充:.NET技术 , C#