Skip to content

Instantly share code, notes, and snippets.

@rusco
Created April 11, 2017 08:39
Show Gist options
  • Save rusco/3eaafba21c2866f065e39385f6a09231 to your computer and use it in GitHub Desktop.
Save rusco/3eaafba21c2866f065e39385f6a09231 to your computer and use it in GitHub Desktop.
Scramble binary files to make it unusable with restore option
package main
import (
"fmt"
"io/ioutil"
"log"
"math/rand"
"os"
)
const (
fileIn = "inoriginal.exe"
fileOut = "scrambled.exe"
fileOut2 = "insrestored.exe"
seed = 1234567890
)
func seedFunc(len int) []int {
rand.Seed(seed)
arr := make([]int, len)
for idx := 0; idx < len; idx++ {
arr[idx] = rand.Intn(idx + 1)
}
return arr
}
func unscramble(slice []byte) []byte {
n := len(slice)
s := seedFunc(n)
for i := 1; i < n; i++ {
j := s[n-i-1]
slice[i], slice[j] = slice[j], slice[i]
}
return slice
}
func scramble(slice []byte) []byte {
n := len(slice)
s := seedFunc(n)
for i := n - 1; i > 0; i-- {
j := s[n-i-1]
slice[i], slice[j] = slice[j], slice[i]
}
return slice
}
func main() {
fStatus, err := os.Stat(fileIn)
if err != nil {
log.Fatal(err)
}
bytesIn, err := ioutil.ReadFile(fileIn)
if err != nil {
fmt.Println(err)
return
}
bytesOut := scramble(bytesIn)
ioutil.WriteFile(fileOut, bytesOut, fStatus.Mode())
bytesOut2 := unscramble(bytesOut)
ioutil.WriteFile(fileOut2, bytesOut2, fStatus.Mode())
fmt.Println("finished.")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment