2025-09-12 18:34:33 +08:00
using Microsoft.VisualBasic ;
using NPOI.SS.UserModel ;
using NPOI.XSSF.UserModel ;
2025-08-29 14:57:55 +08:00
using System ;
using System.Collections.Generic ;
using System.Diagnostics ;
2025-09-03 16:16:00 +08:00
using System.IO ;
2025-08-29 14:57:55 +08:00
using System.Linq ;
2025-09-12 18:34:33 +08:00
using System.Security.Cryptography ;
2025-08-29 14:57:55 +08:00
using System.Text ;
using System.Threading.Tasks ;
using System.Windows ;
using System.Windows.Controls ;
using System.Windows.Media ;
2025-09-12 18:34:33 +08:00
using System.Windows.Media.Imaging ;
2025-08-29 14:57:55 +08:00
using System.Windows.Threading ;
namespace Ramitta.lib
{
public static class Basic
{
#region ȫ <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ɫ
public static SolidColorBrush <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ɫ = new SolidColorBrush ( Color . FromRgb ( 92 , 40 , 147 ) ) ;
public static SolidColorBrush <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ɫ = new SolidColorBrush ( Color . FromRgb ( 134 , 27 , 45 ) ) ;
public static SolidColorBrush <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ɫ = new SolidColorBrush ( Color . FromRgb ( 202 , 81 , 0 ) ) ;
public static SolidColorBrush <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ɫ = new SolidColorBrush ( Color . FromRgb ( 66 , 164 , 60 ) ) ;
public static SolidColorBrush ͨ ֪ <EFBFBD> <EFBFBD> ɫ = new SolidColorBrush ( Color . FromRgb ( 0 , 122 , 204 ) ) ;
public static SolidColorBrush <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ɫ = new SolidColorBrush ( Color . FromRgb ( 30 , 30 , 30 ) ) ;
public static SolidColorBrush Ĭ <EFBFBD> ϻ <EFBFBD> ɫ = new SolidColorBrush ( Color . FromRgb ( 66 , 66 , 66 ) ) ;
#endregion
#region <EFBFBD> <EFBFBD> <EFBFBD> Ժ <EFBFBD> <EFBFBD> <EFBFBD>
2025-09-12 18:34:33 +08:00
public static void DebugBar ( Label obj , String ? text , SolidColorBrush ? color = null )
2025-08-29 14:57:55 +08:00
{
if ( obj = = null ) return ;
// ͨ<> <CDA8> Dispatcherȷ<72> <C8B7> <EFBFBD> <EFBFBD> UI<55> ߳<EFBFBD> ִ<EFBFBD> <D6B4>
obj . Dispatcher . Invoke ( ( ) = >
{
2025-09-12 18:34:33 +08:00
if ( text ! = null ) obj . Content = text ;
if ( color ! = null ) obj . Background = color ;
2025-10-15 18:53:43 +08:00
// ǿ<> <C7BF> ˢ<EFBFBD> <CBA2> UI
obj . InvalidateVisual ( ) ;
obj . InvalidateArrange ( ) ;
obj . InvalidateMeasure ( ) ;
obj . UpdateLayout ( ) ;
// <20> <> <EFBFBD> <EFBFBD> Dispatcher<65> <72> <EFBFBD> <EFBFBD> <EFBFBD> е <EFBFBD> <D0B5> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> IJ<EFBFBD> <C4B2> <EFBFBD> <EFBFBD> <EFBFBD> ȷ<EFBFBD> <C8B7> UI<55> <49> <EFBFBD> <EFBFBD>
Dispatcher . CurrentDispatcher . Invoke ( DispatcherPriority . Background , new Action ( delegate { } ) ) ;
2025-08-29 14:57:55 +08:00
} ) ;
}
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> Զ<EFBFBD> <D4B6> <EFBFBD> <EFBFBD> 쳣<EFBFBD> <ECB3A3>
public class DebugbarException : Exception
{
public string Message { get ; }
public SolidColorBrush Brush { get ; }
public DebugbarException ( string message , SolidColorBrush brush )
: base ( message )
{
Message = message ;
Brush = brush ;
}
}
#endregion
#region <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
public static Dictionary < string , string > ? Startupe ;
public static Dictionary < string , string > ParseCommandLineArgs ( string [ ] args )
{
var arguments = new Dictionary < string , string > ( StringComparer . OrdinalIgnoreCase ) ;
for ( int i = 0 ; i < args . Length ; i + + )
{
string arg = args [ i ] ;
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> Ƿ<EFBFBD> <C7B7> Dz<EFBFBD> <C7B2> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> -<2D> <> /<2F> <> ͷ<EFBFBD> <CDB7>
if ( arg . StartsWith ( "-" ) | | arg . StartsWith ( "/" ) )
{
string key = arg . TrimStart ( '-' , '/' ) ;
string value = "true" ; // Ĭ<> <C4AC> ֵΪtrue<75> <65> <EFBFBD> <EFBFBD> ʾ <EFBFBD> <CABE> <EFBFBD> ش<EFBFBD> <D8B4> <EFBFBD>
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> һ <EFBFBD> <D2BB> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ƿ<EFBFBD> <C7B7> <EFBFBD> ֵ<EFBFBD> <D6B5> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> -<2D> <> /<2F> <> ͷ<EFBFBD> <CDB7>
if ( i + 1 < args . Length & & ! args [ i + 1 ] . StartsWith ( "-" ) & & ! args [ i + 1 ] . StartsWith ( "/" ) )
{
value = args [ i + 1 ] ;
i + + ; // <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> һ <EFBFBD> <D2BB> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ϊ<EFBFBD> Ѿ<EFBFBD> <D1BE> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ֵ
}
arguments [ key ] = value ;
}
}
return arguments ;
}
#endregion
2025-09-12 18:34:33 +08:00
#region <EFBFBD> ļ <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
public static bool IsPathExist ( string databasePath )
2025-09-03 16:16:00 +08:00
{
2025-09-12 18:34:33 +08:00
if ( string . IsNullOrWhiteSpace ( databasePath ) )
{
return false ;
}
return Directory . Exists ( databasePath ) ;
}
public static void FileWrite ( string outputPath , string outputText , bool createDirectories = true , bool fileAdd = false )
{
string directory = Path . GetDirectoryName ( outputPath ) ;
if ( createDirectories & & ! Directory . Exists ( directory ) )
{
Directory . CreateDirectory ( directory ) ;
}
// ʹ <> <CAB9> StreamWriter д<> <D0B4> <EFBFBD> ļ<EFBFBD>
2025-09-03 16:16:00 +08:00
using ( StreamWriter writer = new StreamWriter ( outputPath , false ) )
{
Debug . WriteLine ( outputText ) ;
writer . WriteLine ( outputText ) ;
}
}
public static string FileRead ( string outputPath )
{
return File . ReadAllText ( outputPath ) ;
}
2025-09-12 18:34:33 +08:00
// <20> <> ȡ·<C8A1> <C2B7> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ļ<EFBFBD> <C4BC> <EFBFBD> <EFBFBD> ļ<EFBFBD> <C4BC> У <EFBFBD> ֧<EFBFBD> ִ<EFBFBD> <D6B4> <EFBFBD> <EFBFBD> ж<EFBFBD> <D0B6> ļ<EFBFBD> <C4BC> ĺ<EFBFBD> <C4BA> <EFBFBD>
public static List < string > GetFilesAndDirectories ( string path , bool? isDirectory = null , Func < string , bool > fileFilter = null , Func < string , string > outputFormat = null )
{
List < string > result = new List < string > ( ) ;
// <20> <> ȡ<EFBFBD> ļ<EFBFBD> <C4BC> <EFBFBD>
var directories = Directory . GetDirectories ( path ) ;
foreach ( var dir in directories )
{
if ( isDirectory = = null | | isDirectory = = true )
{
// <20> <> <EFBFBD> <EFBFBD> ָ<EFBFBD> <D6B8> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ʽ <EFBFBD> <CABD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ʹ <EFBFBD> ø<EFBFBD> ʽ <EFBFBD> <CABD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ʹ <EFBFBD> <CAB9> ԭʼ ·<CABC> <C2B7>
result . Add ( outputFormat = = null ? dir : outputFormat ( dir ) ) ;
}
}
// <20> <> ȡ<EFBFBD> ļ<EFBFBD>
var files = Directory . GetFiles ( path ) ;
foreach ( var file in files )
{
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> fileFilter <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> øú<C3B8> <C3BA> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ɸѡ
if ( fileFilter = = null | | fileFilter ( file ) )
{
if ( isDirectory = = null | | isDirectory = = false )
{
// <20> <> <EFBFBD> <EFBFBD> ָ<EFBFBD> <D6B8> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ʽ <EFBFBD> <CABD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ʹ <EFBFBD> ø<EFBFBD> ʽ <EFBFBD> <CABD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ʹ <EFBFBD> <CAB9> ԭʼ ·<CABC> <C2B7>
result . Add ( outputFormat = = null ? file : outputFormat ( file ) ) ;
}
}
}
return result ;
}
// <20> <> <EFBFBD> ɽ<EFBFBD> ͼ
public static PngBitmapEncoder CaptureWindowScreenshot ( Window window , string? filePath = null )
{
// <20> <> ȡ<EFBFBD> <C8A1> ǰ<EFBFBD> <C7B0> <EFBFBD> ڵ<EFBFBD> DPI <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
var dpiScale = VisualTreeHelper . GetDpi ( window ) ;
// <20> <> ȡ DPI <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ӵ<EFBFBD> X <20> <> Y ֵ<> <D6B5> ͨ<EFBFBD> <CDA8> X <20> <> Y Ӧ<> <D3A6> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ȵ<EFBFBD>
double dpiX = dpiScale . DpiScaleX ;
double dpiY = dpiScale . DpiScaleY ;
// <20> <> <EFBFBD> <EFBFBD> DPI <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ӵ<EFBFBD> <D3B5> <EFBFBD> <EFBFBD> <EFBFBD> ͼ<EFBFBD> ijߴ <C4B3>
int width = ( int ) ( window . ActualWidth * dpiX ) ; // ʹ <> <CAB9> dpiX
int height = ( int ) ( window . ActualHeight * dpiY ) ; // ʹ <> <CAB9> dpiY
// <20> <> <EFBFBD> <EFBFBD> һ <EFBFBD> <D2BB> RenderTargetBitmap <20> <> <EFBFBD> <EFBFBD> <F3A3ACB2> ҿ<EFBFBD> <D2BF> <EFBFBD> DPI <20> <> <EFBFBD> <EFBFBD>
RenderTargetBitmap rtb = new RenderTargetBitmap ( width , height , 96 * dpiX , 96 * dpiY , System . Windows . Media . PixelFormats . Pbgra32 ) ;
// ʹ <> <CAB9> VisualTreeHelper <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ⱦ
rtb . Render ( window ) ;
// <20> <> <EFBFBD> <EFBFBD> һ <EFBFBD> <D2BB> PngBitmapEncoder <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ϊ PNG <20> <> ʽ
PngBitmapEncoder pngEncoder = new PngBitmapEncoder ( ) ;
pngEncoder . Frames . Add ( BitmapFrame . Create ( rtb ) ) ;
if ( ! string . IsNullOrEmpty ( filePath ) )
{
// <20> <> <EFBFBD> <EFBFBD> ͼ<EFBFBD> <CDBC> <EFBFBD> <EFBFBD> <EFBFBD> ļ<EFBFBD>
using ( FileStream fs = new FileStream ( filePath , FileMode . Create ) )
{
pngEncoder . Save ( fs ) ;
}
}
return pngEncoder ;
}
#endregion
#region <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
2025-09-02 18:41:07 +08:00
public static async Task < int > RunExternalCommand ( string? applicationPath = "explorer.exe" , string arguments = "" , bool UseShellExecute = false , bool CreateNoWindow = false )
2025-08-29 14:57:55 +08:00
{
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = applicationPath ,
Arguments = arguments ,
UseShellExecute = UseShellExecute ,
CreateNoWindow = CreateNoWindow
} ;
try
{
Process process = Process . Start ( startInfo ) ? ?
throw new InvalidOperationException ( "Failed to start process." ) ;
if ( UseShellExecute ! = true )
{
await Task . Run ( ( ) = > process . WaitForExit ( ) ) ;
return process . ExitCode ; // <20> <> <EFBFBD> ؽ<EFBFBD> <D8BD> ̵<EFBFBD> <CCB5> ˳<EFBFBD> <CBB3> <EFBFBD> <EFBFBD> <EFBFBD>
}
else
{
return 0 ;
}
}
catch ( Exception ex )
{
throw ;
}
}
public static async Task < string > RunExternalCommandResult ( string applicationPath , string arguments , bool CreateNoWindow = true )
{
// <20> <> <EFBFBD> <EFBFBD> һ <EFBFBD> <D2BB> <EFBFBD> µĽ<C2B5> <C4BD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ϣ
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = applicationPath ,
Arguments = arguments ,
UseShellExecute = false , // <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ϊ false <20> Ա<EFBFBD> <D4B1> ܹ<EFBFBD> <DCB9> ض<EFBFBD> <D8B6> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
RedirectStandardOutput = true , // <20> ض<EFBFBD> <D8B6> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <D7BC> <EFBFBD> <EFBFBD>
RedirectStandardError = true , // <20> <> ѡ <EFBFBD> <D1A1> Ҳ<EFBFBD> ض<EFBFBD> <D8B6> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <D7BC> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
CreateNoWindow = CreateNoWindow // <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
} ;
try
{
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
using ( Process process = new Process ( ) )
{
process . StartInfo = startInfo ;
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
process . Start ( ) ;
// <20> <> ȡ<EFBFBD> <C8A1> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
string output = await process . StandardOutput . ReadToEndAsync ( ) ;
string error = await process . StandardError . ReadToEndAsync ( ) ; // <20> <> ȡ<EFBFBD> <C8A1> <EFBFBD> <D7BC> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
// <20> ȴ<EFBFBD> <C8B4> <EFBFBD> <EFBFBD> ̽<EFBFBD> <CCBD> <EFBFBD>
await process . WaitForExitAsync ( ) ;
/ *
if ( process . ExitCode ! = 0 )
{
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ˳<EFBFBD> <CBB3> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
throw new Exception ( $"Process exited with code {process.ExitCode}: {error}" ) ;
}
* /
return output ; // <20> <> <EFBFBD> ر<EFBFBD> <EFBFBD> <D7BC> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
}
}
catch ( Exception ex )
{
// <20> <> <EFBFBD> Լ<EFBFBD> ¼<EFBFBD> <C2BC> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> 쳣
throw ; // <20> <> <EFBFBD> <EFBFBD> <EFBFBD> ׳ <EFBFBD> <D7B3> 쳣<EFBFBD> Ա<EFBFBD> <D4B1> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ߲<EFBFBD> <DFB2> <EFBFBD>
}
}
2025-09-12 18:34:33 +08:00
#endregion
2025-08-29 14:57:55 +08:00
2025-09-12 18:34:33 +08:00
// <20> <> <EFBFBD> ɹ<EFBFBD> ϣ
public static string GenerateHash ( string input )
{
// <20> <> <EFBFBD> <EFBFBD> SHA256<35> <36> <EFBFBD> <EFBFBD>
using ( SHA256 sha256 = SHA256 . Create ( ) )
{
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ַ<EFBFBD> <D6B7> <EFBFBD> ת<EFBFBD> <D7AA> Ϊ<EFBFBD> ֽ<EFBFBD> <D6BD> <EFBFBD> <EFBFBD> 鲢<EFBFBD> <E9B2A2> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ϣֵ
byte [ ] bytes = sha256 . ComputeHash ( Encoding . UTF8 . GetBytes ( input ) ) ;
2025-08-29 14:57:55 +08:00
2025-09-12 18:34:33 +08:00
// <20> <> <EFBFBD> <EFBFBD> ϣֵת<D6B5> <D7AA> Ϊʮ<CEAA> <CAAE> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ַ<EFBFBD> <D6B7> <EFBFBD>
StringBuilder stringBuilder = new StringBuilder ( ) ;
foreach ( byte b in bytes )
{
stringBuilder . Append ( b . ToString ( "x2" ) ) ;
}
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> յĹ<D5B5> ϣ<EFBFBD> ַ<EFBFBD> <D6B7> <EFBFBD>
return stringBuilder . ToString ( ) ;
}
}
2025-08-29 14:57:55 +08:00
}
2025-09-12 18:34:33 +08:00
public class CryptoHelper
{
/// <summary>
/// <20> <> <EFBFBD> <EFBFBD> RSA<53> <41> Կ<EFBFBD> <D4BF>
/// </summary>
/// <param name="keySize"><3E> <> Կ<EFBFBD> <D4BF> <EFBFBD> ȣ <EFBFBD> ͨ<EFBFBD> <CDA8> Ϊ2048<34> <38> 4096<39> <36> </param>
/// <returns><3E> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Կ<EFBFBD> <D4BF> ˽Կ<CBBD> <D4BF> Ԫ<EFBFBD> <D4AA> </returns>
public static ( string publicKey , string privateKey ) GenerateKeyPair ( int keySize = 2048 )
{
try
{
using ( var rsa = RSA . Create ( keySize ) )
{
string publicKey = rsa . ToXmlString ( false ) ;
string privateKey = rsa . ToXmlString ( true ) ;
return ( publicKey , privateKey ) ;
}
}
catch ( Exception ex )
{
MessageBox . Show ( $"<22> <> Կ<EFBFBD> <D4BF> <EFBFBD> <EFBFBD> ʧ<EFBFBD> <CAA7> : {ex.Message}" ) ;
return ( null , null ) ;
}
}
/// <summary>
/// ʹ <> <CAB9> ˽Կ<CBBD> <D4BF> <EFBFBD> <EFBFBD> <EFBFBD> ݽ<EFBFBD> <DDBD> <EFBFBD> ǩ<EFBFBD> <C7A9>
/// </summary>
public static string? SignData ( string data , string privateKey )
{
try
{
using ( var rsa = RSA . Create ( ) )
{
rsa . FromXmlString ( privateKey ) ;
byte [ ] dataBytes = Encoding . UTF8 . GetBytes ( data ) ;
byte [ ] signature = rsa . SignData ( dataBytes , HashAlgorithmName . SHA256 , RSASignaturePadding . Pkcs1 ) ;
return Convert . ToBase64String ( signature ) ;
}
}
catch ( Exception ex )
{
return null ;
}
}
/// <summary>
/// ʹ <> ù<EFBFBD> Կ<EFBFBD> <D4BF> ֤ǩ<D6A4> <C7A9>
/// </summary>
public static bool? VerifySignature ( string data , string signature , string publicKey )
{
try
{
using ( var rsa = RSA . Create ( ) )
{
rsa . FromXmlString ( publicKey ) ;
byte [ ] dataBytes = Encoding . UTF8 . GetBytes ( data ) ;
byte [ ] signatureBytes = Convert . FromBase64String ( signature ) ;
return rsa . VerifyData ( dataBytes , signatureBytes , HashAlgorithmName . SHA256 , RSASignaturePadding . Pkcs1 ) ;
}
}
catch ( Exception ex )
{
return false ;
}
}
/// <summary>
/// ʹ <> ù<EFBFBD> Կ<EFBFBD> <D4BF> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
/// </summary>
public static string? Encrypt ( string data , string publicKey )
{
try
{
using ( var rsa = RSA . Create ( ) )
{
rsa . FromXmlString ( publicKey ) ;
byte [ ] dataBytes = Encoding . UTF8 . GetBytes ( data ) ;
byte [ ] encryptedBytes = rsa . Encrypt ( dataBytes , RSAEncryptionPadding . OaepSHA256 ) ;
return Convert . ToBase64String ( encryptedBytes ) ;
}
}
catch ( Exception ex )
{
return null ;
}
}
/// <summary>
/// ʹ <> <CAB9> ˽Կ<CBBD> <D4BF> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
/// </summary>
public static string? Decrypt ( string encryptedData , string privateKey )
{
try
{
using ( var rsa = RSA . Create ( ) )
{
rsa . FromXmlString ( privateKey ) ;
byte [ ] encryptedBytes = Convert . FromBase64String ( encryptedData ) ;
byte [ ] decryptedBytes = rsa . Decrypt ( encryptedBytes , RSAEncryptionPadding . OaepSHA256 ) ;
return Encoding . UTF8 . GetString ( decryptedBytes ) ;
}
}
catch ( Exception ex )
{
return null ;
}
}
}
2025-08-29 14:57:55 +08:00
}