import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import org.apache.commons.codec.BinaryDecoder;
import org.apache.commons.codec.binary.Hex;
public class DESDemo {
public static final String src = "des test";
public static void main(String[] args)
{
jdkDES();
bcDES();
}
private static void bcDES() {
}
private static void jdkDES() {
try{
//生成KEY
KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
keyGenerator.init(56);
System.out.println(keyGenerator.getAlgorithm());
System.out.println(keyGenerator.getProvider());
SecretKey secretKey = keyGenerator.generateKey();
byte[] bytesKey = secretKey.getEncoded();
System.out.println(secretKey.getAlgorithm());
System.out.println(secretKey.getFormat());
//通过KEY转换生成密钥
DESKeySpec desKeySpec = new DESKeySpec(bytesKey);
SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
SecretKey convertedKey = factory.generateSecret(desKeySpec);
//加密
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");// 算法/工作方式/填充方式
cipher.init(Cipher.ENCRYPT_MODE, convertedKey);
byte[] result = cipher.doFinal("要加密的内容".getBytes());
System.out.println("jdk des encrypt:" + Hex.encodeHexString(result));
//解密
cipher.init(Cipher.DECRYPT_MODE,convertedKey);
result = cipher.doFinal(result);
// System.out.println("jdk des decrypt:" + new String(result)); //jdk des decrypt:?????????? 直接输出会乱码
System.out.println("jdk des decrypt:" + new String( result,"utf-8" )); //jdk des decrypt:?????????? 依旧乱码
}
catch(Exception e) {
e.printStackTrace();
}
}
}
请问windows系统默认编码是gbk吗?
4 个回答
答案对人有帮助,有参考价值0答案没帮助,是错误的答案,答非所问
还是楼主的代码, 没发现什么乱码:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
public class DESDemo {
public static void main(String[] args)
{
try{
//生成KEY
KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
keyGenerator.init(56);
System.out.println(keyGenerator.getAlgorithm());
System.out.println(keyGenerator.getProvider());
SecretKey secretKey = keyGenerator.generateKey();
byte[] bytesKey = secretKey.getEncoded();
System.out.println(secretKey.getAlgorithm());
System.out.println(secretKey.getFormat());
//通过KEY转换生成密钥
DESKeySpec desKeySpec = new DESKeySpec(bytesKey);
SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
SecretKey convertedKey = factory.generateSecret(desKeySpec);
//加密
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");// 算法/工作方式/填充方式
cipher.init(Cipher.ENCRYPT_MODE, convertedKey);
byte[] result = cipher.doFinal("要加密的内容".getBytes());
//解密
cipher.init(Cipher.DECRYPT_MODE,convertedKey);
result = cipher.doFinal(result);
System.out.println("jdk des decrypt:" + new String(result));
}
catch(Exception e) {
e.printStackTrace();
}
}
}
文件保存的编码为: gb2312
的时候运行结果:
文件保存的编码为: utf-8
的时候运行结果:
因为Windows的控制台不能显示 utf-8
编码的内容, 所以我用 >
把结果输出到 11.txt
的文件中,
上面的截图中包含 11.txt
的内容, 可以看到汉字还是正确的.11.txt
文件的编码为 utf-8
.
在文件以UTF-8编码保存时, 使用下面的方式可以正确显示:
同理当文件是以gb2312
编码保存的时候, 可以省略这个参数,或者指定为 gb2312
就可以正常显示了:
附: Linux 下你的代码的执行结果(文件的编码为 utf-8
)
Linux 下我的Console
的默认编码为UTF-8
,所以可以正常显示UTF-8
编码的汉字.
总结一下就是 你源文件保存的编码,与你使用 new String
时传递的第二个参数 不一致导致的.