working encryption and decryption with marshall and unmarshall
sophuwu sophie@sophuwu.site
Sun, 15 Oct 2023 16:14:45 +0200
4 files changed,
52 insertions(+),
31 deletions(-)
M
main.go
→
main.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)) - */ }
M
sopHex/sopHex.go
→
sopHex/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 {