业务场景:App下单后的订单,需要在另一个内部系统中进行扫码打印。两个程序包括服务器之间网络不互通,所以想到了通过二维码携带内容做数据交互,但是将内容转为base64后发现字符串太长,放入二维码后二维码密度相当大,几乎无法被扫描,所以就想到了给字符串进行压缩,从而减少字符串的长度。在App端进行加密,在内部系统中进行解密读取。
(1)下面是介绍环境配置
①首先是net core版本,此版本NET自带Brotli库,直接用就可以了,案例网上搜一下,这里不进行赘述。
②其他Net Framework版本,需要先引入dll包(放在资源里面了,需要自行下载),然后调用。
(2)下面介绍调用方式
①在net 4以上版本中,因为System.Stream类支持CopyTo()方法,所以直接用下面这种方式就可以。
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);
}
}
}
}
②在net 2.0左右的版本中,由于System.Stream类中不支持CopyTo()方法,所以只能手动将数据流转为byte数组,代码如下。文章来源:https://www.toymoban.com/news/detail-504739.html
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)最后介绍一个数据交互时,压缩数据的案例。
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);//解密
}
在数据接收方,需要用split一段一段截取。文章来源地址https://www.toymoban.com/news/detail-504739.html
到了这里,关于字符串压缩加密算法(可逆)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!