Base64
以下加密首先转换成GBK编码,再返回Base64解析后的数据
/// <summary>
/// Base64加密(GBK格式)
/// </summary>
/// <param name="Message"></param>
/// <returns></returns>
public static string Base64Code(string rel)
{
if (string.IsNullOrEmpty(rel)) return "";
//byte[] bytes = Encoding.Default.GetBytes(rel);
var buffer = Encoding.GetEncoding("GBK").GetBytes(rel);
return Convert.ToBase64String(buffer);
}
图片转base64
/// <summary>
/// 图片转base64
/// </summary>
/// <param name="Imagefilename">图片文件地址</param>
/// <returns></returns>
public static string ImgToBase64String(string Imagefilename)
{
try
{
Bitmap bmp = new Bitmap(Imagefilename);
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] arr = new byte[ms.Length];
ms.Position = 0;
ms.Read(arr, 0, (int)ms.Length);
ms.Close();
String strbaser64 = Convert.ToBase64String(arr);
return strbaser64;
}
catch (Exception ex)
{
//LogHelper.Debug(ex);
return "";
}
}
/// <summary>
/// 将PDF转换图片
/// </summary>
/// <param name="pdfInputPath">PDF文件路径</param>
/// <param name="imageOutputPath">图片输出路径</param>
/// <param name="imageName">生成图片的名字</param>
/// <param name="startPageNum">从PDF文档的第几页开始转换</param>
/// <param name="endPageNum">从PDF文档的第几页开始停止转换</param>
/// <param name="imageFormat">设置所需图片格式</param>
/// <param name="definition">设置图片的清晰度,数字越大越清晰</param>
public static string[] PdfToJPEG(string pdfInputPath, string imageOutputPath,
string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, int definition)
{
List<string> outFileList = new List<string>();
PDFFile pdfFile = PDFFile.Open(pdfInputPath);
if (!Directory.Exists(imageOutputPath))
{
Directory.CreateDirectory(imageOutputPath);
}
// pdf pageNum
if (startPageNum <= 0)
{
startPageNum = 1;
}
if (endPageNum > pdfFile.PageCount)
{
endPageNum = pdfFile.PageCount;
}
if (startPageNum > endPageNum)
{
int tempPageNum = startPageNum;
startPageNum = endPageNum;
endPageNum = startPageNum;
}
// 转换每一页
if (endPageNum == 1)
{
Bitmap pageImage = pdfFile.GetPageImage(1 - 1, 56 * (int)definition);
pageImage.Save(imageOutputPath + imageName + "." + imageFormat, imageFormat);
pageImage.Dispose();
outFileList.Add(imageOutputPath + imageName + "." + imageFormat);
}
else
{
for (int i = startPageNum; i <= endPageNum; i++)
{
Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition);
pageImage.Save(imageOutputPath + imageName + i + "." + imageFormat, imageFormat);
pageImage.Dispose();
outFileList.Add(imageOutputPath + imageName + i + "." + imageFormat);
}
}
pdfFile.Dispose();
return outFileList.ToArray(); ;
}
AES/CBC/PKCS5
/// <summary>
/// AES/CBC/PKCS5 加密
/// </summary>
/// <param name="encryptStr">明文</param>
/// <param name="key">密钥</param>
/// <returns></returns>
protected static string Encrypt(string encryptStr, string key, string IV)
{
if (string.IsNullOrEmpty(encryptStr)) return null;
var _aes = new AesCryptoServiceProvider();
_aes.BlockSize = 128;
_aes.KeySize = 256;
_aes.Key = Encoding.UTF8.GetBytes(key);
_aes.IV = Encoding.UTF8.GetBytes(IV); //(byte[])(object)new sbyte[16];
_aes.Padding = PaddingMode.PKCS7;
_aes.Mode = CipherMode.CBC;
var _crypto = _aes.CreateEncryptor(_aes.Key, _aes.IV);
byte[] encrypted = _crypto.TransformFinalBlock(Encoding.UTF8.GetBytes(encryptStr), 0, Encoding.UTF8.GetBytes(encryptStr).Length);
_crypto.Dispose();
return System.Convert.ToBase64String(encrypted);
}
/// <summary>
/// 解密 AES/CBC/PKCS5
/// </summary>
/// <param name="decryptStr">需解密字符串</param>
/// <param name="key">密钥</param>
/// <param name="IV">偏移量</param>
/// <returns></returns>
public static string Decrypt(string decryptStr, string key, string IV)
{
if (string.IsNullOrEmpty(decryptStr)) return null;
var _aes = new AesCryptoServiceProvider();
_aes.BlockSize = 128;
_aes.KeySize = 256;
_aes.Key = Encoding.UTF8.GetBytes(key);
_aes.IV = Encoding.UTF8.GetBytes(IV);// (byte[])(object)new sbyte[16];
_aes.Padding = PaddingMode.PKCS7;
_aes.Mode = CipherMode.CBC;
var _crypto = _aes.CreateDecryptor(_aes.Key, _aes.IV);
byte[] decrypted = _crypto.TransformFinalBlock(
System.Convert.FromBase64String(decryptStr), 0, System.Convert.FromBase64String(decryptStr).Length);
_crypto.Dispose();
return Encoding.UTF8.GetString(decrypted);
}