Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
0c9621ef70 | |||
f814e3a1f9 |
@@ -1,89 +0,0 @@
|
||||
package crypto
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/sha256"
|
||||
"crypto/x509"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"log"
|
||||
)
|
||||
|
||||
// HashToken hashes a token using SHA-256.
|
||||
func HashToken(token string) string {
|
||||
hash := sha256.Sum256([]byte(token))
|
||||
return hex.EncodeToString(hash[:])
|
||||
}
|
||||
|
||||
// VerifyToken compares a plaintext token with a stored hash.
|
||||
func VerifyToken(token, storedHash string) bool {
|
||||
return HashToken(token) == storedHash
|
||||
}
|
||||
|
||||
func main() string {
|
||||
// 1. Generiere einen privaten Schlüssel
|
||||
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||
if err != nil {
|
||||
log.Fatalf("Fehler beim Generieren des privaten Schlüssels: %v", err)
|
||||
}
|
||||
|
||||
// 2. Extrahiere den öffentlichen Schlüssel
|
||||
publicKey := &privateKey.PublicKey
|
||||
|
||||
// 3. Beispieltext zum Verschlüsseln
|
||||
plainText := "Dies ist ein geheimer String!"
|
||||
|
||||
// 4. Verschlüssle den Text mit dem öffentlichen Schlüssel
|
||||
encryptedBytes, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey, []byte(plainText))
|
||||
if err != nil {
|
||||
log.Fatalf("Fehler beim Verschlüsseln: %v", err)
|
||||
}
|
||||
|
||||
// Kodierung des verschlüsselten Textes in Base64 für Lesbarkeit
|
||||
encryptedString := base64.StdEncoding.EncodeToString(encryptedBytes)
|
||||
fmt.Println("Verschlüsselter Text (Base64):", encryptedString)
|
||||
|
||||
// 5. Entschlüssle den Text mit dem privaten Schlüssel
|
||||
decryptedBytes, err := rsa.DecryptPKCS1v15(rand.Reader, privateKey, encryptedBytes)
|
||||
if err != nil {
|
||||
log.Fatalf("Fehler beim Entschlüsseln: %v", err)
|
||||
}
|
||||
|
||||
// 6. Gib den entschlüsselten Text aus
|
||||
decryptedString := string(decryptedBytes)
|
||||
fmt.Println("Entschlüsselter Text:", decryptedString)
|
||||
|
||||
// 7. (Optional) Exportiere und Importiere die Schlüssel
|
||||
privateKeyPEM := exportPrivateKeyToPEM(privateKey)
|
||||
fmt.Println("\nPrivate Key (PEM):\n", privateKeyPEM)
|
||||
|
||||
publicKeyPEM := exportPublicKeyToPEM(publicKey)
|
||||
fmt.Println("\nPublic Key (PEM):\n", publicKeyPEM)
|
||||
return ""
|
||||
}
|
||||
|
||||
// Funktion, um den privaten Schlüssel in PEM-Format zu exportieren
|
||||
func exportPrivateKeyToPEM(privateKey *rsa.PrivateKey) string {
|
||||
privateKeyBytes := x509.MarshalPKCS1PrivateKey(privateKey)
|
||||
privateKeyPEM := pem.EncodeToMemory(&pem.Block{
|
||||
Type: "RSA PRIVATE KEY",
|
||||
Bytes: privateKeyBytes,
|
||||
})
|
||||
return string(privateKeyPEM)
|
||||
}
|
||||
|
||||
// Funktion, um den öffentlichen Schlüssel in PEM-Format zu exportieren
|
||||
func exportPublicKeyToPEM(publicKey *rsa.PublicKey) string {
|
||||
publicKeyBytes, err := x509.MarshalPKIXPublicKey(publicKey)
|
||||
if err != nil {
|
||||
log.Fatalf("Fehler beim Exportieren des öffentlichen Schlüssels: %v", err)
|
||||
}
|
||||
publicKeyPEM := pem.EncodeToMemory(&pem.Block{
|
||||
Type: "PUBLIC KEY",
|
||||
Bytes: publicKeyBytes,
|
||||
})
|
||||
return string(publicKeyPEM)
|
||||
}
|
24
main.go
24
main.go
@@ -1 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.send.nrw/sendnrw/sendnrwlib/netcrypto"
|
||||
"git.send.nrw/sendnrw/sendnrwlib/netproto"
|
||||
)
|
||||
|
||||
func main() {
|
||||
key, _ := netcrypto.AES_GenerateKey()
|
||||
fmt.Println(netcrypto.AES_KeyToString(key))
|
||||
enc, _ := netcrypto.AES_Encrypt("test", key)
|
||||
fmt.Println(enc)
|
||||
dec, _ := netcrypto.AES_Decrypt(enc, key)
|
||||
fmt.Println(dec)
|
||||
|
||||
a := netproto.GenerateNetworkProtocol(netproto.GenerateMainHeader(1, "A", netproto.GenerateEHT1("127.0.0.1", "", ""), netproto.GenerateEHT2("8.8.8.8", "", "80"), netproto.GenerateEHT3("JSON", "0", "0")), "")
|
||||
fmt.Println(a)
|
||||
b, _ := netproto.ToJSON(a)
|
||||
fmt.Println(b)
|
||||
netproto.FromJSON(b, &a)
|
||||
fmt.Println(a)
|
||||
|
||||
}
|
||||
|
212
netcrypto/netcrypto.go
Normal file
212
netcrypto/netcrypto.go
Normal file
@@ -0,0 +1,212 @@
|
||||
package netcrypto
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/sha256"
|
||||
"crypto/x509"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
)
|
||||
|
||||
// HashToken hashes a token using SHA-256.
|
||||
func HashToken(token string) string {
|
||||
hash := sha256.Sum256([]byte(token))
|
||||
return hex.EncodeToString(hash[:])
|
||||
}
|
||||
|
||||
// VerifyToken compares a plaintext token with a stored hash.
|
||||
func VerifyToken(token, storedHash string) bool {
|
||||
return HashToken(token) == storedHash
|
||||
}
|
||||
|
||||
// Generiert einen zufälligen 32-Byte-Schlüssel für AES-256
|
||||
func AES_GenerateKey() ([]byte, error) {
|
||||
key := make([]byte, 32) // 32 Bytes für AES-256
|
||||
_, err := rand.Read(key)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Fehler beim Generieren des Schlüssels: %v", err)
|
||||
}
|
||||
return key, nil
|
||||
}
|
||||
|
||||
func AES_KeyToString(key []byte) string {
|
||||
return base64.StdEncoding.EncodeToString(key)
|
||||
}
|
||||
|
||||
func AES_StringToKey(base64Key string) ([]byte, error) {
|
||||
key, err := base64.StdEncoding.DecodeString(base64Key)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Fehler beim Decodieren des Schlüssels: %v", err)
|
||||
}
|
||||
return key, nil
|
||||
}
|
||||
|
||||
// Verschlüsselt einen JSON-String mit AES-GCM
|
||||
func AES_Encrypt(jsonString string, key []byte) (string, error) {
|
||||
// Erstelle einen AES-Block
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Fehler beim Erstellen des AES-Blocks: %v", err)
|
||||
}
|
||||
|
||||
// AES-GCM-Modus initialisieren
|
||||
aesGCM, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Fehler beim Erstellen von AES-GCM: %v", err)
|
||||
}
|
||||
|
||||
// Generiere eine zufällige Nonce (einmaliger Wert)
|
||||
nonce := make([]byte, aesGCM.NonceSize())
|
||||
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
|
||||
return "", fmt.Errorf("Fehler beim Generieren der Nonce: %v", err)
|
||||
}
|
||||
|
||||
// JSON-Daten verschlüsseln
|
||||
ciphertext := aesGCM.Seal(nil, nonce, []byte(jsonString), nil)
|
||||
|
||||
// Nonce und Ciphertext zusammenfügen und Base64-kodieren
|
||||
result := append(nonce, ciphertext...)
|
||||
return base64.StdEncoding.EncodeToString(result), nil
|
||||
}
|
||||
|
||||
// Entschlüsselt einen verschlüsselten JSON-String mit AES-GCM
|
||||
func AES_Decrypt(encryptedString string, key []byte) (string, error) {
|
||||
// Base64-Dekodierung
|
||||
data, err := base64.StdEncoding.DecodeString(encryptedString)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Fehler beim Base64-Dekodieren: %v", err)
|
||||
}
|
||||
|
||||
// Erstelle einen AES-Block
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Fehler beim Erstellen des AES-Blocks: %v", err)
|
||||
}
|
||||
|
||||
// AES-GCM-Modus initialisieren
|
||||
aesGCM, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Fehler beim Erstellen von AES-GCM: %v", err)
|
||||
}
|
||||
|
||||
// Extrahiere die Nonce und den Ciphertext
|
||||
nonceSize := aesGCM.NonceSize()
|
||||
if len(data) < nonceSize {
|
||||
return "", errors.New("Ungültige verschlüsselte Daten")
|
||||
}
|
||||
nonce, ciphertext := data[:nonceSize], data[nonceSize:]
|
||||
|
||||
// JSON-Daten entschlüsseln
|
||||
plaintext, err := aesGCM.Open(nil, nonce, ciphertext, nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Fehler beim Entschlüsseln: %v", err)
|
||||
}
|
||||
|
||||
return string(plaintext), nil
|
||||
}
|
||||
|
||||
func RSA_GenerateKeyPair() (string, string) {
|
||||
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||
if err != nil {
|
||||
log.Fatalf("Fehler beim Generieren des privaten Schlüssels: %v", err)
|
||||
}
|
||||
publicKey := &privateKey.PublicKey
|
||||
exportPrivateKeyToPEM(privateKey)
|
||||
exportPublicKeyToPEM(publicKey)
|
||||
return exportPrivateKeyToPEM(privateKey), exportPublicKeyToPEM(publicKey)
|
||||
}
|
||||
|
||||
func RSA_Encrypt(PublicKey, PlainText string) string {
|
||||
PuK, err1 := importPublicKeyFromPEM(PublicKey)
|
||||
if err1 != nil {
|
||||
log.Fatalf("Fehler beim Importieren des öffentlichen Schlüssels: %v", err1)
|
||||
}
|
||||
encryptedBytes, err := rsa.EncryptOAEP(sha256.New(), rand.Reader, PuK, []byte(PlainText), nil)
|
||||
if err != nil {
|
||||
log.Fatalf("Fehler beim Verschlüsseln: %v", err)
|
||||
}
|
||||
encryptedString := base64.StdEncoding.EncodeToString(encryptedBytes)
|
||||
return encryptedString
|
||||
}
|
||||
|
||||
func RSA_Decrypt(PrivateKey, EncryptedText string) string {
|
||||
PrK, err1 := importPrivateKeyFromPEM(PrivateKey)
|
||||
if err1 != nil {
|
||||
log.Fatalf("Fehler beim Importieren des privaten Schlüssels: %v", err1)
|
||||
}
|
||||
encryptedstring, err2 := base64.StdEncoding.DecodeString(EncryptedText)
|
||||
if err2 != nil {
|
||||
log.Fatalf("Fehler beim Decodieren des verschlüsselten Textes: %v", err2)
|
||||
}
|
||||
encryptedBytes := []byte(encryptedstring)
|
||||
decryptedBytes, err := rsa.DecryptOAEP(sha256.New(), rand.Reader, PrK, encryptedBytes, nil)
|
||||
if err != nil {
|
||||
log.Fatalf("Fehler beim Entschlüsseln: %v", err)
|
||||
}
|
||||
decryptedString := string(decryptedBytes)
|
||||
return decryptedString
|
||||
}
|
||||
|
||||
// Importiere einen privaten Schlüssel aus einem PEM-String
|
||||
func importPrivateKeyFromPEM(pemString string) (*rsa.PrivateKey, error) {
|
||||
block, _ := pem.Decode([]byte(pemString))
|
||||
if block == nil || block.Type != "RSA PRIVATE KEY" {
|
||||
return nil, errors.New("Ungültiges PEM-Format oder kein privater Schlüssel")
|
||||
}
|
||||
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return privateKey, nil
|
||||
}
|
||||
|
||||
// Importiere einen öffentlichen Schlüssel aus einem PEM-String
|
||||
func importPublicKeyFromPEM(pemString string) (*rsa.PublicKey, error) {
|
||||
block, _ := pem.Decode([]byte(pemString))
|
||||
if block == nil || block.Type != "PUBLIC KEY" {
|
||||
return nil, errors.New("Ungültiges PEM-Format oder kein öffentlicher Schlüssel")
|
||||
}
|
||||
publicKey, err := x509.ParsePKIXPublicKey(block.Bytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Überprüfe den Typ des Schlüssels
|
||||
switch pub := publicKey.(type) {
|
||||
case *rsa.PublicKey:
|
||||
return pub, nil
|
||||
default:
|
||||
return nil, errors.New("Öffentlicher Schlüssel ist kein RSA-Schlüssel")
|
||||
}
|
||||
}
|
||||
|
||||
// Funktion, um den privaten Schlüssel in PEM-Format zu exportieren
|
||||
func exportPrivateKeyToPEM(privateKey *rsa.PrivateKey) string {
|
||||
privateKeyBytes := x509.MarshalPKCS1PrivateKey(privateKey)
|
||||
privateKeyPEM := pem.EncodeToMemory(&pem.Block{
|
||||
Type: "RSA PRIVATE KEY",
|
||||
Bytes: privateKeyBytes,
|
||||
})
|
||||
return string(privateKeyPEM)
|
||||
}
|
||||
|
||||
// Funktion, um den öffentlichen Schlüssel in PEM-Format zu exportieren
|
||||
func exportPublicKeyToPEM(publicKey *rsa.PublicKey) string {
|
||||
publicKeyBytes, err := x509.MarshalPKIXPublicKey(publicKey)
|
||||
if err != nil {
|
||||
log.Fatalf("Fehler beim Exportieren des öffentlichen Schlüssels: %v", err)
|
||||
}
|
||||
publicKeyPEM := pem.EncodeToMemory(&pem.Block{
|
||||
Type: "PUBLIC KEY",
|
||||
Bytes: publicKeyBytes,
|
||||
})
|
||||
return string(publicKeyPEM)
|
||||
}
|
@@ -2,6 +2,7 @@ package netproto
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
type MainHeader struct {
|
||||
@@ -19,12 +20,25 @@ const (
|
||||
ExtensionHeaderType_Destination uint8 = 2 /* https://git.send.nrw/sendnrw/sendnrwlib/issues/1 */
|
||||
ExtensionHeaderType_PayloadDefinition uint8 = 3
|
||||
ExtensionHeaderType_Protocol uint8 = 4
|
||||
ExtensionHeaderType_Control uint8 = 5 /*Steuerbefehle wie Download, Upload, ...*/
|
||||
ExtensionHeaderType_Control uint8 = 5 /*Syn/SynAck/Ack/AckFin/Fin*/
|
||||
ExtensionHeaderType_Crypto uint8 = 6 /*Hinweis auf eine Verschlüsselung*/
|
||||
ExtensionHeaderType_Validation uint8 = 7 /*Hinweis auf eine Validierung oder Checksum*/
|
||||
ExtensionHeaderType_Routing uint8 = 8 /*Hinweis auf eine Routing-Information*/
|
||||
)
|
||||
|
||||
const (
|
||||
ControlFlag_Flow_SYN string = "SYN"
|
||||
ControlFlag_Flow_SYNACK string = "SYNACK"
|
||||
ControlFlag_Flow_ACK string = "ACK"
|
||||
ControlFlag_Flow_ACKFIN string = "ACKFIN"
|
||||
ControlFlag_Flow_FIN string = "FIN"
|
||||
ControlFlag_Encryption_INSECURE string = "INSECURE"
|
||||
ControlFlag_Encryption_STAYINSECURE string = "STAYINSECURE"
|
||||
ControlFlag_Encryption_SECURESTART string = "SECURESTART"
|
||||
ControlFlag_Encryption_SECUREACCEPT string = "SECUREACCEPT"
|
||||
ControlFlag_Encryption_SECUREFAIL string = "SECUREFAIL"
|
||||
)
|
||||
|
||||
func GenerateEHT1(IPv4, IPv6, Port string) ExtensionHeader {
|
||||
return ExtensionHeader{
|
||||
Type: ExtensionHeaderType_Source,
|
||||
@@ -57,6 +71,22 @@ func GenerateEHT4(ProtocolName, ProtocolVersion, ProtocolOID string) ExtensionHe
|
||||
}
|
||||
}
|
||||
|
||||
func GenerateEHT5(ControlFlagFlow, ControlFlagEncryption string) ExtensionHeader {
|
||||
return ExtensionHeader{
|
||||
Type: ExtensionHeaderType_Control,
|
||||
Meta: map[string]string{"version": "1.0"},
|
||||
Data: map[string]string{"controlflagflow": ControlFlagFlow, "controlflagencryption": ControlFlagEncryption},
|
||||
}
|
||||
}
|
||||
|
||||
func GenerateEHT6(PublicKey string) ExtensionHeader {
|
||||
return ExtensionHeader{
|
||||
Type: ExtensionHeaderType_Crypto,
|
||||
Meta: map[string]string{"version": "1.0"},
|
||||
Data: map[string]string{"publickey": PublicKey},
|
||||
}
|
||||
}
|
||||
|
||||
func GenerateEHT7(Checksum string) ExtensionHeader {
|
||||
return ExtensionHeader{
|
||||
Type: ExtensionHeaderType_Validation,
|
||||
@@ -65,18 +95,26 @@ func GenerateEHT7(Checksum string) ExtensionHeader {
|
||||
}
|
||||
}
|
||||
|
||||
func GenerateMH(Version uint8, Flag string) MainHeader {
|
||||
func GenerateMainHeader(Version uint8, Flag string, ExH ...ExtensionHeader) MainHeader {
|
||||
return MainHeader{
|
||||
Version: Version,
|
||||
Meta: map[string]string{"version": "1.0", "flag": Flag},
|
||||
Version: Version,
|
||||
Meta: map[string]string{"version": "1.0", "flag": Flag},
|
||||
ExtensionHeaders: ExH,
|
||||
}
|
||||
}
|
||||
|
||||
func EncodeB64(input string) (string, error) {
|
||||
func GenerateNetworkProtocol(mainheader MainHeader, payload string) NetworkProtocol {
|
||||
return NetworkProtocol{
|
||||
Header: mainheader,
|
||||
Payload: payload,
|
||||
}
|
||||
}
|
||||
|
||||
func EncodeBase64(input string) (string, error) {
|
||||
return base64.StdEncoding.EncodeToString([]byte(input)), nil
|
||||
}
|
||||
|
||||
func DecodeB64(input string) (string, error) {
|
||||
func DecodeBase64(input string) (string, error) {
|
||||
d, err := base64.StdEncoding.DecodeString(input)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -84,6 +122,18 @@ func DecodeB64(input string) (string, error) {
|
||||
return string(d), nil
|
||||
}
|
||||
|
||||
func ToJSON(input interface{}) (string, error) {
|
||||
b, err := json.Marshal(input)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(b), nil
|
||||
}
|
||||
|
||||
func FromJSON(input string, output interface{}) error {
|
||||
return json.Unmarshal([]byte(input), output)
|
||||
}
|
||||
|
||||
type ExtensionHeader struct {
|
||||
Type uint8 `json:"type"`
|
||||
Meta map[string]string `json:"meta"`
|
||||
|
Reference in New Issue
Block a user