PDA

View Full Version : گفتگو: رمزگذاری فایل های ویندوز با ابزار ویندوز و ایجاد کلید رمز



ROSTAM2
یک شنبه 26 فروردین 1403, 10:43 صبح
سلام.

1- برای رمزگذاری فایل در ویندوز 10 از این روش استفاده می کنم...

155581 155582 155583155584 155585

ادامه در پست بعدی....

ROSTAM2
یک شنبه 26 فروردین 1403, 10:49 صبح
ادامه پست اول....

155586

با این مراحل فایل یا پوشه و فایلهی آن رمزگذاری می شوند.

اما اگر کلید بازیابی رمزگذاری ذخیره سازی نشود فایل ها از دست می روند....

برای ایجاد کلید بازیابی رمزگذاری این مراحل باید طی شود....

بعد از مرحله سوم که برای رمزگذاری فایل استفاده شده ادامه می دهم ....

155587 155588 155589 155590

ادامه در پست بعد....

ROSTAM2
یک شنبه 26 فروردین 1403, 10:53 صبح
ادامه پست دوم ....

155591

155592

155593

و اما من می خواهم با برنامه نویسی کلید بازیابی رمزگذاری ها (Certificate) را ایجاد کنم که کار را ساده کنم....

سوال: این کار رو با چه دستوراتی می توان انجام داد؟

با تشکر

ROSTAM2
یک شنبه 26 فروردین 1403, 17:51 عصر
پیداش کردم.

برای این کار نیاز به مرجع System.Security داریم:

Imports System.Security.Cryptography.X509Certificates


زمانی که فایل قفل یا رمزگذاری می شه طبق نام حساب کاربری فعلی Certificate در Store ایجاد و ذخیره سازی می شه:

155598

که با این دستور می شه اونرو بصورت Binary استخراج کرد و در نهایت یا بصورت فایل و یا در دیتابیس برای استفاده مجدد ذخیره سازی کرد:

تابع ایجاد شده: که کدش رو از MSDN گیر آوردم:

Private Shared Function GetCertificateFromStore(ByVal certName As String) As X509Certificate2
' Get the certificate store for the current user.
Dim store As New X509Store(StoreLocation.CurrentUser)
Try
store.Open(OpenFlags.ReadOnly)


' Place all certificates in an X509Certificate2Collection object.
Dim certCollection As X509Certificate2Collection = store.Certificates
' If using a certificate with a trusted root you do not need to FindByTimeValid, instead use:
' currentCerts.Find(X509FindType.FindBySubjectDistin guishedName, certName, true);
Dim currentCerts As X509Certificate2Collection = certCollection.Find(X509FindType.FindByTimeValid, DateTime.Now, False)
Dim signingCert As X509Certificate2Collection = currentCerts.Find(X509FindType.FindBySubjectDistin guishedName, certName, False)
If signingCert.Count = 0 Then
Return Nothing
End If ' Return the first certificate in the collection, has the right name and is current.
Return signingCert(0)
Finally
store.Close()
End Try
End Function 'GetCertificateFromStore


و دستوری که برای استخراج اطلاعات Certificate ایجاد کردم....

Dim CertName As String = String.Format("CN={0}", My.User.Name)
Dim cert As X509Certificate2 = GetCertificateFromStore(CertName)
Dim Bytes As Byte() = cert.Export(X509ContentType.Pfx, "2")
Dim FileName As String = String.Format(".\{0}.pfx", My.User.Name.Replace("", "-"))
IO.File.WriteAllBytes(FileName, Bytes)


در مورد کاراییش توی یک حساب کاربری دیگه هنور تست نکردم. اگه مشکلی داشت تصحیح خواهم کرد اِن شاء الله....

ROSTAM2
یک شنبه 26 فروردین 1403, 18:25 عصر
من برای اینکه تست کنم با رمزگذاری فایل Certificate با چه نامی ایجاد می شه همه Certificate ها رو از Store --> Personal حذف کردم ولی وقتی خواستم Backup بگیرم خطا ارسال کرد.

