简体   繁体   English

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

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

I'm using Base64 for encode a very huge JSON string with thousands of data, after encoded I'm storing on disk.我正在使用 Base64 对包含数千个数据的非常大的 JSON 字符串进行编码,编码后我将其存储在磁盘上。 Later I'm retrieving again from disk and decoding it again into readable normal string 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));
}

Does a limitation exist in the size of the Base64 encode and decode functions? Base64 编码和解码函数的大小是否存在限制? Or am I able to encode and decode super big strings?或者我可以编码和解码超大字符串吗?

No maximum size, but I'd like to also suggest different approach to encrypt and decrypt没有最大大小,但我还想建议不同的加密解密方法

Encrypt加密

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;
}

Decrypt解密

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 can always be a constant in your program, but it is not recommended. key 在您的程序中始终可以是常量,但不建议这样做。

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

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