java加解密操作过程中的中文乱码问题
Posted 旧巷里的旧少年
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java加解密操作过程中的中文乱码问题相关的知识,希望对你有一定的参考价值。
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吗?
以上是关于java加解密操作过程中的中文乱码问题的主要内容,如果未能解决你的问题,请参考以下文章