如何把下面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;
}
} 展开
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;
}
} 展开
1个回答
展开全部
直接用 System.Security.Cryptography 下的 DES 类 实现
追问
我知道用Cryptography类。但是不知道根据我java的加密写法来做。
追答
java 与 C# 互通的des加密
请参考:http://hi.baidu.com/crane0713/item/45af04e56644b43f86d9de43
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询