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;PublicClassDesdemo {PublicStaticFinal String src ="Des Test";PublicStaticvoidMain(string[] args) {jdkdes (); Bcdes ();}PrivateStaticvoidBcdes() { }PrivateStaticvoidJdkdes() {try{Generate 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 ());Generate key by key conversion Deskeyspec Deskeyspec =New Deskeyspec (Byteskey); Secretkeyfactory factory = Secretkeyfactory.getinstance ("DES"); Secretkey Convertedkey = Factory.generatesecret (Deskeyspec);Encryption Cipher Cipher = Cipher.getinstance ("des/ecb/pkcs5padding"); //algorithm/working mode/Fill mode cipher.init (Cipher.encrypt_mode, Convertedkey); byte[] result = cipher.dofinal ("content to encrypt". GetBytes ()); System.out.println ("jdk des encrypt:" + hex.encodehexstring (result)); //Decrypt Cipher.init (cipher.decrypt_mode,convertedkey); result = Cipher.dofinal (result); //System.out.println ("JDK des Decrypt:" + new String (result)); JDK des decrypt:?????????? The direct output will be garbled System.out.println ("jdk des Decrypt:" + new String (result,"Utf-8")); //jdk des Decrypt:?????????? still garbled} catch (Exception e) {e.printstacktrace (); }}}
is the default encoding for Windows system GBK?
- October 15, 2015 question
- 1 reviews
- Invitation to answer
Sort By default sorting time 4 answersthe answer is helpful to the person, has the reference value0 The answer is no help, it's the wrong answer, irrelevantly replying .
Or the code of the landlord, did not find what garbled:
Import Javax.crypto.cipher;import Javax.crypto.keygenerator;import Javax.crypto.secretkey;import Javax.crypto.secretkeyfactory;import Javax.crypto.spec.DESKeySpec;PublicClassDesdemo {PublicStaticvoidMain(string[] args) {try{Generate 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 ());Generate key by key conversion Deskeyspec Deskeyspec =new Deskeyspec (Byteskey); Secretkeyfactory factory = secretkeyfactory.getinstance ("DES"); Secretkey Convertedkey = Factory.generatesecret (Deskeyspec); //encryption Cipher Cipher = Cipher.getinstance ("des/ecb/pkcs5padding"); Algorithm/working mode/Fill mode cipher.init (Cipher.encrypt_mode, Convertedkey); byte[] result = cipher.dofinal ("content to encrypt". GetBytes ()); //Decrypt Cipher.init (cipher.decrypt_mode,convertedkey); result = Cipher.dofinal (result); System. out.println ("jdk des Decrypt:" + new String (result)); catch (Exception e) {e.printstacktrace (); }}}
The file is saved as: gb2312 when the result is run:
The file is saved as: utf-8 when the result is run:
Because the console of Windows cannot display the utf-8 encoded content, I use the > file to output 11.txt the results,
The above contains 11.txt the contents, you can see the Chinese character is still correct.
11.txtThe encoding of the file is utf-8 .
When a file is saved in UTF-8 encoding, it can be displayed correctly using the following method:
Similarly, when a file is saved as an gb2312 encoding, you can omit this parameter, or specify gb2312 it to be displayed as normal:
Attached: Linux execution results of your code (file encoded as utf-8 )
My Console default encoding for Linux is UTF-8 , so I can display UTF-8 the encoded characters normally.
To summarize, it is the encoding of your source file that is not consistent with the new String second argument you passed when you used it.
The problem of Chinese garbled characters during Java encryption and decryption operation