试试这个,…很简单
import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;public class HelloWorld{ public static void main(String[] args) { try{ KeyGenerator keygenerator = KeyGenerator.getInstance("DES"); SecretKey myDesKey = keygenerator.generateKey(); Cipher desCipher; desCipher = Cipher.getInstance("DES"); byte[] text = "No body can see me.".getBytes("UTF8"); desCipher.init(Cipher.ENCRYPT_MODE, myDesKey); byte[] textEncrypted = desCipher.doFinal(text); String s = new String(textEncrypted); System.out.println(s); desCipher.init(Cipher.DECRYPT_MODE, myDesKey); byte[] textDecrypted = desCipher.doFinal(textEncrypted); s = new String(textDecrypted); System.out.println(s); }catch(Exception e) { System.out.println("Exception"); } }}
因此,基本上,在写入文件之前,您需要加密,而在读取文件之后,需要解密。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)