Git Product home page Git Product logo

Comments (7)

GuoHuaijian avatar GuoHuaijian commented on July 17, 2024

// 文件加密
function encryptFile(inputPath, outputPath, key) {
const input = fs.readFileSync(inputPath);
const encryptedData = sm4.encrypt(input, key, { mode: 'ecb', padding: 'pkcs#7' });
fs.writeFileSync(outputPath, encryptedData, 'hex');
}

// 文件解密
function decryptFile(inputPath, outputPath, key) {
const encryptedData = fs.readFileSync(inputPath, 'hex');
const decryptedData = sm4.decrypt(encryptedData, key, { mode: 'ecb', padding: 'pkcs#7' });
fs.writeFileSync(outputPath, decryptedData, 'utf8');
}

from sm-crypto.

Blank0120 avatar Blank0120 commented on July 17, 2024

我用你的代码跑目前完全可以

const sm4 = require('sm-crypto').sm4;
const fs = require('fs');

// 文件加密
function encryptFile(inputPath, outputPath, key) {
	const input = fs.readFileSync(inputPath);
	const encryptedData = sm4.encrypt(input, key, { mode: 'ecb', padding: 'pkcs#7' });
	fs.writeFileSync(outputPath, encryptedData, 'hex');
}

// 文件解密
function decryptFile(inputPath, outputPath, key) {
	const encryptedData = fs.readFileSync(inputPath, 'hex');
	const decryptedData = sm4.decrypt(encryptedData, key, { mode: 'ecb', padding: 'pkcs#7' });
	fs.writeFileSync(outputPath, decryptedData, 'utf8');
}

encryptFile('./test-sm-crypto.txt', './test-sm-crypto-encrypted.txt', '11223344556677881122334455667788');

decryptFile('./test-sm-crypto-encrypted.txt', './test-sm-crypto-decrypted.txt', '11223344556677881122334455667788');

from sm-crypto.

GuoHuaijian avatar GuoHuaijian commented on July 17, 2024

txt文件是可以的 其他格式不行 @Blank0120

from sm-crypto.

Blank0120 avatar Blank0120 commented on July 17, 2024

txt文件是可以的 其他格式不行 @Blank0120

你说的对,png, jpg解密会遇到非char的值超过unicode码表了,导致报错。

from sm-crypto.

changhr2013 avatar changhr2013 commented on July 17, 2024

二进制文件需要读取/写入字节数组,本身库是支持传入字节数组的:

import SmCrypto from "sm-crypto";
import fs from "fs";

const{sm4}= SmCrypto;


const filePath = '/Downloads/20221118160713.png';
const outputFilePath = '/Downloads/20221118160713-enc';
const decFilePath = '/Downloads/20221118160713-dec.png';
let key = "f4320510c51fda88e6d1cc94b72bb2b8";

encryptFile(filePath, outputFilePath, key);
decryptFile(outputFilePath, decFilePath, key);

// 文件加密
function encryptFile(inputPath, outputPath, key) {
    const input = readBinaryFileSync(inputPath);
    const encryptedData = sm4.encrypt(input, key, {mode: 'ecb', padding: 'pkcs#7',output: 'array'});
    writeBinaryFileSync(outputPath, encryptedData);
}

// 文件解密
function decryptFile(inputPath, outputPath, key) {
    const encryptedData = readBinaryFileSync(inputPath);
    const decryptedData = sm4.decrypt(encryptedData, key, {mode: 'ecb', padding: 'pkcs#7', output:'array'});
    writeBinaryFileSync(outputPath, decryptedData);
}

function readBinaryFileSync(filePath) {
    try {
        const data = fs.readFileSync(filePath);
        const byteArray = [...data];
        return byteArray;
    } catch (err) {
        console.error('Error reading binary file:', err);
        return null;
    }
}

function writeBinaryFileSync(filePath, byteArray) {
    try {
        const buffer = Buffer.from(byteArray);
        fs.writeFileSync(filePath, buffer);
        console.log('Binary file written successfully.');
    } catch (err) {
        console.error('Error writing binary file:', err);
    }
}

from sm-crypto.

GuoHuaijian avatar GuoHuaijian commented on July 17, 2024

@changhr2013 谢谢 已经解决了 因为需求需要加密超大文件所以这个库并不适合流的方式读取加密 需要自己单独处理每块填充pkcs#7格式

from sm-crypto.

JuneAndGreen avatar JuneAndGreen commented on July 17, 2024

基于 TypedArray 的实现可以参考此实现:https://github.com/Cubelrti/sm-crypto-v2

from sm-crypto.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.