请懂.net的帮忙翻译一段代码 DES算法应该都相通的,你先去网上找个JAVA版的,测试下看看,然后对一下结果是否一致 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 vb.net 的写法。真心看起费劲。。 LZ头像好妖艳的感觉 +1要注意的的是编码 关键的地方 inputByteArray = Encoding.UTF8.GetBytes(Text) des.Key = ASCIIEncoding.ASCII.GetBytes("12345678") des.IV = ASCIIEncoding.ASCII.GetBytes("12345678") 版主不要乱移版块,我要翻译成java代码,当然在这个区 import javax.crypto.Cipher;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.DESKeySpec;import javax.crypto.spec.IvParameterSpec;public class DESCrypt { private static final char[] HEX_CHAR = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; public static String encrypt(String text, String key) { try { DESKeySpec ks = new DESKeySpec("12345678".getBytes("ASCII")); SecretKeyFactory skf = SecretKeyFactory.getInstance("DES"); SecretKey sk = skf.generateSecret(ks); Cipher cip = Cipher.getInstance("DES/CBC/PKCS5Padding"); IvParameterSpec iv2 = new IvParameterSpec("12345678".getBytes("ASCII")); cip.init(Cipher.ENCRYPT_MODE, sk, iv2); byte[] bytes = cip.doFinal(text.getBytes("UTF-8")); return toHexString(bytes); } catch (Exception ex) { return "EnStrError"; // 还是直接抛异常好 } } public static String decrypt(String text, String key) { try { DESKeySpec ks = new DESKeySpec("12345678".getBytes("ASCII")); SecretKeyFactory skf = SecretKeyFactory.getInstance("DES"); SecretKey sk = skf.generateSecret(ks); Cipher cip = Cipher.getInstance("DES/CBC/PKCS5Padding"); IvParameterSpec iv2 = new IvParameterSpec("12345678".getBytes("ASCII")); cip.init(Cipher.DECRYPT_MODE, sk, iv2); byte[] bytes = new byte[text.length() >> 1]; for (int i = 0, pos = 0; i < bytes.length; i++, pos += 2) { bytes[i] = (byte)Integer.parseInt(text.substring(pos, pos+2), 16); } byte[] decrypt = cip.doFinal(bytes); return new String(decrypt, "UTF-8"); } catch (Exception ex) { return "DeStrError"; } } private static String toHexString(byte[] bytes) { int len = bytes.length << 1; char[] chars = new char[len]; for (int i = 0, pos = 0; i < bytes.length; i++, pos += 2) { int unsignedByte = bytes[i] & 0xFF; chars[pos] = HEX_CHAR[unsignedByte >> 4]; chars[pos + 1] = HEX_CHAR[unsignedByte & 0xF]; } return new String(chars); } public static void main(String[] args) throws Exception { String[] tests = new String[] { "abcd", "1234567890", "Hello World!", "你好,世界!" }; for (String test : tests) { System.out.println(test); String encrypt = encrypt(test, ""); System.out.println(encrypt); String decrypt = decrypt(encrypt, ""); System.out.println(decrypt); } System.out.println(encrypt("abcd", "")); System.out.println(encrypt("Hello World!", "")); System.out.println(encrypt("你好,世界!", "")); }} 参考这篇文章相关VB代码,用等价的C#测试过,至少这几个main方法中测试结果是一致的。 基本数据类型 和 包装类 的区别 请教关于读TXT档 抓字串 计算次数 写出TXT档 jsp和SQL Server如何实现登录? 关于调用一个继承serializable接口的类遇到的奇怪问题,高分相赠!!! ****为什么我的log4j配置不生效? 为什么这样子找不到自定义的包呢? 怎么得到JList中的所有项? jtree中如何在树的每个结点前加上一个小图片,而不使用原有图形? 连接数据库的方法 加载图象 怎么查询没有结果? 代码转移后出现错误NoSuchFieldError
inputByteArray = Encoding.UTF8.GetBytes(Text)
des.Key = ASCIIEncoding.ASCII.GetBytes("12345678")
des.IV = ASCIIEncoding.ASCII.GetBytes("12345678")
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;public class DESCrypt { private static final char[] HEX_CHAR = new char[] {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
'E', 'F'
}; public static String encrypt(String text, String key) {
try {
DESKeySpec ks = new DESKeySpec("12345678".getBytes("ASCII"));
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
SecretKey sk = skf.generateSecret(ks);
Cipher cip = Cipher.getInstance("DES/CBC/PKCS5Padding");
IvParameterSpec iv2 = new IvParameterSpec("12345678".getBytes("ASCII"));
cip.init(Cipher.ENCRYPT_MODE, sk, iv2);
byte[] bytes = cip.doFinal(text.getBytes("UTF-8"));
return toHexString(bytes);
} catch (Exception ex) {
return "EnStrError"; // 还是直接抛异常好
}
} public static String decrypt(String text, String key) {
try {
DESKeySpec ks = new DESKeySpec("12345678".getBytes("ASCII"));
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
SecretKey sk = skf.generateSecret(ks);
Cipher cip = Cipher.getInstance("DES/CBC/PKCS5Padding");
IvParameterSpec iv2 = new IvParameterSpec("12345678".getBytes("ASCII"));
cip.init(Cipher.DECRYPT_MODE, sk, iv2);
byte[] bytes = new byte[text.length() >> 1];
for (int i = 0, pos = 0; i < bytes.length; i++, pos += 2) {
bytes[i] = (byte)Integer.parseInt(text.substring(pos, pos+2), 16);
}
byte[] decrypt = cip.doFinal(bytes);
return new String(decrypt, "UTF-8");
} catch (Exception ex) {
return "DeStrError";
}
} private static String toHexString(byte[] bytes) {
int len = bytes.length << 1;
char[] chars = new char[len];
for (int i = 0, pos = 0; i < bytes.length; i++, pos += 2) {
int unsignedByte = bytes[i] & 0xFF;
chars[pos] = HEX_CHAR[unsignedByte >> 4];
chars[pos + 1] = HEX_CHAR[unsignedByte & 0xF];
}
return new String(chars);
} public static void main(String[] args) throws Exception {
String[] tests = new String[] {
"abcd",
"1234567890",
"Hello World!",
"你好,世界!"
};
for (String test : tests) {
System.out.println(test);
String encrypt = encrypt(test, "");
System.out.println(encrypt);
String decrypt = decrypt(encrypt, "");
System.out.println(decrypt);
}
System.out.println(encrypt("abcd", ""));
System.out.println(encrypt("Hello World!", ""));
System.out.println(encrypt("你好,世界!", ""));
}
}
相关VB代码,用等价的C#测试过,至少这几个main方法中测试结果是一致的。