String compression encryption algorithm (reversible)

Business scenario: After the order is placed on the App, the order needs to be scanned and printed in another internal system. The two programs, including the server, do not communicate with each other through the network, so I thought of carrying the content through the QR code for data interaction, but after converting the content to base64, I found that the string was too long. After putting the QR code into the QR code, the density of the QR code is quite large, and it is almost impossible to scan, so I thought of compressing the string to reduce the length of the string. It is encrypted on the App side and decrypted and read in the internal system.

(1) The following is an introduction to the environment configuration

① The first is the net core version. This version of NET comes with the Brotli library, which can be used directly. Search the case online, and I won’t go into details here.

② For other Net Framework versions, you need to import the dll package first ( put it in the resource, you need to download it yourself ), and then call it.

(2) The calling method is introduced below

①In versions above net 4, because the System.Stream class supports the CopyTo() method, you can directly use the following method.

    public static class EncryptionHelper
    {
        public static string Encode(string str)
        {
            var bytes = System.Text.Encoding.UTF8.GetBytes(str);
            using (var memoryStream = new MemoryStream())
            {
                using (var brotliStream = new BrotliStream(memoryStream, CompressionMode.Compress,true))
                {
                    brotliStream.Write(bytes, 0, bytes.Length);
                }

                var result = Convert.ToBase64String(memoryStream.ToArray());
                return result;
            }
        }

        public static string DeEncode(string str)
        {
            var bytes = Convert.FromBase64String(str);
            using (var memoryStream = new MemoryStream(bytes))
            {
                using (var outputStream = new MemoryStream())
                {
                    using (var decompressStream = new BrotliStream(memoryStream, CompressionMode.Decompress))
                    {
                        decompressStream.CopyTo(outputStream);
                    }
                    var buffer = outputStream.ToArray();

                    return System.Text.Encoding.UTF8.GetString(buffer);
                }
            }
        }
    }

②In versions around net 2.0, since the System.Stream class does not support the CopyTo() method, the data stream can only be converted into a byte array manually, and the code is as follows.

    public static class EncryptionHelper
    {
        public static string Encode(string str)
        {
            var bytes = System.Text.Encoding.UTF8.GetBytes(str);
            using (var memoryStream = new MemoryStream())
            {
                using (var brotliStream = new Brotli.BrotliStream(memoryStream, CompressionMode.Compress, true))
                {
                    brotliStream.Write(bytes, 0, bytes.Length);
                }

                var result = Convert.ToBase64String(memoryStream.ToArray());
                return result;
            }
        }

        public static string DeEncode(string str)
        {
            var bytes = Convert.FromBase64String(str);
            using (var memoryStream = new MemoryStream(bytes))
            {
                var decompressStream = new Brotli.BrotliStream(memoryStream, CompressionMode.Decompress);
                byte[] mybytes = new byte[64*1024];
                decompressStream.Read(mybytes, 0, mybytes.Length);
                decompressStream.Dispose();

                Stream s = new MemoryStream();
                return System.Text.Encoding.UTF8.GetString(mybytes);

            }
        }

    }

(3) Finally, a case of compressing data during data interaction is introduced.

    public class AppPrintinfo
    {
        /// <summary>
        /// 
        /// </summary>
        public string Filea{ get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string Fileb{ get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string Filec{ get; set; }

        /// <summary>
        /// 
        /// </summary>
        public List<AppPrintDetail> MRList { get; set; }

        public override string ToString()
        {
            var l = string.Join("|", MRList.Select(s =>s.ToString()).ToArray());


            var r = Filea+ "`" + Fileb+ "`" + Filec+ "<" + l;

            return r;
        }

    }

    public class AppPrintDetail
    {
        /// <summary>
        /// 
        /// </summary>
        public string Filed1 { get; set; }
        public string Filed2 { get; set; }
        public string Filed3 { get; set; }
        public string Filed4 { get; set; }
        public string Filed5 { get; set; }

        public override string ToString()
        {
            return Filed1+ "`" + Filed2+ "`" + Filed3+ "`" + Filed4+ "`" + Filed5;
        }
    }

 public static void test()
        {
            AppPrintinfo appPrintinfo = new AppPrintinfo();
            appPrintinfo.Filea= "";
            appPrintinfo.Fileb= "";
            appPrintinfo.Filec= "";
            List<AppPrintDetail> list = new List<AppPrintDetail>();
            list.Add(new AppPrintDetail() {.....});
            list.Add(new AppPrintDetail() {.....});
            list.Add(new AppPrintDetail() {.....});
            list.Add(new AppPrintDetail() {.....});
            list.Add(new AppPrintDetail() {.....});
            list.Add(new AppPrintDetail() {.....});
            var str = appPrintinfo.ToString();//先尽可能地压缩数据

            var enCodeStr = EncryptionHelper.TestEncode(str);//加密

            var deEnCodeStr = EncryptionHelper.TestDeEncode(enCodeStr);//解密
        }

On the data receiving side, you need to use split to intercept segment by segment.

Guess you like

Origin blog.csdn.net/weixin_45963929/article/details/126585055