برای رفع خطا Backup که از قبل داشتم رو اجرا کردم و گزینه استفاده برای Backup رو هم براش فعال کردم درست شد:

155599

اما نامی که ذخیره سازی شد فقط نام حساب کاربری بود و نام Computer قبلش نبود، یعنی بصورت پیش فرض به این صورت در Store موجود هست.

بنابراین دستور رو برای دریافت نام حساب کاربری به اینصورت تغییر دادم:

Dim CertName As String = String.Format("CN={0}", My.User.Name.Split("")(1))
Dim cert As X509Certificate2 = GetCertificateFromStore(CertName)


توی تابع Split از (\) Back Slash استفاده شده که این سایت حذفش می کنه.

البته از این دستور هم می شه استفاده کرد:

System.Environment.UserName

ROSTAM2
جمعه 12 مرداد 1403, 11:33 صبح
155924
اجرای فایل قفل(رمزگذاری) شده در حساب کاربری دیگر با ویژوال بیسیک (https://www.aparat.com/v/hrb4499)

ROSTAM2
پنج شنبه 25 مرداد 1403, 19:08 عصر
سلام به همه

برای تشخیص فایل رمزگذاری شده می شه از FileAttributes استفاده کرد:


<Extension()>
Function IsEncrypted(File As IO.FileInfo) As Boolean
Return File.Attributes And System.IO.FileAttributes.Encrypted
End Function


سوال من اینه که چطور توی حساب کاربری دیگه و یا یک سیستم دیگه تشخیص بدیم این فایل قابل دسترسی توسط اون حساب کاربری نیست؟!

با تشکر....

ROSTAM2
جمعه 26 مرداد 1403, 04:33 صبح
این کد هست ولی هیچ عکس العملی نشون نمی ده!!!


Dim File As New System.IO.FileInfo(IO.Path.GetFullPath(".\database\KeyStore.sdf"))
Dim f2 As New FileIOPermission(FileIOPermissionAccess.Read Or FileIOPermissionAccess.Write, File.FullName)
Try
f2.Demand()
Catch s As System.Security.SecurityException
MsgBox(s.Message, MsgBoxStyle.Critical)
End
End Try


البته این کد رو خودم نوشتم مطمئنا روش صحیح نیست ولی خطای عدم دسترسی رو بر می گردونه....


<Extension()>
Function IsAccessible(File As IO.FileInfo) As Boolean
Dim Strm As IO.Stream = Nothing
Try
strm = File.OpenRead()
Return True
Catch ex As Exception
Return False
Finally
If Strm IsNot Nothing Then Strm.Close()
End Try
End Function


155972

ROSTAM2
جمعه 26 مرداد 1403, 14:47 عصر
سلام مجدد
من این کد رو برای ذخیره گواهینامه در جدول دیتابیس نوشتم ولی موقع فراخوانی نمی دونم چطوری بایت هاش رو بصورت صحیح برگردونم که خطای پسورد اشتباه نده....

155973


Imports System.Security.Principal
Imports System.Security.AccessControl
Imports System.Security.Permissions
Imports System.Security.Cryptography.X509Certificates
Public Module ProtectionMethods
Friend Function CheckForEncryption() As Boolean
Try
Redo: Dim File As New System.IO.FileInfo(IO.Path.GetFullPath(".\database\KeyStore.sdf"))


If File.IsEncrypted = False Then
With CertificatePasswordDialog
If .ShowDialog(MainForm) = DialogResult.Cancel Then Return False
Dim Bytes As Byte() = {}
For Each CertItem As Certificates In My.ProtectionData.Certificates.Execute(Objects.Mer geOption.PreserveChanges)
My.ProtectionData.DeleteObject(CertItem)
Next
My.ProtectionData.SaveChanges()
My.ProtectionData.AcceptAllChanges()
Dim Certificate As X509Certificate2 = TrustCertificates.FromStore
Bytes = Certificate.Export(X509ContentType.Pfx, .Password)


Dim Cert As New Certificates With {.ID = New Guid("<GUID>"),
.FriendlyName = Certificate.FriendlyName,
.SerialNumber = Certificate.SerialNumber,
.rawBytes = Bytes,
.Length = Bytes.Length
}
My.ProtectionData.AddToCertificates(Cert)
End With


My.ProtectionData.SaveChanges()
My.ProtectionData.AcceptAllChanges()
File.Encrypt()
GoTo Redo
Else
If File.IsAccessible = False Then
'MsgBox("منبع اطلاعات الکترونیک برای این حساب کاربری غیر قابل دسترسی است!", MsgBoxStyle.Critical)
Dim Param As New Objects.ObjectParameter("ID", New Guid("<GUID>"))
Dim QueryList As Objects.ObjectQuery(Of Certificates) = My.ProtectionData.CreateQuery(Of Certificates)(
"SELECT VALUE Certificates FROM Certificates WHERE Certificates.ID = @ID", Param)
If QueryList.Count = 0 Then
MsgBox("اطلاعات گواهینامه در منبع اطلاعات الکترونیک موجود نیست!", MsgBoxStyle.Critical)
Return False
End If
Dim Expr As X509Certificate2 = Nothing
Dim Cert As Certificates = QueryList.FirstOrDefault
Dim Bytes(Cert.Length - 1) As Byte
Array.Copy(Cert.rawBytes, Bytes, Cert.Length)
With CertificateLoginDialog
.FriendlyName = Cert.FriendlyName
.SerialNumber = Cert.SerialNumber
If .ShowDialog(MainForm) = DialogResult.Cancel Then Return False
Try
Expr = TrustCertificates.FromBytes(Bytes, .Password)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
GoTo Redo
End Try
TrustCertificates.Add(Expr)
MsgBox("گواهینامه منبع اطلاعات الکترونیک به حساب کاربری اضافه شد.", MsgBoxStyle.Information)
GoTo Redo
End With
End If
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
Return False
End Try
Return True
End Function


End Module



Function FromStore() As X509Certificate2
' Get the certificate store for the current user.
Dim store As New X509Store(StoreLocation.CurrentUser)
Try
store.Open(OpenFlags.ReadOnly)
' Place all certificates in an X509Certificate2Collection object.
Dim certCollection As X509Certificate2Collection = store.Certificates
' If using a certificate with a trusted root you do not need to FindByTimeValid, instead use:
' currentCerts.Find(X509FindType.FindBySubjectDistin guishedName, certName, true);
Dim currentCerts As X509Certificate2Collection = certCollection.Find(X509FindType.FindByTimeValid, DateTime.Now, False)
Dim signingCert As X509Certificate2Collection = currentCerts.Find(X509FindType.FindBySubjectDistin guishedName, String.Format("CN={0}", Environment.UserName), False)
If signingCert.Count = 0 Then
Return Nothing
End If ' Return the first certificate in the collection, has the right name and is current.
Return signingCert(0)
Finally
store.Close()
End Try
End Function 'GetCertificateFromStore



Function FromBytes(Bytes As Byte(), Password As Security.SecureString) As X509Certificate2
Dim cert As New X509Certificate2(Bytes, Password)
Return cert
End Function

ROSTAM2
جمعه 26 مرداد 1403, 16:24 عصر
درست شد من دستورات رو درست ننوشته بودم یکی برای پنجره Password ها و یکی هم متود Add برای اضافه کردن به Store

الآن براحتی با رمز برای هر حساب کاربری دسترسی به دیتابیس مجاز می شه....


Shared Sub Add(Certificate As X509Certificate2)
Dim store As New X509Store(StoreLocation.CurrentUser)
Try
store.Open(OpenFlags.ReadWrite)
store.Add(Certificate)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
Finally
store.Close()
End Try
End Sub


با تشکر از همه مخصوصا خودم.... :لبخند: