繁体   English   中英

编码字符串是否有最大 base64 大小?

[英]Is there a maximum base64 size for a encoded string?

我正在使用 Base64 对包含数千个数据的非常大的 JSON 字符串进行编码,编码后我将其存储在磁盘上。 后来我再次从磁盘中检索并将其再次解码为可读的普通字符串 JSON。

public static String encryptString(String string) {     
    byte[] bytesEncoded = Base64.getMimeEncoder().encode(string.getBytes());
    return (new String(bytesEncoded));
}

public static String decryptString(String string) {
    byte[] bytesDecoded = Base64.getMimeDecoder().decode(string);
    return (new String(bytesDecoded));
}

Base64 编码和解码函数的大小是否存在限制? 或者我可以编码和解码超大字符串吗?

没有最大大小,但我还想建议不同的加密解密方法

加密

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);
        String isoText = new String(strClearText.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1); // there are shorthand ways of doing this, but this is for your explaination
        byte[] encrypted=cipher.doFinal(isoText.getBytes(StandardCharsets.ISO_8859_1));
        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));
        isoStrData=new String(decrypted, StandardCharsets.ISO_8859_1);
        strData=new String(isoStrData.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception(e);
    }
    return strData;
}

key 在您的程序中始终可以是常量,但不建议这样做。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM