diff --git a/Projs/PD2/internal/utils/cryptoUtils/cryptoUtils.go b/Projs/PD2/internal/utils/cryptoUtils/cryptoUtils.go index 9b10d7d..3052c85 100644 --- a/Projs/PD2/internal/utils/cryptoUtils/cryptoUtils.go +++ b/Projs/PD2/internal/utils/cryptoUtils/cryptoUtils.go @@ -252,9 +252,8 @@ func (k *KeyStore) GetServerTLSConfig() *tls.Config { return tlsConfig } -func (k KeyStore) EncryptMessageContent(receiverCert *x509.Certificate, content []byte) ([]byte, error) { +func (k KeyStore) EncryptMessageContent(receiverCert *x509.Certificate, recieverId string, content []byte) ([]byte, error) { // Digital envolope - // Create a random symmetric key dataKey := make([]byte, 32) if _, err := rand.Read(dataKey); err != nil { @@ -272,7 +271,7 @@ func (k KeyStore) EncryptMessageContent(receiverCert *x509.Certificate, content } // sign the message and append the signature - hashedContent := sha256.Sum256(content) + hashedContent := sha256.Sum256(append(content, []byte(recieverId)...)) signature, err := rsa.SignPKCS1v15(nil, k.privKey, crypto.SHA256, hashedContent[:]) if err != nil { return nil, err @@ -288,7 +287,7 @@ func (k KeyStore) EncryptMessageContent(receiverCert *x509.Certificate, content return pair(encryptedDataKey, ciphertext), nil } -func (k KeyStore) DecryptMessageContent(senderCert *x509.Certificate, cipherContent []byte) ([]byte, error) { +func (k KeyStore) DecryptMessageContent(senderCert *x509.Certificate, recieverId string, cipherContent []byte) ([]byte, error) { encryptedDataKey, encryptedMsg := unPair(cipherContent) dataKey, err := rsa.DecryptOAEP(sha256.New(), rand.Reader, k.GetPrivKey(), encryptedDataKey, nil) if err != nil { @@ -307,7 +306,7 @@ func (k KeyStore) DecryptMessageContent(senderCert *x509.Certificate, cipherCont } // check signature with sender public key signature, content := unPair(contentAndSig) - hashedContent := sha256.Sum256(content) + hashedContent := sha256.Sum256(append(content, []byte(recieverId)...)) senderKey := senderCert.PublicKey.(*rsa.PublicKey) if err := rsa.VerifyPKCS1v15(senderKey, crypto.SHA256, hashedContent[:], signature); err != nil { return nil, err