how to transform encrypt function from golang to nodejs

huangapple go评论116阅读模式
英文:

how to transform encrypt function from golang to nodejs

问题

我使用golang编写了一个文件加密函数,但我不知道如何使用nodejs实现它。

以下是你要翻译的内容:

我使用golang编写了一个文件加密函数,但我不知道如何使用nodejs实现它。

  1. package main
  2. import (
  3. "bytes"
  4. "crypto/aes"
  5. "crypto/cipher"
  6. "crypto/rand"
  7. "io"
  8. "io/ioutil"
  9. "os"
  10. )
  11. func encrypt(aeskey string, filename string) {
  12. plaintext, err := ioutil.ReadFile(filename)
  13. if err != nil {
  14. panic(err.Error())
  15. }
  16. // 字符串的字节数组
  17. key := []byte(aeskey)
  18. // 创建AES密码器
  19. block, err := aes.NewCipher(key)
  20. if err != nil {
  21. panic(err)
  22. }
  23. // IV需要是唯一的,但不需要保密。因此,通常将其包含在密文的开头。
  24. ciphertext := make([]byte, aes.BlockSize+len(plaintext))
  25. iv := ciphertext[:aes.BlockSize]
  26. if _, err := io.ReadFull(rand.Reader, iv); err != nil {
  27. panic(err)
  28. }
  29. stream := cipher.NewCFBEncrypter(block, iv)
  30. stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
  31. // 创建一个新文件来保存加密数据。
  32. f, err := os.Create(filename + ".aes")
  33. if err != nil {
  34. panic(err.Error())
  35. }
  36. _, err = io.Copy(f, bytes.NewReader(ciphertext))
  37. if err != nil {
  38. panic(err.Error())
  39. }
  40. }
  41. func decrypt(aesKey string, inputFile string) {
  42. ciphertext, err := ioutil.ReadFile(inputFile)
  43. if err != nil {
  44. panic(err.Error())
  45. }
  46. // 密钥
  47. key := []byte(aesKey)
  48. // 创建AES密码器
  49. block, err := aes.NewCipher(key)
  50. if err != nil {
  51. panic(err)
  52. }
  53. // 在测试解密之前,
  54. // 如果文本太短,则不正确
  55. if len(ciphertext) < aes.BlockSize {
  56. panic("Text is too short")
  57. }
  58. // 获取16字节的IV
  59. iv := ciphertext[:aes.BlockSize]
  60. // 从密文中删除IV
  61. ciphertext = ciphertext[aes.BlockSize:]
  62. // 返回解密流
  63. stream := cipher.NewCFBDecrypter(block, iv)
  64. // 从密文中解密字节
  65. stream.XORKeyStream(ciphertext, ciphertext)
  66. // 创建一个新文件来保存解密数据。
  67. f, err := os.Create(inputFile + ".ts")
  68. if err != nil {
  69. panic(err.Error())
  70. }
  71. _, err = io.Copy(f, bytes.NewReader(ciphertext))
  72. if err != nil {
  73. panic(err.Error())
  74. }
  75. }
  76. func main() {
  77. key := "0123456789123456"
  78. encrypt(key, "1.ts")
  79. decrypt(key, "1.ts.aes")
  80. }

实际上,我对以下部分有些困惑:

  1. ciphertext := make([]byte, aes.BlockSize+len(plaintext))
  2. iv := ciphertext[:aes.BlockSize]

我使用node编写了一个解密函数,但它不能正常工作:

  1. var fs = require('fs');
  2. var crypto = require('crypto');
  3. function decrypt(aseKey, inputFile){
  4. var buffer = fs.readFileSync(inputFile)
  5. var arr = Array.prototype.slice.call(buffer, 0)
  6. var iv = arr.slice(0, 16)
  7. var bodyBytes = arr.slice(16)
  8. var cipher = crypto.createCipheriv('aes-128-cbc', aseKey, new Buffer(iv));
  9. buffer = cipher.update(new Buffer(bodyBytes));
  10. fs.writeFile(inputFile + ".node.ts", buffer)
  11. }
  12. decrypt("0123456789123456", "1.ts.aes")

谢谢你的帮助!

英文:

