如何把下面java的DES加密的写法写成C#的,用C#来实现?求高手指教!

importcom.sun.crypto.provider.SunJCE;importjava.security.Key;importjava.security.Secu... import com.sun.crypto.provider.SunJCE;
import java.security.Key;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class DESSSO {

private static String strDefaultKey = "national";

private Cipher encryptCipher = null;

private Cipher decryptCipher = null;

public static String byteArr2HexStr(byte[] arrB)
throws Exception
{
int iLen = arrB.length;

StringBuffer sb = new StringBuffer(iLen * 2);
for (int i = 0; i < iLen; ++i) {
int intTmp = arrB[i];

while (intTmp < 0) {
intTmp += 256;
}

if (intTmp < 16) {
sb.append("0");
}
sb.append(Integer.toString(intTmp, 16));
}
return sb.toString();
}

public static byte[] hexStr2ByteArr(String strIn)
throws Exception
{
byte[] arrB = strIn.getBytes();
int iLen = arrB.length;

byte[] arrOut = new byte[iLen / 2];
for (int i = 0; i < iLen; i += 2) {
String strTmp = new String(arrB, i, 2);
arrOut[(i / 2)] = (byte)Integer.parseInt(strTmp, 16);
}
return arrOut;
}

public DESSSO()
throws Exception
{
this(strDefaultKey);
}

public DESSSO(String strKey)
throws Exception
{
Security.addProvider(new SunJCE());
Key key = getKey(strKey.getBytes());

this.encryptCipher = Cipher.getInstance("DES");
this.encryptCipher.init(1, key);

this.decryptCipher = Cipher.getInstance("DES");
this.decryptCipher.init(2, key);
}

public byte[] encrypt(byte[] arrB)
throws Exception
{
return this.encryptCipher.doFinal(arrB);
}

public String encrypt(String strIn)
throws Exception
{
return byteArr2HexStr(encrypt(strIn.getBytes()));
}

public byte[] decrypt(byte[] arrB)
throws Exception
{
return this.decryptCipher.doFinal(arrB);
}

public String decrypt(String strIn)
throws Exception
{
return new String(decrypt(hexStr2ByteArr(strIn)));
}

private Key getKey(byte[] arrBTmp)
throws Exception
{
byte[] arrB = new byte[8];

for (int i = 0; (i < arrBTmp.length) && (i < arrB.length); ++i) {
arrB[i] = arrBTmp[i];
}

Key key = new SecretKeySpec(arrB, "DES");

return key;
}
}
展开
 我来答
freeeeeewind
2013-09-29 · TA获得超过1万个赞
知道大有可为答主
回答量:3227
采纳率:94%
帮助的人:1301万
展开全部
直接用 System.Security.Cryptography 下的 DES 类 实现
追问
我知道用Cryptography类。但是不知道根据我java的加密写法来做。
追答
java 与 C# 互通的des加密
请参考:http://hi.baidu.com/crane0713/item/45af04e56644b43f86d9de43
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式