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

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 的時候運行結果:apache

文件保存的編碼爲: utf-8 的時候運行結果:segmentfault

由於Windows的控制檯不能顯示 utf-8 編碼的內容, 因此我用 > 把結果輸出到 11.txt 的文件中,
上面的截圖中包含 11.txt 的內容, 能夠看到漢字仍是正確的.
11.txt 文件的編碼爲 utf-8.windows

在文件以UTF-8編碼保存時, 使用下面的方式能夠正確顯示:ide

同理當文件是以gb2312編碼保存的時候, 能夠省略這個參數,或者指定爲 gb2312 就能夠正常顯示了:post


附: Linux 下你的代碼的執行結果(文件的編碼爲 utf-8)
Linux 下個人Console的默認編碼爲UTF-8,因此能夠正常顯示UTF-8編碼的漢字.編碼

總結一下就是 你源文件保存的編碼,與你使用 new String 時傳遞的第二個參數 不一致致使的.加密


相關文章
相關標籤/搜索