SecurityLibrary
77784 ワード
using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace Miraclesoft.SecurityLibrary
{
///
/// (CSP) (System.Security.Cryptography.DES)
///
public static class DESCryp
{
#region
///
/// (MiracleSoft)
///
///
///
public static string Encrypt(string str) => Encrypt(str, "MiracleSoft");
///
///
///
///
///
///
public static string Encrypt(string str, string key)
{
var des = new DESCryptoServiceProvider
{
Key = Encoding.ASCII.GetBytes(GetMD5(key)),
IV = Encoding.ASCII.GetBytes(GetMD5(key))
};
var inputByteArray = Encoding.Default.GetBytes(str);
using (var ms = new MemoryStream())
{
using (var cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
var ret = new StringBuilder();
foreach (var b in ms.ToArray())
ret.AppendFormat($"{b:X2}");
return ret.ToString();
}
}
}
#endregion
#region
///
/// (MiracleSoft)
///
///
///
public static string Decrypt(string str) => Decrypt(str, "MiracleSoft");
///
///
///
///
///
///
public static string Decrypt(string str, string key)
{
var des = new DESCryptoServiceProvider
{
Key = Encoding.ASCII.GetBytes(GetMD5(key)),
IV = Encoding.ASCII.GetBytes(GetMD5(key))
};
var len = str.Length / 2;
var inputByteArray = new byte[len];
for (var i = 0; i < len; i++)
inputByteArray[i] = (byte)Convert.ToInt32(str.Substring(i * 2, 2), 16);
using (var ms = new MemoryStream())
{
using (var cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Encoding.Default.GetString(ms.ToArray());
}
}
}
#endregion
#region MD5
///
/// MD5
///
///
/// MD5
private static string GetMD5(string str)
{
MD5 md5 = new MD5CryptoServiceProvider();
#region ///MD5CryptoServiceProvider MSDN
//https://msdn.microsoft.com/zh-cn/library/system.security.cryptography.md5cryptoserviceprovider(v=vs.110).aspx
#endregion
var md5char = md5.ComputeHash(Encoding.Unicode.GetBytes(str));
var result = md5char.Aggregate<byte, string>(null, (current, t) => current + t.ToString("x2"));
return result.ToUpper();
}
#endregion
}
}
using System;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace Miraclesoft.SecurityLibrary
{
///
/// , .
///
public static class HashCryp
{
///
///
///
///
public static string Security => HashEncoding();
///
/// (10-64 )
///
/// ,true= ,
/// ,true= ,
/// ,true= ,
/// ,true= ,
///
///
private static string GetRandomString(bool useNum = true, bool useLow = true, bool useUpp = true,
bool useSpe = true, string custom = "")
{
var b = new byte[4];
new RNGCryptoServiceProvider().GetBytes(b);
var rad = new Random(BitConverter.ToInt32(b, 0));
var length = rad.Next(10, 64);
string result = null;
if (useNum)
custom += "0123456789";
if (useLow)
custom += "abcdefghijklmnopqrstuvwxyz";
if (useUpp)
custom += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if (useSpe)
custom += "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
for (var i = 0; i < length; i++)
result += custom.Substring(rad.Next(0, custom.Length - 1), 1);
return result;
}
///
///
///
///
public static string HashEncoding()
{
var sHA512 = new SHA512Managed();
var unicodeEncoding = new UnicodeEncoding();
var value = sHA512.ComputeHash(unicodeEncoding.GetBytes(GetRandomString()));
return value.Aggregate("", (current, o) => current + ((int)o + "O"));
}
}
}
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace Miraclesoft.SecurityLibrary
{
/**
* ##############################################################################
* RSA RSA
* KEY XML ,
* !! !!
* ##############################################################################
*/
/**
* .Net Framework RSA , , 11,
* , . , , , .
*
*/
///
/// RSA RSA
/// (CSP) System.Security.Cryptography.RSA
///
public class RSACryp
{
#region RSA
///
/// RSA
///
/// RSA XML ( )--
/// RSA XML ( )--
public static void RSAKey(out string XmlPrivateKey, out string XmlPublicKey)
{
var rsa = new RSACryptoServiceProvider();
XmlPrivateKey = rsa.ToXmlString(true);
XmlPublicKey = rsa.ToXmlString(false);
}
#endregion
#region RSA
///
/// RSA String( )
///
/// RSA XML ( )--
///
///
public static string RSAEncrypt(string XmlPublicKey, string Plaintext) =>
Convert.ToBase64String(RSAEncrypt(XmlPublicKey, new UnicodeEncoding().GetBytes(Plaintext)));
///
/// RSA byte[]( )
///
/// RSA XML ( )--
///
///
public static byte[] RSAEncrypt(string XmlPublicKey, byte[] Plaintext)
{
var rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(XmlPublicKey);
return rsa.Encrypt(Plaintext, false);
}
#endregion
#region RSA
///
/// RSA String( )
///
/// RSA XML ( )--
///
///
public static string RSADecrypt(string XmlPrivateKey, string Ciphertext) =>
new UnicodeEncoding().GetString(RSADecrypt(XmlPrivateKey, Convert.FromBase64String(Ciphertext)));
///
/// RSA byte[]( )
///
/// RSA XML ( )--
///
///
public static byte[] RSADecrypt(string XmlPrivateKey, byte[] Ciphertext)
{
var rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(XmlPrivateKey);
return rsa.Decrypt(Ciphertext, false);
}
#endregion
#region Hash
///
/// Hash
///
///
///
/// Hash
public static byte[] GetHashByte(string source)
{
if (string.IsNullOrEmpty(source))
throw new ArgumentException(" ", nameof(source));
return HashAlgorithm.Create("MD5").ComputeHash(Encoding.GetEncoding("GB2312").GetBytes(source));
}
///
/// Hash
///
///
/// Hash
public static string GetHashString(string source) => Convert.ToBase64String(GetHashByte(source));
///
/// Hash
///
///
///
/// Hash
public static byte[] GetFileHashByte(FileStream objFile)
{
if (objFile == null)
throw new ArgumentNullException(nameof(objFile));
var arry = HashAlgorithm.Create("MD5").ComputeHash(objFile);
objFile.Close();
return arry;
}
///
/// Hash
///
///
///
public static string GetFileHashString(FileStream objFile) =>
Convert.ToBase64String(GetFileHashByte(objFile));
#endregion
#region RSA
///
/// RSA
///
/// RSA XML ( )--
///
///
public static void SignatureFormatter(string XmlPrivateKey, byte[] HashbyteSignature, ref byte[] EncryptedSignatureByte)
{
var RSA = new RSACryptoServiceProvider();
RSA.FromXmlString(XmlPrivateKey);
var RSAFormatter = new RSAPKCS1SignatureFormatter(RSA);
// MD5
RSAFormatter.SetHashAlgorithm("MD5");
//
EncryptedSignatureByte = RSAFormatter.CreateSignature(HashbyteSignature);
}
///
/// RSA
///
/// RSA XML ( )--
///
///
public static void SignatureFormatter(string XmlPrivateKey, byte[] HashbyteSignature, ref string EncryptedSignatureString)
{
byte[] EncryptedSignatureData = null;
SignatureFormatter(XmlPrivateKey, HashbyteSignature, ref EncryptedSignatureData);
EncryptedSignatureString = Convert.ToBase64String(EncryptedSignatureData);
}
///
/// RSA
///
/// RSA XML ( )--
///
///
///
public static void SignatureFormatter(string XmlPrivateKey, string HashStringSignature, ref byte[] EncryptedSignatureByte) =>
SignatureFormatter(XmlPrivateKey, Convert.FromBase64String(HashStringSignature), ref EncryptedSignatureByte);
///
/// RSA
///
/// RSA XML ( )--
///
///
public static void SignatureFormatter(string XmlPrivateKey, string HashStringSignature, ref string EncryptedSignatureString) =>
SignatureFormatter(XmlPrivateKey, Convert.FromBase64String(HashStringSignature), ref EncryptedSignatureString);
#endregion
#region RSA
///
/// RSA
///
/// RSA XML ( )--
/// RSA
///
/// HashByteVerification SignatureByte , true; false.
public static bool SignatureVerification(string XmlPublicKey, byte[] HashByteVerification, byte[] SignatureByte)
{
var RSA = new RSACryptoServiceProvider();
RSA.FromXmlString(XmlPublicKey);
var RSADeformatter = new RSAPKCS1SignatureDeformatter(RSA);
// HASH MD5
RSADeformatter.SetHashAlgorithm("MD5");
return RSADeformatter.VerifySignature(HashByteVerification, SignatureByte);
}
///
/// RSA
///
/// RSA XML ( )--
/// RSA
///
/// HashStringVerification SignatureByte , true; false.
public static bool SignatureVerification(string XmlPublicKey, string HashStringVerification, byte[] SignatureByte) =>
SignatureVerification(XmlPublicKey, Convert.FromBase64String(HashStringVerification), SignatureByte);
///
/// RSA
///
/// RSA XML ( )--
/// RSA
///
/// HashByteVerification SignatureString , true; false.
public static bool SignatureVerification(string XmlPublicKey, byte[] HashByteVerification, string SignatureString) =>
SignatureVerification(XmlPublicKey, HashByteVerification, Convert.FromBase64String(SignatureString));
///
/// RSA
///
/// RSA XML ( )--
/// RSA
///
/// HashStringVerification SignatureString , true; false.
public static bool SignatureVerification(string XmlPublicKey, string HashStringVerification, string SignatureString) =>
SignatureVerification(XmlPublicKey, HashStringVerification, Convert.FromBase64String(SignatureString));
#endregion
#region
///
/// RSA
///
///
///
///
public static void RSAEncrypt(string XmlPublicKey, string Plaintext, ref string Ciphertext)
{
if (string.IsNullOrEmpty(Plaintext))
throw new Exception(" .");
if (string.IsNullOrWhiteSpace(XmlPublicKey))
throw new ArgumentException(" ");
using (var rsaProvider = new RSACryptoServiceProvider())
{
var inputBytes = Convert.FromBase64String(Plaintext); //
rsaProvider.FromXmlString(XmlPublicKey); //
var bufferSize = (rsaProvider.KeySize / 8) - 11; //
var buffer = new byte[bufferSize];
using (MemoryStream inputStream = new MemoryStream(inputBytes), outputStream = new MemoryStream())
{
while (true)
{
//
var readSize = inputStream.Read(buffer, 0, bufferSize);
if (readSize <= 0)
break;
var temp = new byte[readSize];
Array.Copy(buffer, 0, temp, 0, readSize);
var encryptedBytes = rsaProvider.Encrypt(temp, false);
outputStream.Write(encryptedBytes, 0, encryptedBytes.Length);
}
Ciphertext = Convert.ToBase64String(outputStream.ToArray()); //
}
}
}
///
/// RSA
///
///
///
///
public static void RSADecrypt(string XmlPrivateKey, string Ciphertext, ref string Plaintext)
{
if (string.IsNullOrEmpty(Ciphertext))
throw new Exception(" .");
if (string.IsNullOrWhiteSpace(XmlPrivateKey))
throw new ArgumentException(" ");
using (var rsaProvider = new RSACryptoServiceProvider())
{
var inputBytes = Convert.FromBase64String(Ciphertext);
rsaProvider.FromXmlString(XmlPrivateKey);
var bufferSize = rsaProvider.KeySize / 8;
var buffer = new byte[bufferSize];
using (MemoryStream inputStream = new MemoryStream(inputBytes), outputStream = new MemoryStream())
{
while (true)
{
var readSize = inputStream.Read(buffer, 0, bufferSize);
if (readSize <= 0)
break;
var temp = new byte[readSize];
Array.Copy(buffer, 0, temp, 0, readSize);
var rawBytes = rsaProvider.Decrypt(temp, false);
outputStream.Write(rawBytes, 0, rawBytes.Length);
}
Plaintext = new UnicodeEncoding().GetString((outputStream.ToArray()));
}
}
}
#endregion
}
}
using System;
using System.Security.Cryptography;
using System.Text;
namespace Miraclesoft.SecurityLibrary
{
///
/// (CSP) System.Security.Cryptography.TripleDES
///
public static class TripleDESCryp
{
#region / String
///
/// (yuwan.net) String
///
///
///
public static string Encrypt(string original) => Encrypt(original, "yuwan.net");
///
/// (yuwan.net) String
///
///
///
public static string Decrypt(string original) => Decrypt(original, "yuwan.net", Encoding.Default);
#endregion
#region / String
///
/// String
///
///
///
///
public static string Encrypt(string original, string key) =>
Convert.ToBase64String(Encrypt(Encoding.Default.GetBytes(original), Encoding.Default.GetBytes(key)));
///
/// string
///
///
///
///
public static string Decrypt(string original, string key) => Decrypt(original, key, Encoding.Default);
///
/// string,
///
///
///
///
///
public static string Decrypt(string encrypted, string key, Encoding encoding) =>
encoding.GetString(Decrypt(Convert.FromBase64String(encrypted), Encoding.Default.GetBytes(key)));
#endregion
#region / /byte[]
///
/// (MiracleSoft) Byte[]
///
/// Byte[]
///
public static byte[] Decrypt(byte[] encrypted) =>
Decrypt(encrypted, Encoding.Default.GetBytes("MiracleSoft"));
///
/// (MiracleSoft)
///
///
///
public static byte[] Encrypt(byte[] original) =>
Encrypt(original, Encoding.Default.GetBytes("MiracleSoft"));
#endregion
#region / /byte[]
///
/// MD5
///
///
/// MD5
public static byte[] MakeMD5(byte[] original) => new MD5CryptoServiceProvider().ComputeHash(original);
///
///
///
///
///
///
public static byte[] Encrypt(byte[] original, byte[] key)
{
var des = new TripleDESCryptoServiceProvider
{
Key = MakeMD5(key),
Mode = CipherMode.ECB
};
return des.CreateEncryptor().TransformFinalBlock(original, 0, original.Length);
}
///
///
///
///
///
///
public static byte[] Decrypt(byte[] encrypted, byte[] key)
{
var des = new TripleDESCryptoServiceProvider
{
Key = MakeMD5(key),
Mode = CipherMode.ECB
};
return des.CreateDecryptor().TransformFinalBlock(encrypted, 0, encrypted.Length);
}
#endregion
}
}