亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

java密碼問題,當我想破譯它時

java密碼問題,當我想破譯它時

慕婉清6462132 2021-08-25 09:56:12
我用 javafx 制作了一個應用程序,我可以編寫一些東西并將其保存到數據庫中。我的數據庫是sqlite。這是一個非常簡單的應用程序。雖然我已經在我的寫作應用程序中添加了登錄應用程序,但仍然可以通過任何軟件打開 sqlite。而不是加密sqlite db(我不知道,我發現做起來真的很困惑:))我決定在java中加密文本,稍后當我想閱讀它時,我會將它恢復正常并顯示它。我從這個鏈接學會了如何做到這一點,我將其更改為打印字符串而不是寫入文件,因此我的最終代碼如下所示:public static void main(String[] args) throws Exception {    String textA = "";    String textB="";    byte[] thisismykey = "Hello How manyBytes are in@hts A".getBytes();    SecretKey secKey = new SecretKeySpec(thisismykey, "AES");    Cipher aesCipher = Cipher.getInstance("AES");    //turn your original text to byte    byte[] myoriginaltexttobyte = "Your Plain Text Here".getBytes();    //activate the encrypt method    aesCipher.init(Cipher.ENCRYPT_MODE, secKey);    //encrypt the text and assign the encrypted text to a byte array    byte[] bytecipheredoforgtext = aesCipher.doFinal(myoriginaltexttobyte);    //change it to string with new string    textA = new String(bytecipheredoforgtext);    System.out.println(textA);    //get the bytes of encrypted text and assign it to a byte array    byte[] byteofencryptedtext = textA.getBytes();    //activate the decrypt mode of the cipher    aesCipher.init(Cipher.DECRYPT_MODE, secKey);    //decrypt the encrypted text and assign it to a byte array    byte[] byteofencryptedbacktonormaltext = aesCipher.doFinal(byteofencryptedtext);    //change it to string with new string    textB = new String(byteofencryptedbacktonormaltext);    System.out.println(textB);}現在加密和解密使用相同的方法,它可以完美運行,但我想將其更改為具有不同方法的類,以便我可以使用一種方法加密文本并使用另一種方法解密。但是當我將事物分開時,解密效果不佳。加密工作良好。我該怎么辦?
查看完整描述

1 回答

?
慕后森

TA貢獻1802條經驗 獲得超5個贊

我不確定錯誤是由這個引起的,但是您正在執行的 String 和 byte[] 之間的轉換可能適用于字符集范圍內的字節,但是當數據被加密時,您可能在數組中有任何字節,不僅可以打印人物。

在獲得帶有加密文本的 byte[] 后,將其編碼為 Base64 并存儲字符串表示。

解密的時候,先把Base64解碼成byte[],然后再解密。


查看完整回答
反對 回復 2021-08-25
  • 1 回答
  • 0 關注
  • 144 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號