"Активування Trial версії программи"



Download 60 Kb.
Sana09.06.2022
Hajmi60 Kb.
#647850
TuriЛабораторна робота
Bog'liq
БП lab №7


ХМЕЛЬНИЦЬКИЙ НАЦІОНАЛЬНИЙ УНІВЕРСИТЕТ

Факультет інформаційних технологій

Кафедра інженерії програмного забезпечення

Лабораторна робота № 7

з дисципліни «Безпека програм і даних»


на тему:
“Активування Trial версії программи”

Виконав:студент 2 курсу, групи ІПЗ-20 ______________ Б. І. Балицький
(підпис)
Перевірив: ______________ І. В. Гурман
(підпис)
Хмельницький – 2022 р.
Мета роботи: втілити можливість активування программи.
Код программи:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.IO;
using System.Management;
using System.Runtime.Serialization.Formatters.Binary;

namespace lab6


{
public partial class Form1 : Form
{
const string path1 = "C:\\ProgramData\\win64.dat";
const string path1_1 = "C:\\ProgramData\\win64_k.dat";
const string path1_2 = "C:\\ProgramData\\win64_v.dat";
const string path2 = "C:\\temp\\set32.dat";
const string path2_1 = "C:\\temp\\set32_k.dat";
const string path2_2 = "C:\\temp\\set32_v.dat";
const string path3 = "C:\\boot.dat";
const string path3_1 = "C:\\boot_k.dat";
const string path3_2 = "C:\\boot_v.dat";
public AeS a = new AeS();
public D D = new D();
int trial = 20;
string HardInfo = GetComponent("Win32_Processor", "ProcessorId");
BinaryFormatter formatter = new BinaryFormatter();
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
//label2.Text = HardInfo;
bool off = true;
bool p1 = false;
bool p2 = false;
bool p3 = false;
if (!File.Exists(path1)) p1 = false;
else p1 = true;
if (!File.Exists(path2)) p2 = false;
else p2 = true;
if (!File.Exists(path3)) p3 = false;
else p3 = true;
bool b = false;

if (p1 || p2 || p3)


{
if (p1)
{
try
{
off = Read(path1, path1_1, path1_2, a);
}
catch (Exception)
{
throw;
}
}
else if (p2)
{
try
{
off = Read(path2, path2_1, path2_2, a);
}
catch (Exception)
{
throw;
}
}
else if (p3)
{
try
{
off = Read(path3, path3_1, path3_2, a);
}
catch (Exception)
{
throw;
}
}
if (off)
{
label1.Text = "Днів тріалу залишилось:" + trial;
}
else
{
MessageBox.Show("Тріал закінчився", "0 днів", MessageBoxButtons.OK);
Application.Exit();
}
}
else
{
Write(path1, path1_1, path1_2, a, D);
Write(path2, path2_1, path2_2, a, D);
Write(path3, path3_1, path3_2, a, D);
}
File.WriteAllText("key.txt", Hash(HardInfo));

}
private static string GetComponent(string hwclass, string syntax)


{
string result = " ";
ManagementObjectSearcher mos = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM " + hwclass);
foreach (ManagementObject mj in mos.Get())
{
if (Convert.ToString(mj[syntax]) != "")
result = (Convert.ToString(mj[syntax]));
}
return result;
}
static string Hash(string input)
{
using (SHA1Managed sha1 = new SHA1Managed())
{
var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(input));
var sb = new StringBuilder(hash.Length * 2);

foreach (byte b in hash)


{
// can be "x2" if you want lowercase
sb.Append(b.ToString("X2"));
}

return sb.ToString();


}
}

[DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")]


public static extern bool ZeroMemory(IntPtr Destination, int Length);

private void Write(string path, string path_1, string path_2, AeS aes, D d)


{

try
{


File.WriteAllText(path, DateTime.Now.ToLongDateString());
string data = File.ReadAllText(path);
byte[] output = EncryptStringToBytes_Aes(data, aes.myAes.Key, aes.myAes.IV);
File.WriteAllBytes(path, output);
File.WriteAllBytes(path_1, aes.myAes.Key);
File.WriteAllBytes(path_2, aes.myAes.IV);
}
catch (Exception)
{
throw;
}
}
private bool Read(string path, string path_1, string path_2, AeS aes)
{
a.myAes.Key = File.ReadAllBytes(path_1);
a.myAes.IV = File.ReadAllBytes(path_2);
string s = DecryptStringFromBytes_Aes(File.ReadAllBytes(path), a.myAes.Key, a.myAes.IV);
D.thisDay = DateTime.Parse(s);
if (D.thisDay.AddDays(30).CompareTo(DateTime.Now) > 0)
{
trial = 30 - (int)(DateTime.Now - DateTime.Today).TotalDays;
return true;
}
else
{
trial = 0;
return false;
}
}
static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV)
{
// Check arguments.
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
byte[] encrypted;
// Create an Aes object
// with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = Key;
aesAlg.IV = IV;
// Create an encryptor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
//Write all data to the stream.
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
// Return the encrypted bytes from the memory stream.
return encrypted;
}
static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
// Create an Aes object
// with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = Key;
aesAlg.IV = IV;
// Create a decryptor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}

private void button1_Click(object sender, EventArgs e)


{
if(sebPassword.Text == Hash(HardInfo))
{
trial = 1000000;
label1.Text = "Ви користуєтесь повною версією программи";
}
}
}
public class AeS
{

public Aes myAes;


public AeS()
{
myAes = Aes.Create();

}


}

public class D


{
public DateTime thisDay;
public D()
{
thisDay = DateTime.Now;
}
}
}
Результат роботи программи:






Висновок: під час виконання лабораторної роботи було розглянуто метод хешування айді процессора SHA1 і використання його, як ключа.
Download 60 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©www.hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish