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吗?