java 写的3Des加密解密,要在C#中实现相同的功能,求大神帮忙。小弟感觉难点在java根据密码生成一个密钥,但是C#Des加密Key值用的是base64
java代码:
import java.security.MessageDigest;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
/**
* Common utility methods to encrypt/decrypt numbers and strings.
*/
public class Crypts {
private static final char[] HEX_CHARACTERS = { '0', '1', '2', '3', '4','5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
/**
* Encrypts griven string with given pass-phrase with DESede.
*/
public static String cipherDESede(String msg, byte[] passPhrase) {
try {
KeySpec keySpec = new DESedeKeySpec(passPhrase);
SecretKey key = SecretKeyFactory.getInstance("DESede")
.generateSecret(keySpec);
Cipher cipher = Cipher.getInstance(key.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, key);
return toHexString(cipher.doFinal(msg.getBytes()));
} catch (Exception e) {
return null;
}
}
/**
* Decrypts griven string with given pass-phrase with DESede.
*/
public static String decipherDESede(String msg, byte[] passPhrase) {
try {
KeySpec keySpec = new DESedeKeySpec(passPhrase);
SecretKey key = SecretKeyFactory.getInstance("DESede")
.generateSecret(keySpec);
Cipher cipher = Cipher.getInstance(key.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, key);
return new String(cipher.doFinal(fromHexString(msg)));
} catch (Exception e) {
return null;
}
}
/**
* Creates a md5 encoded for given input.
*/
public static String md5(byte[] input) {
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.reset();
md5.update(input);
return toHexString(md5.digest());
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
/**
* Converts given byte-array into hexadecimal.
*/
public static String toHexString(byte[] b) {
StringBuffer sb = new StringBuffer(b.length * 2);
for (int i = 0; i < b.length; i++) {
sb.append(HEX_CHARACTERS[(b[i] & 0xf0) >>> 4]);
sb.append(HEX_CHARACTERS[b[i] & 0x0f]);
}
return sb.toString();
}
/**
* Converts given hexadecimal into byte-array.
*/
public static byte[] fromHexString(String s) {
int stringLength = s.length();
if ((stringLength % 2) != 0) {
throw new IllegalArgumentException(
"Even number of characters required");
}
byte[] b = new byte[stringLength / 2];
for (int i = 0, j = 0; i < stringLength; i += 2, j++) {
int high = charToNibble(s.charAt(i));
int low = charToNibble(s.charAt(i + 1));
b[j] = (byte) ((high << 4) | low);
}
return b;
}
private static int charToNibble(char c) {
if ('0' <= c && c <= '9') {
return c - '0';
} else if ('a' <= c && c <= 'f') {
return c - 'a' + 0xa;
} else if ('A' <= c && c <= 'F') {
return c - 'A' + 0xa;
} else {
throw new IllegalArgumentException("Invalid hex character: " + c);
}
}
public static void main(String[] args) {
String cryptKey = "Portal$Token@Qilinsoft#com";
String msg = "portal010@cmbc.com";
//DESede Encrypt
String passwd = cipherDESede(msg, cryptKey.getBytes());
//DESede Decrypt
String original = decipherDESede(passwd, cryptKey.getBytes());
System.out.println("orignial:" + original + " passwd:" + passwd);
String md5pwd = md5(msg.getBytes());//MD5 Encrypt
System.out.println("md5 passwd:" + md5pwd);
}
}
MD5 加密可以不用考虑。 求大神帮忙,纠结好长时间了 ,在线等回复,谢谢。