migrate to new git
This commit is contained in:
132
web/App_Code/encrypt.cs
Normal file
132
web/App_Code/encrypt.cs
Normal file
@@ -0,0 +1,132 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Web;
|
||||
using System.Web.Security;
|
||||
|
||||
/// <summary>
|
||||
/// 加密相關
|
||||
/// </summary>
|
||||
///
|
||||
|
||||
namespace MyWeb
|
||||
{
|
||||
public class encrypt
|
||||
{
|
||||
public string MD5(string str, int bits = 32) //MD5加密
|
||||
{
|
||||
if (bits == 16)
|
||||
{
|
||||
return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower().Substring(8, 16);
|
||||
}
|
||||
else
|
||||
{
|
||||
return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower();
|
||||
}
|
||||
}
|
||||
|
||||
public string SHA1(string SourceStr) //SHA1加密
|
||||
{
|
||||
return FormsAuthentication.HashPasswordForStoringInConfigFile(SourceStr, "SHA1");
|
||||
}
|
||||
|
||||
private byte[] Keys = { 0xEF, 0xAB, 0x56, 0x78, 0x90, 0x34, 0xCD, 0x12 };
|
||||
|
||||
public string EncryptDes(string SourceStr, string skey) //使用標準DES對稱加密, skey請帶入8位數自訂KEY
|
||||
{
|
||||
try
|
||||
{
|
||||
byte[] rgbKey = Encoding.UTF8.GetBytes(skey.Substring(0, 8));
|
||||
byte[] rgbIV = Keys;
|
||||
byte[] inputByteArray = Encoding.UTF8.GetBytes(SourceStr);
|
||||
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
|
||||
MemoryStream mStream = new MemoryStream();
|
||||
CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
|
||||
cStream.Write(inputByteArray, 0, inputByteArray.Length);
|
||||
cStream.FlushFinalBlock();
|
||||
string str = Convert.ToBase64String(mStream.ToArray());
|
||||
return str;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return SourceStr;
|
||||
}
|
||||
}
|
||||
|
||||
public string DecryptDes(string SourceStr, string skey) //使用標準DES對稱解密, skey請帶入8位數自訂KEY
|
||||
{
|
||||
try
|
||||
{
|
||||
byte[] rgbKey = Encoding.UTF8.GetBytes(skey.Substring(0, 8));
|
||||
byte[] rgbIV = Keys;
|
||||
byte[] inputByteArray = Convert.FromBase64String(SourceStr);
|
||||
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
|
||||
MemoryStream mStream = new MemoryStream();
|
||||
CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
|
||||
cStream.Write(inputByteArray, 0, inputByteArray.Length);
|
||||
cStream.FlushFinalBlock();
|
||||
return Encoding.UTF8.GetString(mStream.ToArray());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return SourceStr;
|
||||
}
|
||||
}
|
||||
|
||||
public string EncryptAutoKey(string str)
|
||||
{
|
||||
function f = new function();
|
||||
if (!f.isStrNull(str))
|
||||
{
|
||||
string key = f.randKey(8);
|
||||
string encode = EncryptDes(str, key);
|
||||
str = f.randKey(3) + f.Left(key, 5) + f.Left(encode, encode.Length - 2) + f.Right(key, 3) + f.Right(encode, 2);
|
||||
return str;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
public string DecryptAutoKey(string str)
|
||||
{
|
||||
try
|
||||
{
|
||||
function f = new function();
|
||||
if (!string.IsNullOrEmpty(str))
|
||||
{
|
||||
string str2 = str?.Substring(3, (str ?? "").Length - 3);
|
||||
string key = f.Left(str2, 5) + f.Left(f.Right(str2, 5), 3);
|
||||
str2 = str2.Substring(5, str2.Length - 10) + f.Right(str2, 2);
|
||||
return DecryptDes(str2, key);
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
public string SHA256(string str)
|
||||
{
|
||||
using (SHA256 hash = SHA256Managed.Create())
|
||||
{
|
||||
return string.Concat(hash
|
||||
.ComputeHash(Encoding.UTF8.GetBytes(str))
|
||||
.Select(item => item.ToString("x2")));
|
||||
}
|
||||
}
|
||||
|
||||
public string followerHash(string s1, string s2)
|
||||
{
|
||||
string input = s1 + s2;
|
||||
input = new string(input.Where(c =>
|
||||
char.IsLetterOrDigit(c)).ToArray()).ToUpper();
|
||||
return SHA256(input);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user