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

时间:2019-06-06 07:10:12

标签: java android encryption cryptography

在将加密应用于字符串后,我遇到了问题,我想将该加密的字符串解密为普通字符串,但是所有示例均无用。

此外,它们正在处理字节数组代码,对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 个答案:

答案 0 :(得分:0)

您可以使用Base64编码和解码。

示例:

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 =

     

测试

答案 1 :(得分:-1)

  

这里是简单的编码和解码(在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;
   }
 }

希望这个答案对您有帮助。