code optimization
This commit is contained in:
@@ -1,80 +1,91 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
/* Nicht gemergte Änderung aus Projekt "Pilz.Cryptography (net6.0)"
|
||||
Vor:
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Security.Cryptography;
|
||||
using System.IO;
|
||||
Nach:
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
*/
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace Pilz.Cryptography
|
||||
namespace Pilz.Cryptography;
|
||||
|
||||
public class SimpleStringCrypter : ICrypter
|
||||
{
|
||||
public class SimpleStringCrypter : ICrypter
|
||||
private readonly TripleDES TripleDes;
|
||||
public Encoding TextEncoding { get; private set; } = Encoding.Default;
|
||||
|
||||
public SimpleStringCrypter() : this(string.Empty)
|
||||
{
|
||||
private readonly TripleDES TripleDes;
|
||||
public Encoding TextEncoding { get; private set; } = Encoding.Default;
|
||||
}
|
||||
|
||||
public SimpleStringCrypter() : this(string.Empty)
|
||||
{
|
||||
}
|
||||
public SimpleStringCrypter(string key) : this(key, Encoding.Default)
|
||||
{
|
||||
}
|
||||
|
||||
public SimpleStringCrypter(string key) : this(key, Encoding.Default)
|
||||
{
|
||||
}
|
||||
public SimpleStringCrypter(string key, Encoding textEncoding)
|
||||
{
|
||||
TextEncoding = textEncoding;
|
||||
TripleDes = TripleDES.Create();
|
||||
TripleDes.Key = TruncateHash(key, TripleDes.KeySize / 8);
|
||||
TripleDes.IV = TruncateHash(string.Empty, TripleDes.BlockSize / 8);
|
||||
}
|
||||
|
||||
public SimpleStringCrypter(string key, Encoding textEncoding)
|
||||
{
|
||||
TextEncoding = textEncoding;
|
||||
TripleDes = TripleDES.Create();
|
||||
TripleDes.Key = TruncateHash(key,TripleDes.KeySize / 8);
|
||||
TripleDes.IV = TruncateHash(string.Empty, TripleDes.BlockSize / 8);
|
||||
}
|
||||
private byte[] TruncateHash(string key, int length)
|
||||
{
|
||||
SHA1 sha1CryptoServiceProvider = SHA1.Create();
|
||||
var bytes = TextEncoding.GetBytes(key);
|
||||
var array = sha1CryptoServiceProvider.ComputeHash(bytes);
|
||||
|
||||
private byte[] TruncateHash(string key, int length)
|
||||
{
|
||||
SHA1 sha1CryptoServiceProvider = SHA1.Create();
|
||||
var bytes = TextEncoding.GetBytes(key);
|
||||
var array = sha1CryptoServiceProvider.ComputeHash(bytes);
|
||||
var output = new byte[length];
|
||||
var lowerLength = Math.Min(array.Length, output.Length);
|
||||
|
||||
var output = new byte[length];
|
||||
var lowerLength = Math.Min(array.Length, output.Length);
|
||||
|
||||
for (int i = 0; i < lowerLength; i++)
|
||||
output[i] = array[i];
|
||||
for (int i = 0; i < lowerLength; i++)
|
||||
output[i] = array[i];
|
||||
|
||||
return output;
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
private string EncryptData(string plaintext)
|
||||
{
|
||||
var bytes = TextEncoding.GetBytes(plaintext);
|
||||
using var memoryStream = new MemoryStream();
|
||||
using var cryptoStream = new CryptoStream(memoryStream, TripleDes.CreateEncryptor(), CryptoStreamMode.Write);
|
||||
cryptoStream.Write(bytes, 0, bytes.Length);
|
||||
cryptoStream.FlushFinalBlock();
|
||||
return Convert.ToBase64String(memoryStream.ToArray());
|
||||
}
|
||||
private string EncryptData(string plaintext)
|
||||
{
|
||||
var bytes = TextEncoding.GetBytes(plaintext);
|
||||
using var memoryStream = new MemoryStream();
|
||||
using var cryptoStream = new CryptoStream(memoryStream, TripleDes.CreateEncryptor(), CryptoStreamMode.Write);
|
||||
cryptoStream.Write(bytes, 0, bytes.Length);
|
||||
cryptoStream.FlushFinalBlock();
|
||||
return Convert.ToBase64String(memoryStream.ToArray());
|
||||
}
|
||||
|
||||
private string DecryptData(string encryptedtext)
|
||||
{
|
||||
var array = Convert.FromBase64String(encryptedtext);
|
||||
using var memoryStream = new MemoryStream();
|
||||
using var cryptoStream = new CryptoStream(memoryStream, TripleDes.CreateDecryptor(), CryptoStreamMode.Write);
|
||||
cryptoStream.Write(array, 0, array.Length);
|
||||
cryptoStream.FlushFinalBlock();
|
||||
return TextEncoding.GetString(memoryStream.ToArray());
|
||||
}
|
||||
private string DecryptData(string encryptedtext)
|
||||
{
|
||||
var array = Convert.FromBase64String(encryptedtext);
|
||||
using var memoryStream = new MemoryStream();
|
||||
using var cryptoStream = new CryptoStream(memoryStream, TripleDes.CreateDecryptor(), CryptoStreamMode.Write);
|
||||
cryptoStream.Write(array, 0, array.Length);
|
||||
cryptoStream.FlushFinalBlock();
|
||||
return TextEncoding.GetString(memoryStream.ToArray());
|
||||
}
|
||||
|
||||
public string Encrypt(string plainValue)
|
||||
{
|
||||
return EncryptData(plainValue ?? string.Empty);
|
||||
}
|
||||
public string Encrypt(string plainValue)
|
||||
{
|
||||
return EncryptData(plainValue ?? string.Empty);
|
||||
}
|
||||
|
||||
public string Decrypt(string encryptedValue)
|
||||
{
|
||||
if (string.IsNullOrEmpty(encryptedValue))
|
||||
return string.Empty;
|
||||
else
|
||||
return DecryptData(encryptedValue);
|
||||
}
|
||||
public string Decrypt(string encryptedValue)
|
||||
{
|
||||
if (string.IsNullOrEmpty(encryptedValue))
|
||||
return string.Empty;
|
||||
else
|
||||
return DecryptData(encryptedValue);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user