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

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

是否有加密不適用于字符串格式

是否有加密不適用于字符串格式

慕哥6287543 2023-01-05 17:20:33
將加密應用于字符串后我遇到了問題,我想將該 encrypted_string 解密為普通字符串,但這些示例均無效。此外,他們正在為字節數組代碼工作,Byte_array 加密和解密非常好,但我需要它為字符串工作。例如,我已經嘗試過, How to encrypt and decrypt String with my passphrase in Java (Pc not mobile platform)?public static String encrypt(String strClearText,String strKey) throws Exception{    String strData="";    try {        SecretKeySpec skeyspec=new SecretKeySpec(strKey.getBytes(),"Blowfish");        Cipher cipher=Cipher.getInstance("Blowfish");        cipher.init(Cipher.ENCRYPT_MODE, skeyspec);        byte[] encrypted=cipher.doFinal(strClearText.getBytes());        strData=new String(encrypted);    } catch (Exception e) {        e.printStackTrace();        throw new Exception(e);    }    return strData;}public static String decrypt(String strEncrypted,String strKey) throws Exception{    String strData="";    try {        SecretKeySpec skeyspec=new SecretKeySpec(strKey.getBytes(),"Blowfish");        Cipher cipher=Cipher.getInstance("Blowfish");        cipher.init(Cipher.DECRYPT_MODE, skeyspec);        byte[] decrypted=cipher.doFinal(strEncrypted.getBytes());        strData=new String(decrypted);    } catch (Exception e) {        e.printStackTrace();        throw new Exception(e);    }    return strData;}Stringbyte[]然后轉換不能byte[]正常string工作?
查看完整描述

2 回答

?
白衣非少年

TA貢獻1155條經驗 獲得超0個贊

您可以使用 Base64 enocde 和解碼。


例子:


package test;


import java.util.Base64;


import javax.crypto.Cipher;

import javax.crypto.spec.SecretKeySpec;



public class Test2 {


    public static void main(String[] args) throws Exception {

        String key = "abc123!";

        String encrypted = encrypt("test", key);

        System.out.println(encrypted);

        String decrypted = decrypt(encrypted, key);

        System.out.println(decrypted);

    }


    public static String encrypt(String strClearText, String strKey) throws Exception {

        String strData = "";


        try {

            SecretKeySpec skeyspec = new SecretKeySpec(strKey.getBytes(), "Blowfish");

            Cipher cipher = Cipher.getInstance("Blowfish");

            cipher.init(Cipher.ENCRYPT_MODE, skeyspec);

            byte[] encrypted = cipher.doFinal(Base64.getDecoder().decode(strClearText));

            strData = Base64.getEncoder().encodeToString(encrypted);


        } catch (Exception e) {

            e.printStackTrace();

            throw new Exception(e);

        }

        return strData;

    }


    public static String decrypt(String strEncrypted, String strKey) throws Exception {

        String strData = "";


        try {

            SecretKeySpec skeyspec = new SecretKeySpec(strKey.getBytes(), "Blowfish");

            Cipher cipher = Cipher.getInstance("Blowfish");

            cipher.init(Cipher.DECRYPT_MODE, skeyspec);

            byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(strEncrypted));

            strData = Base64.getEncoder().encodeToString(decrypted);


        } catch (Exception e) {

            e.printStackTrace();

            throw new Exception(e);

        }

        return strData;

    }


}

輸出:


fsmwp8c1n9w=


測試


查看完整回答
反對 回復 2023-01-05
?
四季花海

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

這里簡單的編解碼(上面kitkat)


寫這個類,只調用方法


class EncodeDecode

{

    public String encodeString(String text)

  {

    String b64;

    byte[] data=new byte[0];

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) 

    {

        data=text.getBytes(StandardCharsets.UTF_8);

        b64=Base64.encodeToString(data,Base64.DEFAULT);

    }


    return b64;

  } 


public String decodeString(String text)

   {

    String deString;

        if(android.os.Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.KITKAT)

        {

            byte[] data2=Base64.decode(base64,Base64.DEFAULT);

            deString=new String (data2,StandardCharsets.UTF_8);

        }

        return deString;

   }

 }

我希望這個答案能幫助你..


查看完整回答
反對 回復 2023-01-05
  • 2 回答
  • 0 關注
  • 162 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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