請問如下的這段java代碼,有沒有法子改造成c#代碼?
嘗試了多次都無果。。。
package com.demo;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import java.security.Key;
import java.security.SecureRandom;
public class Arithmetic {
static Key key;
//密鑰
public static final String DEFAULT_SECRET_KEY = "2qaz3wsx3edc$RFV%TGB^YHN&UJMasdgf3asdwe3qws1233gawe";
static {
key = getKey(DEFAULT_SECRET_KEY);
}
public static Key getKey(String strKey) {
try {
KeyGenerator _generator = KeyGenerator.getInstance("DES");
_generator.init(new SecureRandom(strKey.getBytes()));
key = _generator.generateKey();
_generator = null;
} catch (Exception e) {
e.printStackTrace();
}
return key;
}
public static String getEncString(String strMing) {
byte[] byteMi = null;
byte[] byteMing = null;
String strMi = "";
try {
System.out.println("1."+strMing.getBytes());
System.out.println("2."+getEncCode(strMing.getBytes()));
System.out.println("3."+byte2hex(getEncCode(strMing.getBytes())));
return byte2hex(getEncCode(strMing.getBytes()));
// byteMing = strMing.getBytes("UTF8");
// byteMi = this.getEncCode(byteMing);
// strMi = new String( byteMi,"UTF8");
} catch (Exception e) {
e.printStackTrace();
} finally {
byteMing = null;
byteMi = null;
}
return strMi;
}
public static String getDesString(String strMi) {
byte[] byteMing = null;
byte[] byteMi = null;
String strMing = "";
try {
return new String(getDesCode(hex2byte(strMi.getBytes())));
// byteMing = this.getDesCode(byteMi);
// strMing = new String(byteMing,"UTF8");
} catch (Exception e) {
e.printStackTrace();
} finally {
byteMing = null;
byteMi = null;
}
return strMing;
}
private static byte[] getEncCode(byte[] byteS) {
byte[] byteFina = null;
Cipher cipher;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byteFina = cipher.doFinal(byteS);
} catch (Exception e) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
}
private static byte[] getDesCode(byte[] byteD) {
Cipher cipher;
byte[] byteFina = null;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byteFina = cipher.doFinal(byteD);
} catch (Exception e) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
}
public static String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else
hs = hs + stmp;
}
return hs.toUpperCase();
}
public static byte[] hex2byte(byte[] b) {
if ((b.length % 2) != 0)
throw new IllegalArgumentException("長度不是偶數");
byte[] b2 = new byte[b.length / 2];
for (int n = 0; n < b.length; n += 2) {
String item = new String(b, n, 2);
b2[n / 2] = (byte) Integer.parseInt(item, 16);
}
return b2;
}
public static void main(String[] args) {
String strEnc = Arithmetic.getEncString("wangmn");
System.out.println(strEnc);
String strDes = Arithmetic.getDesString(strEnc);
System.out.println(strDes);
}
}
uj5u.com熱心網友回復:
https://www.cnblogs.com/zywf/p/7565103.html沒必要照著這個代碼翻譯 有現成的
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/284958.html
標籤:C#
上一篇:ie瀏覽器問題
