mirror of
https://github.com/rudollee/Encryption.git
synced 2025-06-07 07:46:09 +00:00
Encryption
This commit is contained in:
parent
bc5e67e7c3
commit
6f92d292e4
@ -1,12 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Encryption
|
||||
{
|
||||
public class Class1
|
||||
{
|
||||
}
|
||||
}
|
67
Encryption/Crypto.cs
Normal file
67
Encryption/Crypto.cs
Normal file
@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace Encryption
|
||||
{
|
||||
/// <summary>
|
||||
/// reference: https://msdn.microsoft.com/en-gb/library/system.security.cryptography.tripledescryptoserviceprovider(v=vs.110).aspx
|
||||
/// </summary>
|
||||
public class Crypto
|
||||
{
|
||||
public List<byte[]> GenerateKeyAndIV()
|
||||
{
|
||||
TripleDESCryptoServiceProvider tDES = new TripleDESCryptoServiceProvider();
|
||||
|
||||
return new List<byte[]> { tDES.Key, tDES.IV };
|
||||
}
|
||||
|
||||
public string Encrypt(string s, byte[] key, byte[] iv)
|
||||
{
|
||||
try
|
||||
{
|
||||
MemoryStream mStream = new MemoryStream();
|
||||
CryptoStream cStream = new CryptoStream(mStream, new TripleDESCryptoServiceProvider().CreateEncryptor(key, iv), CryptoStreamMode.Write);
|
||||
byte[] toEncrypt = new UTF8Encoding().GetBytes(s);
|
||||
|
||||
cStream.Write(toEncrypt, 0, toEncrypt.Length);
|
||||
cStream.FlushFinalBlock();
|
||||
|
||||
byte[] bytesEncrypted = mStream.ToArray();
|
||||
|
||||
cStream.Close();
|
||||
mStream.Close();
|
||||
|
||||
return Convert.ToBase64String(bytesEncrypted, 0, bytesEncrypted.Length);
|
||||
}
|
||||
catch (CryptographicException e)
|
||||
{
|
||||
Console.WriteLine("Error: {0}", e.Message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public string Decrypt(string s, byte[] key, byte[] iv)
|
||||
{
|
||||
try
|
||||
{
|
||||
byte[] bytesEncrypted = Convert.FromBase64String(s);
|
||||
MemoryStream mStream = new MemoryStream(bytesEncrypted);
|
||||
CryptoStream cStream = new CryptoStream(mStream, new TripleDESCryptoServiceProvider().CreateDecryptor(key, iv), CryptoStreamMode.Read);
|
||||
byte[] fromEncrypt = new byte[s.Length];
|
||||
|
||||
cStream.Read(fromEncrypt, 0, fromEncrypt.Length);
|
||||
|
||||
return new UTF8Encoding().GetString(fromEncrypt);
|
||||
}
|
||||
catch (CryptographicException e)
|
||||
{
|
||||
Console.WriteLine("Error: {0}", e.Message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -1,10 +1,10 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>ed031a70-570f-4599-ac04-0453a997a6a4</ProjectGuid>
|
||||
<ProjectGuid>{ED031A70-570F-4599-AC04-0453A997A6A4}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>Encryption</RootNamespace>
|
||||
@ -30,24 +30,13 @@
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System"/>
|
||||
|
||||
<Reference Include="System.Core"/>
|
||||
<Reference Include="System.Xml.Linq"/>
|
||||
<Reference Include="System.Data.DataSetExtensions"/>
|
||||
|
||||
|
||||
<Reference Include="Microsoft.CSharp"/>
|
||||
|
||||
<Reference Include="System.Data"/>
|
||||
|
||||
<Reference Include="System.Net.Http"/>
|
||||
|
||||
<Reference Include="System.Xml"/>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Class1.cs" />
|
||||
<Compile Include="Crypto.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
</Project>
|
Loading…
x
Reference in New Issue
Block a user