I write a encrypt file function use golang, but I don't how to implement it use nodejs

  1. package main
  2. import (
  3. &quot;bytes&quot;
  4. &quot;crypto/aes&quot;
  5. &quot;crypto/cipher&quot;
  6. &quot;crypto/rand&quot;
  7. &quot;io&quot;
  8. &quot;io/ioutil&quot;
  9. &quot;os&quot;
  10. )
  11. func encrypt(aeskey string, filename string) {
  12. plaintext, err := ioutil.ReadFile(filename)
  13. if err != nil {
  14. panic(err.Error())
  15. }
  16. // Byte array of the string
  17. key := []byte(aeskey)
  18. // Create the AES cipher
  19. block, err := aes.NewCipher(key)
  20. if err != nil {
  21. panic(err)
  22. }
  23. // The IV needs to be unique, but not secure. Therefore it&#39;s common to
  24. // include it at the beginning of the ciphertext.
  25. ciphertext := make([]byte, aes.BlockSize+len(plaintext))
  26. iv := ciphertext[:aes.BlockSize]
  27. if _, err := io.ReadFull(rand.Reader, iv); err != nil {
  28. panic(err)
  29. }
  30. stream := cipher.NewCFBEncrypter(block, iv)
  31. stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
  32. // create a new file for saving the encrypted data.
  33. f, err := os.Create(filename + &quot;.aes&quot;)
  34. if err != nil {
  35. panic(err.Error())
  36. }
  37. _, err = io.Copy(f, bytes.NewReader(ciphertext))
  38. if err != nil {
  39. panic(err.Error())
  40. }
  41. }
  42. func decrypt(aesKey string, inputFile string) {
  43. ciphertext, err := ioutil.ReadFile(inputFile)
  44. if err != nil {
  45. panic(err.Error())
  46. }
  47. // Key
  48. key := []byte(aesKey)
  49. // Create the AES cipher
  50. block, err := aes.NewCipher(key)
  51. if err != nil {
  52. panic(err)
  53. }
  54. // Before even testing the decryption,
  55. // if the text is too small, then it is incorrect
  56. if len(ciphertext) &lt; aes.BlockSize {
  57. panic(&quot;Text is too short&quot;)
  58. }
  59. // Get the 16 byte IV
  60. iv := ciphertext[:aes.BlockSize]
  61. // Remove the IV from the ciphertext
  62. ciphertext = ciphertext[aes.BlockSize:]
  63. // Return a decrypted stream
  64. stream := cipher.NewCFBDecrypter(block, iv)
  65. // Decrypt bytes from ciphertext
  66. stream.XORKeyStream(ciphertext, ciphertext)
  67. // create a new file for saving the encrypted data.
  68. f, err := os.Create(inputFile + &quot;.ts&quot;)
  69. if err != nil {
  70. panic(err.Error())
  71. }
  72. _, err = io.Copy(f, bytes.NewReader(ciphertext))
  73. if err != nil {
  74. panic(err.Error())
  75. }
  76. }
  77. func main() {
  78. key := &quot;0123456789123456&quot;
  79. encrypt(key, &quot;1.ts&quot;)
  80. decrypt(key, &quot;1.ts.aes&quot;)
  81. }

In fact, I just have some confuse about

  1. ciphertext := make([]byte, aes.BlockSize+len(plaintext))
  2. iv := ciphertext[:aes.BlockSize]

I write a decrypt function use node ,but it don't work well:

  1. var fs = require(&#39;fs&#39;);
  2. var crypto = require(&#39;crypto&#39;);
  3. function decrypt(aseKey, inputFile){
  4. var buffer = fs.readFileSync(inputFile)
  5. var arr = Array.prototype.slice.call(buffer, 0)
  6. var iv = arr.slice(0, 16)
  7. var bodyBytes = arr.slice(16)
  8. var cipher = crypto.createCipheriv(&#39;aes-128-cbc&#39;, aseKey, new Buffer(iv));
  9. buffer = cipher.update(new Buffer(bodyBytes));
  10. fs.writeFile(inputFile + &quot;.node.ts&quot;, buffer)
  11. }
  12. decrypt(&quot;0123456789123456&quot;, &quot;1.ts.aes&quot;)

thanks for you help

答案1

得分: 1

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))

  1. fs.writeFileSync(inputFile + ".n.ts", recv)

}

英文:
  1. var fs = require(&#39;fs&#39;);
  2. var crypto = require(&#39;crypto&#39;);
  3. function decrypt(aseKey, inputFile){
  4. var fileBody = fs.readFileSync(inputFile)
  5. var decipher = crypto.createDecipheriv(&quot;aes-128-cfb&quot;,new Buffer(aseKey) , fileBody.slice(0,16))
  6. var recv = decipher.update(fileBody.slice(16))
  7. fs.writeFileSync(inputFile + &quot;.n.ts&quot;, recv)
  8. }

huangapple
  • 本文由 发表于 2017年5月27日 17:08:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/44215014.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定