Created
May 26, 2023 17:26
-
-
Save puppis42/0f2d08091d13821673a50c88ff26fd99 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"github.com/chzyer/readline" | |
"strings" | |
"crypto/aes" | |
"crypto/cipher" | |
"io/ioutil" | |
"io" | |
"crypto/rand" | |
"syscall" | |
"unsafe" | |
) | |
func main() { | |
//Set Title Console | |
SetConsoleTitle("AES ENC-DEC") | |
//Get args | |
data := os.Args[1] | |
if strings.Contains(data, ".locked") { | |
fmt.Println("Do you want decrypt this file? (Y/N) " + data) | |
rl, err := readline.New("> ") | |
if err != nil { | |
panic(err) | |
} | |
defer rl.Close() | |
line, err := rl.Readline() | |
if err != nil { // io.EOF | |
os.Exit(0) | |
} | |
if line == "Y" || line == "y" { | |
//Yes... | |
decfile() | |
}else if line == "N" || line == "n" { | |
os.Exit(0) | |
}else { | |
os.Exit(0) | |
} | |
}else { | |
fmt.Println("Do you want encrypt this file? (Y/N) " + data) | |
rl, err := readline.New("> ") | |
if err != nil { | |
panic(err) | |
} | |
defer rl.Close() | |
line, err := rl.Readline() | |
if err != nil { // io.EOF | |
panic(err) | |
} | |
if line == "Y" || line == "y" { | |
//Yes... | |
encfile() | |
}else if line == "N" || line == "n" { | |
os.Exit(0) | |
}else { | |
os.Exit(0) | |
} | |
} | |
fmt.Scanln() | |
} | |
func encfile(){ | |
data := os.Args[1] | |
fmt.Println("[32 character] Password: ") | |
rl, err := readline.New("> ") | |
if err != nil { | |
panic(err) | |
} | |
defer rl.Close() | |
line, err := rl.Readline() | |
//Encrypt file | |
encryptfile(data, line) | |
//Delete originial file | |
err = os.Remove(data) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
fmt.Println("File successfully encrypted!") | |
fmt.Println("Please press any key...") | |
} | |
func check(e error) { | |
if e != nil { | |
panic(e) | |
} | |
} | |
func decfile(){ | |
data := os.Args[1] | |
fmt.Println("[32 character] Password: ") | |
rl, err := readline.New("> ") | |
if err != nil { | |
panic(err) | |
} | |
defer rl.Close() | |
//Decrpting... | |
line, err := rl.Readline() | |
key := []byte(line) | |
ciphertext, err := ioutil.ReadFile(data) | |
if err != nil { | |
fmt.Println(err) | |
} | |
c, err := aes.NewCipher(key) | |
if err != nil { | |
fmt.Println(err) | |
} | |
gcmDecrypt, err := cipher.NewGCM(c) | |
if err != nil { | |
fmt.Println(err) | |
} | |
nonceSize := gcmDecrypt.NonceSize() | |
if len(ciphertext) < nonceSize { | |
fmt.Println(err) | |
} | |
nonce, encryptedMessage := ciphertext[:nonceSize], ciphertext[nonceSize:] | |
plaintext, err := gcmDecrypt.Open(nil, nonce, encryptedMessage, nil) | |
if err != nil { | |
fmt.Println(err) | |
} | |
//Decrypt file | |
decryptfile(string(plaintext), strings.Replace(data, ".locked", "", 2)) | |
fmt.Println("File successfully decrypted!") | |
fmt.Println("Please press any key...") | |
} | |
func encryptfile(file string, keyy string){ | |
dat, err := ioutil.ReadFile(file) | |
check(err) | |
text := []byte(string(dat)) | |
key := []byte(keyy) | |
cphr, err := aes.NewCipher(key) | |
if err != nil { | |
fmt.Println(err) | |
} | |
gcm, err := cipher.NewGCM(cphr) | |
if err != nil { | |
fmt.Println(err) | |
} | |
nonce := make([]byte, gcm.NonceSize()) | |
if _, err = io.ReadFull(rand.Reader, nonce); err != nil { | |
fmt.Println(err) | |
} | |
err = ioutil.WriteFile(file + ".locked", gcm.Seal(nonce, nonce, text, nil), 0777) | |
if err != nil { | |
fmt.Println(err) | |
} | |
} | |
func decryptfile(data string, filenew string){ | |
d1 := []byte(data) | |
err := ioutil.WriteFile(filenew, d1, 0644) | |
check(err) | |
} | |
func SetConsoleTitle(title string) (int, error) { | |
handle, err := syscall.LoadLibrary("Kernel32.dll") | |
if err != nil { | |
return 0, err | |
} | |
defer syscall.FreeLibrary(handle) | |
proc, err := syscall.GetProcAddress(handle, "SetConsoleTitleW") | |
if err != nil { | |
return 0, err | |
} | |
r, _, err := syscall.Syscall(proc, 1, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(title))), 0, 0) | |
return int(r), err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment