recherche

Maison  >  Questions et réponses  >  le corps du texte

node.js - Comment décrypter des fichiers cryptés par Golang avec nodejs?

J'ai utilisé Golang pour implémenter le cryptage et le décryptage de fichiers, mais je ne sais pas comment utiliser nodejs pour implémenter le décryptage Golang.
code golang :

package main

import (
    "bytes"
    "crypto/aes"
    "crypto/cipher"
    "crypto/rand"
    "io"
    "io/ioutil"
    "os"
)

func encrypt(aeskey string, filename string) {
    plaintext, err := ioutil.ReadFile(filename)
    if err != nil {
        panic(err.Error())
    }

    // Byte array of the string
    key := []byte(aeskey)

    // Create the AES cipher
    block, err := aes.NewCipher(key)
    if err != nil {
        panic(err)
    }

    // The IV needs to be unique, but not secure. Therefore it's common to
    // include it at the beginning of the ciphertext.
    ciphertext := make([]byte, aes.BlockSize+len(plaintext))
    iv := ciphertext[:aes.BlockSize]
    if _, err := io.ReadFull(rand.Reader, iv); err != nil {
        panic(err)
    }

    stream := cipher.NewCFBEncrypter(block, iv)
    stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)

    // create a new file for saving the encrypted data.
    f, err := os.Create(filename + ".aes")
    if err != nil {
        panic(err.Error())
    }
    _, err = io.Copy(f, bytes.NewReader(ciphertext))
    if err != nil {
        panic(err.Error())
    }
}
func main() {
    key := "0123456789123456"
    encrypt(key, "1.ts")
}

Comment les fichiers cryptés par le code golang ci-dessus peuvent-ils être analysés à l'aide de nodejs ?

Je ne sais pas si la conversion vers nodejs se concentre principalement sur quelques octets de traitement golang. Je l'ai écrit moi-même, mais c'était faux. .

var fs = require('fs');
var crypto = require('crypto');

function decrypt(aseKey, inputFile){

    var fileBody = fs.readFileSync(inputFile)
    var  cipher =  crypto.createDecipheriv("aes128", Buffer.from(aseKey), fileBody.slice(0,16))
    var result = cipher.update(fileBody.slice(16))
    fs.writeFileSync(inputFile+".n.ts", result)
}

decrypt("0123456789123456", "1.ts.aes")

Ci-joint la fonction golang pour décrypter ce fichier

func decrypt(aesKey string, inputFile string) {

    ciphertext, err := ioutil.ReadFile(inputFile)
    if err != nil {
        panic(err.Error())
    }

    // Key
    key := []byte(aesKey)

    // Create the AES cipher
    block, err := aes.NewCipher(key)
    if err != nil {
        panic(err)
    }

    // Before even testing the decryption,
    // if the text is too small, then it is incorrect
    if len(ciphertext) < aes.BlockSize {
        panic("Text is too short")
    }

    // Get the 16 byte IV
    iv := ciphertext[:aes.BlockSize]

    // Remove the IV from the ciphertext
    ciphertext = ciphertext[aes.BlockSize:]

    // Return a decrypted stream
    stream := cipher.NewCFBDecrypter(block, iv)
    // Decrypt bytes from ciphertext
    stream.XORKeyStream(ciphertext, ciphertext)
    // create a new file for saving the encrypted data.
    f, err := os.Create(inputFile + ".ts")
    if err != nil {
        panic(err.Error())
    }
    _, err = io.Copy(f, bytes.NewReader(ciphertext))
    if err != nil {
        panic(err.Error())
    }
}

func main() {
    key := "0123456789123456"
    decrypt(key, "1.ts.aes")
}
PHP中文网PHP中文网2804 Il y a quelques jours1270

répondre à tous(3)je répondrai

  • phpcn_u1582

    phpcn_u15822017-05-31 10:41:35

    https://github.com/danielecr/aes128encrypter

    répondre
    0
  • 我想大声告诉你

    我想大声告诉你2017-05-31 10:41:35

    Le code suivant utilise AES192, similaire à 128. Vous pouvez cliquer pour afficher la source du code source.

    var crypto = require('crypto');
    var key = crypto.randomBytes(192/8);  // 替换成自己需要的key
    var iv = crypto.randomBytes(128/8);  // 替换成自己需要的iv
    var algorithm = 'aes192';
    
    function encrypt(text){
        var cipher = crypto.createCipheriv(algorithm, key, iv);
        cipher.update(text);
        return cipher.final('hex');
    }
    
    function decrypt(encrypted){
        var decipher = crypto.createDecipheriv(algorithm, key, iv);
        decipher.update(encrypted, 'hex');
        return decipher.final('utf8');
    }
    
    var content = 'hello';
    var crypted = encrypt('hello');
    console.log( crypted );  // 输出:1b87be446405ff910cd280ae6aa0423f
    
    var decrypted = decrypt( crypted );
    console.log( decrypted );  // 输出:hello

    répondre
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-31 10:41:35

    var fs = require('fs');
    var crypto = require('crypto');
    function decrypt(aseKey, inputFile){
        var fileBody = fs.readFileSync(inputFile)
        var  decipher =  crypto.createDecipheriv("aes-128-cfb",new Buffer(aseKey) , fileBody.slice(0,16))
        var recv = decipher.update(fileBody.slice(16))
        fs.writeFileSync(inputFile + ".n.ts", recv)
    }
    
    decrypt("0123456789123456", "1.ts.aes")

    répondre
    0
  • Annulerrépondre