麻烦牛人将java代码转换为c#或vb.net代码,要求能够运行且功能相同。
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class TripleDes {
public static String encroptMode = "ECB";
public static String paddingMode = "PKCS5Padding";
public static String algorithm = "DESede";
public static String charset = "UTF-8";
public static void main(String[] args) {
String oriData = "aabbcc";
String keyStr = "abcdefghijklmnopqrstuvwxyz123456"; //24位密钥,经base64编码
System.out.println("oriData:"+oriData);
String encData = EncryptData(oriData,keyStr);
System.out.println("encData:"+encData);
String decData = DecryptData(encData,keyStr);
System.out.println("decData:"+decData);
}
/**
* 加密
*/
public static String EncryptData(String oriData, String keyStr) {
BASE64Decoder base64decoder = new BASE64Decoder();
BASE64Encoder base64encoder = new BASE64Encoder();
try {
byte[] keyBytes = base64decoder.decodeBuffer(keyStr);
SecretKey key = new SecretKeySpec(keyBytes, "DESede");
Cipher cipher = Cipher.getInstance(algorithm + "/" + encroptMode + "/" + paddingMode);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptBytes = cipher.doFinal(oriData.getBytes(charset));
String encryptStr = base64encoder.encode(encryptBytes);
return encryptStr;
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
/**
* 解密
*/
public static String DecryptData(String encData, String keyStr) {
BASE64Decoder base64decoder = new BASE64Decoder();
try {
byte[] keyBytes = base64decoder.decodeBuffer(keyStr);
SecretKey key = new SecretKeySpec(keyBytes, "DESede");
Cipher cipher = Cipher.getInstance(algorithm + "/" + encroptMode + "/" + paddingMode);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decBytes = cipher.doFinal(base64decoder.decodeBuffer(encData));
String decData = new String(decBytes, charset);
return decData;
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
}