silly powershell
# Function to get hardware identifiers function Get-HardwareId { $cpu = (Get-WmiObject Win32_Processor).ProcessorId $disk = (Get-WmiObject Win32_DiskDrive | Where-Object {$_.Index -eq 0}).SerialNumber return "$cpu-$disk" } # Function to encrypt data function Protect-Data { param ( [string]$FilePath, [string]$EncryptedPath ) try { # Get hardware ID and combine with data $hwid = Get-HardwareId $data = Get-Content $FilePath -Raw $dataWithHwid = "$hwid`n$data" # Encrypt the data $bytes = [Text.Encoding]::UTF8.GetBytes($dataWithHwid) $encrypted = [System.Security.Cryptography.ProtectedData]::Protect( $bytes, $null, [System.Security.Cryptography.DataProtectionScope]::CurrentUser ) # Save encrypted data [Convert]::ToBase64String($encrypted) | Set-Content $EncryptedPath Write-Host "File encrypted successfully." } catch { Write-Host "Encryption failed: $_" } } # Function to decrypt data function Unprotect-Data { param ( [string]$EncryptedPath, [string]$DecryptedPath ) try { # Decrypt the data $encryptedData = Get-Content $EncryptedPath -Raw $decrypted = [System.Security.Cryptography.ProtectedData]::Unprotect( [Convert]::FromBase64String($encryptedData), $null, [System.Security.Cryptography.DataProtectionScope]::CurrentUser ) $decryptedText = [Text.Encoding]::UTF8.GetString($decrypted) # Split hardware ID and actual data $lines = $decryptedText -split "`n", 2 $storedHwid = $lines[0] $currentHwid = Get-HardwareId # Verify hardware ID if ($storedHwid -ne $currentHwid) { Write-Host "Error: Hardware verification failed. This file cannot be decrypted on this machine." return } # Save decrypted data $lines[1] | Set-Content $DecryptedPath Write-Host "File decrypted successfully." } catch { Write-Host "Decryption failed: $_" } } # Example usage: # Protect-Data -FilePath "C:\path\to\original.txt" -EncryptedPath "C:\path\to\encrypted.bin" # Unprotect-Data -EncryptedPath "C:\path\to\encrypted.bin" -DecryptedPath "C:\path\to\decrypted.txt"
Leave a Comment