java 写的3Des加密解密,要在C#中实现相同的功能,求大神帮忙。小弟感觉难点在java根据密码生成一个密钥,但是C#Des加密Key值用的是base64

他们都叫我胖子 2012-09-23 08:49:03
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 加密可以不用考虑。 求大神帮忙,纠结好长时间了 ,在线等回复,谢谢。
...全文
279 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
水猿兵团五哥 2012-09-24
  • 打赏
  • 举报
回复
什么??
yonglaixiazaide 2012-09-24
  • 打赏
  • 举报
回复
是不是 那个地方没有转换对。。
  • 打赏
  • 举报
回复
这是一个JAVA写的程序,现在要用C#实现相同的功能,JAVA加密的东西可以C#解密,C#程序可以根据JAVA中使用的密码加密文本,并且JAVA程序可以解密,
有点绕,求大侠帮忙

111,045

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