sophuwu.site > seks
working encryption and decryption with marshall and unmarshall
sophuwu sophie@sophuwu.site
Sun, 15 Oct 2023 16:14:45 +0200
commit

7e75414778e20df4c1bd1249e4f3ad2ba29b7152

parent

5b6e57ad5940dca22134bb4746d6c86b733a6191

4 files changed, 52 insertions(+), 31 deletions(-)

jump to
M go.modgo.mod

@@ -2,7 +2,6 @@ module seks

go 1.21.3 -require ( - golang.org/x/crypto v0.14.0 // indirect - golang.org/x/sys v0.13.0 // indirect -) +require golang.org/x/crypto v0.14.0 + +require golang.org/x/sys v0.13.0 // indirect
M main.gomain.go

@@ -28,7 +28,25 @@ // User makes a password for each bucket

passwd := []byte("TestPassword") - salt := ran() + encrypted, err := sopHex.UnMarshall(`-----BEGIN SEKS SECRET----- +A+=PEEM3<3PI<++1VE+=VS1O+L+H1AN3 +=L3HI+MPM33OE+3NANN1NASNSII=MV+N +1+P3VSOA+SOVLSL3VE=+EHM++LSPEAIM +SLI3NPMPN3IMMP=V<VS1N31AHPHAI1=S ++3P=1ISMIHALS3VHL=V3O3=1V=<1SI1H +E1HH=H3=IVE+OE=H+E=SE<VL1V13SIH= +3AP<OA3O=HNELO3PNL3N+1LO<I3SMOP< +VSEH<HEOIVOHSMLV=HH=3MAO3HMSSSEV +==E3AOO<HPOMSN<PAE1HNVP<NP+AVO+O +3ISIHAP1PE=1VPS<O1S<+LN+H=E3MLV+ +-----END SEKS SECRET----- +`) + if err != nil { + fmt.Println(err) + return + } + salt := encrypted[0:32] + //salt := ran() hash := sha256.New() hash.Write(passwd) hash.Write(salt)

@@ -36,20 +54,19 @@

var key [32]byte copy(key[:], hash.Sum(nil)) - var nonce = [24]byte(ran()[0:24]) - salt = append(salt[:], nonce[:]...) - message := []byte("I like to eat apples and bananas. However, I do not like to eat oranges. Cars can drive!") - encrypted := secretbox.Seal(nil, message, &nonce, &key) + //var nonce = [24]byte(ran()[0:24]) + //salt = append(salt[:], nonce[:]...) + //message := []byte("I like to eat apples and bananas. However, I do not like to eat oranges. Cars can drive!") + //encrypted := secretbox.Seal(salt, message, &nonce, &key) + + //fmt.Println(sopHex.Marshall(encrypted)) - fmt.Println(sopHex.Marshall(encrypted)) - /* - deNonce := [24]byte(encrypted[0:24]) - decrypted, boolEnlon := secretbox.Open(nil, encrypted[24:], &deNonce, &key) - if boolEnlon != true { - fmt.Println("OOPS") - return - } - fmt.Println(string(decrypted)) + deNonce := [24]byte(encrypted[32 : 32+24]) + decrypted, boolEnlon := secretbox.Open(nil, encrypted[32+24:], &deNonce, &key) + if boolEnlon != true { + fmt.Println("OOPS") + return + } + fmt.Println(string(decrypted)) - */ }
D sopHex/go.mod

@@ -1,3 +0,0 @@

-module sopHex - -go 1.21.3
M sopHex/sopHex.gosopHex/sopHex.go

@@ -1,14 +1,14 @@

package sopHex -import "fmt" +import ( + "fmt" + "strings" +) const sopHexSet string = "SOPHIE+MAL1VN=<3" -const seksHeader string = `-----BEGIN SEKS SECRET----- -` -const seksFooter string = ` ------END SEKS SECRET----- -` +const seksHeader string = "-----BEGIN SEKS SECRET-----" +const seksFooter string = "-----END SEKS SECRET-----" func Marshall(b []byte) string { var s string

@@ -18,13 +18,21 @@ s += "\n"

} s += string(sopHexSet[(b[i]>>4)&15]) + string(sopHexSet[b[i]&15]) } - return seksHeader + s + seksFooter + return seksHeader + s + "\n" + seksFooter + "\n" } func UnMarshall(s string) ([]byte, error) { - if len(s)%2 != 0 { - return nil, fmt.Errorf("sopHex UnMarshall: invalid length %d", len(s)) + begin := strings.Index(s, seksHeader) + end := strings.Index(s, seksFooter) + if begin < 0 || end < 0 { + return nil, fmt.Errorf("sopHex UnMarshall: invalid seks secret") } + s = s[begin+len(seksHeader) : end] + s = strings.ReplaceAll(s, "\t", "") + s = strings.ReplaceAll(s, "\n", "") + s = strings.TrimPrefix(s, seksHeader) + s = strings.TrimSuffix(s, seksFooter) + s = strings.ReplaceAll(s, " ", "") var b []byte var n int for i, v := range s {