no encrypt

This commit is contained in:
Zack Scholl 2023-10-05 08:30:56 -07:00
parent 601d88af74
commit e8eb78f020
2 changed files with 36 additions and 42 deletions

View File

@ -1802,10 +1802,10 @@ func (c *Client) receiveData(i int) {
continue continue
} }
data, err = crypt.Decrypt(data, c.Key) // data, err = crypt.Decrypt(data, c.Key)
if err != nil { // if err != nil {
panic(err) // panic(err)
} // }
if !c.Options.NoCompress { if !c.Options.NoCompress {
data = compress.Decompress(data) data = compress.Decompress(data)
} }
@ -1904,10 +1904,7 @@ func (c *Client) sendData(i int) {
var err error var err error
var dataToSend []byte var dataToSend []byte
if c.Options.NoCompress { if c.Options.NoCompress {
dataToSend, err = crypt.Encrypt( dataToSend = append(posByte, data[:n]...)
append(posByte, data[:n]...),
c.Key,
)
} else { } else {
dataToSend, err = crypt.Encrypt( dataToSend, err = crypt.Encrypt(
compress.Compress( compress.Compress(

View File

@ -1,6 +1,7 @@
package crypt package crypt
import ( import (
"crypto/aes"
"crypto/cipher" "crypto/cipher"
"crypto/rand" "crypto/rand"
"crypto/sha256" "crypto/sha256"
@ -34,46 +35,42 @@ func New(passphrase []byte, usersalt []byte) (key []byte, salt []byte, err error
// Encrypt will encrypt using the pre-generated key // Encrypt will encrypt using the pre-generated key
func Encrypt(plaintext []byte, key []byte) (encrypted []byte, err error) { func Encrypt(plaintext []byte, key []byte) (encrypted []byte, err error) {
encrypted = plaintext // generate a random iv each time
// http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
// Section 8.2
ivBytes := make([]byte, 12)
if _, err = rand.Read(ivBytes); err != nil {
log.Fatalf("can't initialize crypto: %v", err)
}
b, err := aes.NewCipher(key)
if err != nil {
return
}
aesgcm, err := cipher.NewGCM(b)
if err != nil {
return
}
encrypted = aesgcm.Seal(nil, ivBytes, plaintext, nil)
encrypted = append(ivBytes, encrypted...)
return return
// // generate a random iv each time
// // http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
// // Section 8.2
// ivBytes := make([]byte, 12)
// if _, err = rand.Read(ivBytes); err != nil {
// log.Fatalf("can't initialize crypto: %v", err)
// }
// b, err := aes.NewCipher(key)
// if err != nil {
// return
// }
// aesgcm, err := cipher.NewGCM(b)
// if err != nil {
// return
// }
// encrypted = aesgcm.Seal(nil, ivBytes, plaintext, nil)
// encrypted = append(ivBytes, encrypted...)
// return
} }
// Decrypt using the pre-generated key // Decrypt using the pre-generated key
func Decrypt(encrypted []byte, key []byte) (plaintext []byte, err error) { func Decrypt(encrypted []byte, key []byte) (plaintext []byte, err error) {
plaintext = encrypted if len(encrypted) < 13 {
err = fmt.Errorf("incorrect passphrase")
return
}
b, err := aes.NewCipher(key)
if err != nil {
return
}
aesgcm, err := cipher.NewGCM(b)
if err != nil {
return
}
plaintext, err = aesgcm.Open(nil, encrypted[:12], encrypted[12:], nil)
return return
// if len(encrypted) < 13 {
// err = fmt.Errorf("incorrect passphrase")
// return
// }
// b, err := aes.NewCipher(key)
// if err != nil {
// return
// }
// aesgcm, err := cipher.NewGCM(b)
// if err != nil {
// return
// }
// plaintext, err = aesgcm.Open(nil, encrypted[:12], encrypted[12:], nil)
// return
} }
// NewArgon2 generates a new key based on a passphrase and salt // NewArgon2 generates a new key based on a passphrase and salt