ZYW.ZYWConvert
1.0.5
dotnet add package ZYW.ZYWConvert --version 1.0.5
NuGet\Install-Package ZYW.ZYWConvert -Version 1.0.5
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ZYW.ZYWConvert" Version="1.0.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ZYW.ZYWConvert" Version="1.0.5" />
<PackageReference Include="ZYW.ZYWConvert" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ZYW.ZYWConvert --version 1.0.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: ZYW.ZYWConvert, 1.0.5"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ZYW.ZYWConvert@1.0.5
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ZYW.ZYWConvert&version=1.0.5
#tool nuget:?package=ZYW.ZYWConvert&version=1.0.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
ConvertHelper - 通用转换与加密辅助类库
一个轻量级的.NET辅助类库,提供丰富的类型转换、加密解密、位操作、时间戳转换等工具方法,简化日常开发中的通用操作。
功能特性
🔐 加密解密
- 位交换加密/解密:基于字节位交换的自定义加密方式,支持多组位索引交换
- MD5哈希:字符串MD5加密(32位小写)
- AES加密解密:
- CBC模式(支持自定义Key/IV,默认内置密钥)
- ECB模式(支持自定义Key,默认内置密钥)
- 所有加密输出均支持16进制字符串格式
📊 类型转换
- 基础类型转换:string ↔ int/double/byte/bool
- 字节数组与16进制字符串互转
- 数值类型的位操作:
- byte/int16/int32 转布尔数组(支持大端/小端)
- 提取指定位置的位值
- 多字节拼接/拆分
- CRC16校验码生成
⏰ 时间戳转换
支持基于2025-01-01基准时间的多粒度时间戳转换:
- 天/分钟/秒/毫秒 与 DateTime 互转
- 当前时间快速生成对应粒度时间戳
快速开始
安装
Install-Package ConvertHelper
或通过.NET CLI安装:
dotnet add package ConvertHelper
命名空间
using Helper;
常用示例
1. MD5加密
string original = "hello world";
string md5Result = original.ToMd5();
// 输出:5eb63bbbe01eeed093cb22bb8f5acdc3
2. AES CBC加密解密
// 加密
string plainText = "敏感数据";
string encrypted = plainText.ToAESCBC();
// 解密
string decrypted = encrypted.ToStrFromAESCBC();
3. 位交换加密
// 使用默认位交换规则(1↔6, 2↔5, 3↔4)
string original = "test";
string encrypted = original.ToSwapEnCode();
string decrypted = encrypted.ToSwapDECode();
// 自定义位交换规则
var swapRules = new[] {
new KeyValuePair<int, int>(0, 7),
new KeyValuePair<int, int>(3, 4)
};
string customEncrypted = original.ToSwapEnCode(swapRules);
4. 16进制字符串与字节数组互转
// 16进制字符串转字节数组
string hexStr = "1A2B3C";
byte[] bytes = hexStr.ToByteArr();
// 字节数组转16进制字符串
string hexResult = bytes.ToHexStr(); // 带空格分隔:"1A 2B 3C"
string hexNoSplit = bytes.ToHexStrNoSplit(); // 无分隔:"1A2B3C"
5. 时间戳转换
// DateTime转天级时间戳(基于2025-01-01)
DateTime dt = new DateTime(2025, 01, 10);
int dayStamp = dt.ToTimeStampAsDay(); // 输出:9
// 时间戳转回DateTime
DateTime newDt = dayStamp.ToDateTimeByDay(); // 输出:2025-01-10 00:00:00
6. 位操作
// 提取byte的第3位值
byte b = 0b10101010;
bool bitValue = b.ToBit(3); // 输出:true
// int32转布尔数组
int num = 123;
bool[] boolArray = num.ToBoolArray();
核心API参考
加密相关
| 方法 | 说明 |
|---|---|
string.ToMd5() |
生成32位小写MD5哈希 |
string.ToAESCBC(byte[] key, byte[] iv) |
AES CBC加密,返回16进制字符串 |
string.ToStrFromAESCBC(byte[] key, byte[] iv) |
AES CBC解密,输入16进制字符串 |
string.ToAESECB(byte[] key) |
AES ECB加密,返回16进制字符串 |
string.ToStrFromAESECB(byte[] key) |
AES ECB解密,输入16进制字符串 |
string.ToSwapEnCode(KeyValuePair<int,int>[]) |
位交换加密,返回16进制字符串 |
string.ToSwapDECode(KeyValuePair<int,int>[]) |
位交换解密,输入16进制字符串 |
类型转换相关
| 方法 | 说明 |
|---|---|
string.ToInt(int defaultValue) |
字符串转int,支持默认值 |
string.ToByteArr() |
16进制字符串转字节数组 |
byte[].ToHexStr() |
字节数组转带空格的16进制字符串 |
byte[].ToHexStrNoSplit() |
字节数组转无分隔的16进制字符串 |
byte.ToBoolArray(bool isBigEndian) |
byte转8位布尔数组 |
int.ToByteArray(bool isBigEndian) |
int32转4字节数组 |
byte.ToBit(int bitPosition) |
提取byte指定位置的位值 |
时间戳相关
| 方法 | 说明 |
|---|---|
DateTime.ToTimeStampAsDay() |
转天级时间戳(基准2025-01-01) |
int.ToDateTimeByDay() |
天级时间戳转DateTime |
DateTime.ToTimeStampAsMinute() |
转分钟级时间戳 |
DateTime.ToTimeStampAsMillisecond() |
转毫秒级时间戳 |
注意事项
- AES密钥要求:
- Key长度必须为16/24/32字节(对应AES-128/192/256)
- IV长度必须为16字节(CBC模式)
- 位交换索引:仅支持0-7的索引范围(对应字节的8个位)
- 时间戳基准:所有时间戳均基于2025-01-01 00:00:00,而非Unix纪元
- 异常处理:加密解密方法内置异常捕获,解密失败时返回空字符串,建议业务层增加结果校验
兼容性
- .NET Framework 4.5+
- .NET Core 2.0+
- .NET 5+
- .NET Standard 2.0+
许可证
本项目采用MIT许可证,详情请见LICENSE文件。
贡献
欢迎提交Issue和PR,贡献代码请遵循以下规范:
- 保持代码风格与现有代码一致
- 新增功能需提供对应的单元测试
- 完善相关注释和文档
问题反馈
如有使用问题或功能建议,请通过以下方式反馈:
- GitHub Issues: 提交问题
- 邮箱:your-email@example.com
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- System.Data.DataSetExtensions (>= 4.5.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.