-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5157f99
Showing
13 changed files
with
1,556 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* FILE : AES_Example.go | ||
* PROJECT : INFO-1340 - Block Ciphers | ||
* PROGRAMMER : Daniel Pieczewski, ref: https://github.com/mickelsonm | ||
* FIRST VERSION : 2020-04-12 | ||
* DESCRIPTION : | ||
* The function(s) in this file make up example code for encryption and decryption of a block of text | ||
* using the Golang standard library AES implementation using the Cipher Feedback mode of encryption (CFB). | ||
* DISCLAIMER: There is no way that this a secure implementation of AES. This is only for my personal learning. | ||
* So help you God if this ends up in some commercial application. | ||
*/ | ||
package AesHelper | ||
|
||
import ( | ||
"crypto/aes" | ||
"crypto/cipher" | ||
"crypto/rand" | ||
"encoding/base64" | ||
"errors" | ||
"io" | ||
|
||
) | ||
|
||
/* | ||
* FUNCTION : encrypt | ||
* DESCRIPTION : | ||
* This function takes a string and a cipher key and uses AES to encrypt the message | ||
* | ||
* PARAMETERS : | ||
* byte[] key : Byte array containing the cipher key | ||
* byte[] message : byte[] containing the message to encrypt | ||
* | ||
* RETURNS : | ||
* byte[] encoded : byte[] containing the encoded user input | ||
* error err : Error message | ||
*/ | ||
func Encrypt(key []byte, plainText []byte) (encoded []byte, err error) { | ||
|
||
//Create a new AES cipher using the key | ||
block, err := aes.NewCipher(key) | ||
|
||
//IF NewCipher failed, exit: | ||
if err != nil { | ||
return | ||
} | ||
|
||
//Make the cipher text a byte array of size BlockSize + the length of the message | ||
cipherText := make([]byte, aes.BlockSize+len(plainText)) | ||
|
||
//iv is the ciphertext up to the blocksize (16) | ||
iv := cipherText[:aes.BlockSize] | ||
if _, err = io.ReadFull(rand.Reader, iv); err != nil { | ||
return | ||
} | ||
|
||
//Encrypt the data: | ||
stream := cipher.NewCFBEncrypter(block, iv) | ||
stream.XORKeyStream(cipherText[aes.BlockSize:], plainText) | ||
|
||
|
||
dst := make([]byte, base64.StdEncoding.EncodedLen(len(cipherText))) | ||
base64.StdEncoding.Encode(dst, cipherText) | ||
|
||
//Return string encoded in base64 | ||
return dst, err | ||
} | ||
|
||
/* | ||
* FUNCTION : decrypt | ||
* DESCRIPTION : | ||
* This function takes a string and a key and uses AES to decrypt the string into plain text | ||
* | ||
* PARAMETERS : | ||
* byte[] key : Byte array containing the cipher key | ||
* byte[] secure : byte[] containing an encrypted message | ||
* | ||
* RETURNS : | ||
* byte[] decoded : byte[] containing the decrypted equivalent of secure | ||
* error err : Error message | ||
*/ | ||
func Decrypt(key []byte, secure []byte) (decoded []byte, err error) { | ||
//Remove base64 encoding: | ||
cipherText := make([]byte, base64.StdEncoding.DecodedLen(len(secure))) | ||
_, err = base64.StdEncoding.Decode(cipherText, []byte(secure)) | ||
|
||
//IF DecodeString failed, exit: | ||
if err != nil { | ||
return | ||
} | ||
|
||
//Create a new AES cipher with the key and encrypted message | ||
block, err := aes.NewCipher(key) | ||
|
||
//IF NewCipher failed, exit: | ||
if err != nil { | ||
return | ||
} | ||
|
||
//IF the length of the cipherText is less than 16 Bytes: | ||
if len(cipherText) < aes.BlockSize { | ||
err = errors.New("Ciphertext block size is too short!") | ||
return | ||
} | ||
|
||
iv := cipherText[:aes.BlockSize] | ||
cipherText = cipherText[aes.BlockSize:] | ||
|
||
//Decrypt the message | ||
stream := cipher.NewCFBDecrypter(block, iv) | ||
stream.XORKeyStream(cipherText, cipherText) | ||
|
||
return cipherText, err | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package ClipboardHelper | ||
|
||
import ( | ||
"golang.design/x/clipboard" | ||
"context" | ||
) | ||
|
||
var ctxCancelFunc context.CancelFunc | ||
var ClipboardSet bool | ||
|
||
func init() { | ||
Initialize() | ||
} | ||
|
||
/* Check if the module is ready to work */ | ||
func Initialize() { | ||
|
||
err := clipboard.Init() | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
/* Read string from the clipboard */ | ||
func ReadClipboard() string { | ||
return string(clipboard.Read(clipboard.FmtText)) | ||
} | ||
|
||
/* Set clipboard value */ | ||
func SetClipboard(value string) { | ||
clipboard.Write(clipboard.FmtText, []byte(value)) | ||
ClipboardSet = true | ||
} | ||
|
||
/* Get a channel of clipboard values */ | ||
func GetClipboardChannel() <-chan []byte { | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
ctxCancelFunc = cancel | ||
|
||
ch := clipboard.Watch(ctx, clipboard.FmtText) | ||
return ch | ||
} | ||
|
||
/* Stop the "watch" of clipboard channel */ | ||
func CancelClipboardWatch() { | ||
ctxCancelFunc() | ||
} | ||
|
||
|
||
func main() { | ||
ch := GetClipboardChannel() | ||
|
||
for data := range ch { | ||
println(string(data)) | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package main | ||
|
||
import ( | ||
"ClipboardShare/ClipboardHelper" | ||
"ClipboardShare/PubsubHelper" | ||
"ClipboardShare/MessageHelper" | ||
"ClipboardShare/SqlHelper" | ||
"flag" | ||
"fmt" | ||
"os" | ||
"log" | ||
) | ||
|
||
func ClipboardReciever(modifyClipboard bool) { | ||
// creating messages channel, all the new messages will be added to here | ||
messages := make(chan []byte) | ||
|
||
log.Println("Subscribing to the PubSub Topic") | ||
go PubsubHelper.DefaultSubscribe(messages) | ||
|
||
log.Println("Starting PubSub listener") | ||
for data := range messages { | ||
deserializedMessage, err := MessageHelper.DeserializeMessage(data) | ||
|
||
if err != 0 { | ||
// something with this message gone wrong - ignoring it | ||
// for example - wrong checksum | ||
continue | ||
} | ||
|
||
// some debug | ||
log.Println(fmt.Sprintf("Received: %s, From Device: %s, Date: %d", | ||
deserializedMessage.Message, deserializedMessage.Device, deserializedMessage.Date)) | ||
|
||
// update the computer clipboard to the received data | ||
if modifyClipboard { | ||
ClipboardHelper.SetClipboard(deserializedMessage.Message) | ||
} | ||
|
||
} | ||
} | ||
|
||
func ClipboardSender(sendClipboard bool) { | ||
|
||
log.Println("Getting Clipboard Listener") | ||
clipChan := ClipboardHelper.GetClipboardChannel() | ||
|
||
log.Println("Starting Clipboard Listener") | ||
for data := range clipChan { | ||
|
||
if ClipboardHelper.ClipboardSet { | ||
ClipboardHelper.ClipboardSet = false | ||
continue | ||
} | ||
|
||
// public the new clipboard content to the Google Cloud PubSub | ||
if (sendClipboard) { | ||
log.Println("Sending " + string(data)) | ||
PubsubHelper.DefaultPublish(MessageHelper.SerializeMessage(MessageHelper.CreateMessage(string(data)))) | ||
} | ||
} | ||
} | ||
|
||
func main() { | ||
|
||
encryptionKey := flag.String("enc", "somemagichere123", "Encryption key for the messages") | ||
modifyClipboard := flag.Bool("modifyClipboard", true, "Modify clipboard when recieving messages") | ||
sendClipboard := flag.Bool("sendClipboard", true, "Send clipboard data when it's changed") | ||
pubsubJson := flag.String("pubsubJson", "", "Pub Sub creds json") | ||
flag.Parse() | ||
|
||
if *pubsubJson != "" { | ||
PubsubHelper.SetPubSubConfig(*pubsubJson) | ||
os.Exit(0) | ||
} | ||
|
||
// insert encryption key to the database | ||
SqlHelper.InsertConfig(SqlHelper.Config{Key: "encryptionKey", Value: *encryptionKey}) | ||
|
||
go ClipboardReciever(*modifyClipboard) | ||
ClipboardSender(*sendClipboard) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package MessageHelper | ||
|
||
import ( | ||
"ClipboardShare/SqlHelper" | ||
"ClipboardShare/AesHelper" | ||
"crypto/sha1" | ||
"bytes" | ||
"encoding/gob" | ||
"os" | ||
"io" | ||
"fmt" | ||
) | ||
|
||
// creating Message object from the input clipboard data | ||
// Input - the <string> content of the message | ||
// Output - a SqlHelper.Message object | ||
func CreateMessage(data string) *SqlHelper.Message { | ||
|
||
message := new(SqlHelper.Message) | ||
h := sha1.New() | ||
|
||
message.Date = SqlHelper.GetTimestamp() | ||
message.Message = data | ||
message.Mode = 1 // sent mode | ||
message.Device, _ = os.Hostname() | ||
|
||
// calculate checksum for the message | ||
io.WriteString(h, fmt.Sprintf("%d", message.Date)) | ||
io.WriteString(h, message.Message) | ||
io.WriteString(h, message.Device) | ||
message.Checksum = string(h.Sum(nil)) | ||
|
||
return message | ||
} | ||
|
||
// Converting SqlHelper.Message object to Encrypted byte array | ||
// encryption key is stored in the local database | ||
// input - SqlHelper.Message object | ||
// output - AES encrypted bytes array | ||
func SerializeMessage(message *SqlHelper.Message) []byte { | ||
var serializedMessage bytes.Buffer | ||
encryptionKey := SqlHelper.QueryConfigByKey("encryptionKey").Value | ||
|
||
enc := gob.NewEncoder(&serializedMessage) | ||
_ = enc.Encode(message) | ||
|
||
serializedData, _ := AesHelper.Encrypt([]byte(encryptionKey), serializedMessage.Bytes()) | ||
return serializedData | ||
} | ||
|
||
// Converting Encrypted byte array to SqlHelper.Message object | ||
// encryption key is stored in the local database | ||
// input - AES encrypted bytes array | ||
// output - SqlHelper.Message object | ||
func DeserializeMessage(serializedMessage []byte) (*SqlHelper.Message, int) { | ||
var message *SqlHelper.Message | ||
encryptionKey := SqlHelper.QueryConfigByKey("encryptionKey").Value | ||
|
||
serializedMessage, _ = AesHelper.Decrypt([]byte(encryptionKey), serializedMessage) | ||
|
||
reader := bytes.NewReader(serializedMessage) | ||
dec := gob.NewDecoder(reader) | ||
_ = dec.Decode(&message) | ||
|
||
message.Mode = 0 | ||
|
||
// validate checksum | ||
h := sha1.New() | ||
|
||
io.WriteString(h, fmt.Sprintf("%d", message.Date)) | ||
io.WriteString(h, message.Message) | ||
io.WriteString(h, message.Device) | ||
|
||
if ( string(h.Sum(nil)) != message.Checksum ) { | ||
// in case the message checksum is wrong - the data is diffrent from the original one | ||
return nil, -1 | ||
} | ||
|
||
return message, 0 | ||
} |
Oops, something went wrong.