Comments (7)
// 文件加密
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.
我用你的代码跑目前完全可以
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.
txt文件是可以的 其他格式不行 @Blank0120
from sm-crypto.
txt文件是可以的 其他格式不行 @Blank0120
你说的对,png, jpg解密会遇到非char的值超过unicode码表了,导致报错。
from sm-crypto.
二进制文件需要读取/写入字节数组,本身库是支持传入字节数组的:
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.
@changhr2013 谢谢 已经解决了 因为需求需要加密超大文件所以这个库并不适合流的方式读取加密 需要自己单独处理每块填充pkcs#7格式
from sm-crypto.
基于 TypedArray 的实现可以参考此实现:https://github.com/Cubelrti/sm-crypto-v2
from sm-crypto.
Related Issues (20)
- sm4 加密报错
- 在使用代码混淆后,无法加解密 HOT 1
- vue项目,计算视频文件SM3,大一点的就崩溃 HOT 5
- SM2解密 后台采用hutool,密文加了04,但是还是偶发解密不了,报错InvalidCipherTextException: invalid cipher text HOT 1
- IE 出现兼容问题,建议修改 packjson 的main的路径 HOT 1
- 提供一个高性能的Java版,采用了BC库
- 一些其他的版本实现,方便取用 HOT 3
- sm4.encrypt的key要求是128比特,可是demo那里的字符是32位。 HOT 5
- sm4.encrypt的key要求是128比特,可是demo那里的字符是32位。
- uniapp 提示未定义 HOT 4
- sm4密钥 HOT 7
- hi, 这个工具又从证书获取公钥或私钥的方法吗? HOT 7
- 什么时候支持ts呀? HOT 1
- 请问能支持国密csr生成吗? HOT 4
- 使用前端进行签名后,使用golang后端进行验签,发现验证失败。 HOT 1
- 鸿蒙OS SM2签名中文,后端验签失败,去掉中文就可以验签通过 HOT 1
- sm3 hmac 算出来的结果和 crypto 算出来的结果不一样 HOT 1
- iv is invalid HOT 3
- 0.2版本的sm2.doVerifySignature能通过,0.3版本的不能
- 国密标准OID疑问
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sm-crypto.