Golang шифрует данные с использованием AES - PullRequest
0 голосов
/ 22 июня 2019

Я не уверен, что это правильное место, чтобы спросить это.Но у меня нет опыта работы с C #, и мне было поручено преобразовать часть кода безопасности в Golang

Мне было интересно, не пропускаю ли я что-то здесь.

Код C # использует класс Rijndael для шифрования части данных.Значение key и значение iv записываются в байт-код следующим образом:

   public static byte[] Key = new byte[]{0xx, 0xx, 0xx, 0xx, 0xx,
                    0xx4, 0xxx, 0xxx, 0xxx, 0xxx, xxx, 0xxx,
                    0xxx, 0xxx, 0xxx, 0xxx};

public static byte[] IV = new byte[] // save structure as above with 16 in length

, а затем появляется бит кода, который выполняет это

Rijndael alg = Rijndael.Create();
                alg.Key = Key;
                alg.IV = IV;
                CryptoStream cs = new CryptoStream(ms,
                alg.CreateEncryptor(), CryptoStreamMode.Write);
                cs.Write(dataWithoutHeader, 0, dataWithoutHeader.Length);
                cs.Close();

функция отправляет byte[] data как вывод

Я пытаюсь имитировать это golang вот так

func StartEncryption(message []byte) []byte {
    var key = []byte {// same as C# } 

    var iv = []byte{ // same as C# }

    var err error
    fmt.Printf("\n length of key %+v \n, \n length of iv \n %+v \n", len(key), len(iv))
    // Encrypt
    encrypted := make([]byte, len(message))
    err = EncryptAESCFB(encrypted, []byte(message), key, iv)
    if err != nil {
        panic(err)
    }
    return encrypted
}

Функция шифрования

func EncryptAESCFB(dst, src, key, iv []byte) error {
    aesBlockEncrypter, err := aes.NewCipher([]byte(key))
    if err != nil {
        return err
    }
    aesEncrypter := cipher.NewCFBEncrypter(aesBlockEncrypter, iv)
    aesEncrypter.XORKeyStream(dst, src)
    return nil
}

Выходные данные отправляются через API, чьи выходные данные необходимо расшифровать.Я использую это ниже

func decryptMessage(message []byte)error{
    var key = []byte{ // same as C# }

    var iv = []byte{ // same as C#  }

    // Remove the head part of the response (45 bytes)
    responseBody := message[45:]

    decrypted := make([]byte, len(responseBody))

    err := DecryptAESCFB(decrypted, responseBody, key, iv)

    if err != nil {
        fmt.Printf("\n error : \n %+v \n", err)
    }
    return nil
}

func DecryptAESCFB(dst, src, key, iv []byte) error {
    aesBlockDecrypter, err := aes.NewCipher([]byte(key))
    if err != nil {
        return nil
    }
    aesDecrypter := cipher.NewCFBDecrypter(aesBlockDecrypter, iv)
    aesDecrypter.XORKeyStream(dst, src)
    return nil
}

Расшифровщик дает мне тарабарщину - я где-то ошибаюсь?

Мой вопрос сводится к 2 вопросам

  1. Будет ли функция C # с использованием класса rijndael и функции golang выдавать один и тот же вывод или я должен делать что-то большее / меньшее

  2. Является ли байтовый массив подходящими данными для хранения?ключ, IV in - то есть он не тот, который используется в C # при копировании в GO

1 Ответ

1 голос
/ 30 июня 2019

Есть несколько проблем с кодом, который вы разместили.

  1. Не храните ключ в байтовом массиве, потому что это означает, что вы жестко его кодируете. Вместо этого сгенерируйте случайный 256-битный ключ, закодируйте его в шестнадцатеричную строку, затем сохраните вне вашей программы и прочитайте его, используя библиотеку конфигурации, такую ​​как viper .
  2. Не кодируйте IV. Вы должны генерировать новый IV для каждого сообщения. Повторное использование того же IV значительно ослабляет ваше шифрование. Для каждого зашифрованного сообщения создайте случайный IV и добавьте его к сообщению. Когда вы пытаетесь расшифровать его, прочитайте IV с первых n байтов, а затем расшифруйте.
  3. Вы должны использовать аутентифицированное шифрование в качестве меры защиты от выбранных атак зашифрованного текста. Режим GCM обеспечивает аутентификацию для вас.

Вот пример. Playground Link

package main

import (
    "crypto/aes"
    "crypto/cipher"
    "crypto/rand"
    "encoding/hex"
    "fmt"
    "os"
)

var (
    key       = randBytes(256 / 8)
    gcm       cipher.AEAD
    nonceSize int
)

// Initilze GCM for both encrypting and decrypting on program start.
func init() {
    block, err := aes.NewCipher(key)
    if err != nil {
        fmt.Printf("Error reading key: %s\n", err.Error())
        os.Exit(1)
    }

    fmt.Printf("Key: %s\n", hex.EncodeToString(key))

    gcm, err = cipher.NewGCM(block)
    if err != nil {
        fmt.Printf("Error initializing AEAD: %s\n", err.Error())
        os.Exit(1)
    }

    nonceSize = gcm.NonceSize()
}

func randBytes(length int) []byte {
    b := make([]byte, length)
    rand.Read(b)
    return b
}

func encrypt(plaintext []byte) (ciphertext []byte) {
    nonce := randBytes(nonceSize)
    c := gcm.Seal(nil, nonce, plaintext, nil)
    return append(nonce, c...)
}

func decrypt(ciphertext []byte) (plaintext []byte, err error) {
    if len(ciphertext) < nonceSize {
        return nil, fmt.Errorf("Ciphertext too short.")
    }
    nonce := ciphertext[0:nonceSize]
    msg := ciphertext[nonceSize:]
    return gcm.Open(nil, nonce, msg, nil)
}

func main() {
    fmt.Println("Encrypting...")
    msg := []byte("The quick brown fox jumped over the lazy dog.")
    ciphertext := encrypt(msg)
    fmt.Printf("Encrypted message: %v\n", ciphertext)

    fmt.Println("Decrypting...")
    plaintext, err := decrypt(ciphertext)
    if err != nil {
        // Don't display this message to the end-user, as it could potentially
        // give an attacker useful information. Just tell them something like "Failed to decrypt."
        fmt.Printf("Error decryping message: %s\n", err.Error())
        os.Exit(1)
    }
    fmt.Printf("Decrypted message: %s\n", string(plaintext))
}
...