String 将加密数据转换为字符串并返回
我想以字符串格式存储加密数据。不幸的是,我不能做到这一点,我不明白为什么 出于某种原因,这一行:String 将加密数据转换为字符串并返回,string,typescript,encryption,arraybuffer,String,Typescript,Encryption,Arraybuffer,我想以字符串格式存储加密数据。不幸的是,我不能做到这一点,我不明白为什么 出于某种原因,这一行: const str = new TextEncoder().encode(buff1); 创建一个比以前的Arraybuffer中包含更多字节和不同字节的字符串 此代码的最终结果是将这些记录到控制台: 阵列缓冲{bytellength:139} ArrayBuffer{ByTeleLength:262} 这是一个问题,因为它们本应相似,但事实并非如此。我尝试了很多替代方案,但所有可能的方法最终都
const str = new TextEncoder().encode(buff1);
创建一个比以前的Arraybuffer中包含更多字节和不同字节的字符串
此代码的最终结果是将这些记录到控制台:阵列缓冲{bytellength:139}
ArrayBuffer{ByTeleLength:262} 这是一个问题,因为它们本应相似,但事实并非如此。我尝试了很多替代方案,但所有可能的方法最终都会遇到同样的问题。如果有人能告诉我如何解决这个问题,我将不胜感激 请注意,EncryptText函数的工作原理与它应该的一样。我只为那些想在IDE中运行代码的人添加了它
async function encryptText(plainText, iv, password): Promise<ArrayBuffer> {
const ptUtf8 = new TextEncoder().encode(plainText);
const pwUtf8 = new TextEncoder().encode(password);
const pwHash = await crypto.subtle.digest('SHA-256', pwUtf8);
const alg = { name: 'AES-GCM', iv: iv };
const key = await crypto.subtle.importKey('raw', pwHash, alg, false, ['encrypt']);
return crypto.subtle.encrypt(alg, key, ptUtf8);
};
async function encrBuffToUtf8Test() {
const plainData = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, ' +
'sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.';
const password = 'infamous';
const randomValues = crypto.getRandomValues(new Uint8Array(12));
const encryptionResult1 = await this.encryptText(plainData, randomValues, password);
const buff1 = Buffer.from(encryptionResult1);
const str = new TextEncoder().encode(buff1);
const utf8buff = new TextDecoder('utf-8').decode(buff);
const encryptionResult2 = utf8buff.buffer;
const resTest = encryptionResult1 === encryptionResult2;
if (!resTest) {
console.log(encryptionResult1);
console.log(encryptionResult2);
}
}
异步函数encryptText(明文、iv、密码):承诺{
const ptUtf8=new textcoder().encode(纯文本);
const pwUtf8=new textcoder().encode(密码);
const pwHash=等待加密微妙摘要('SHA-256',pwUtf8);
常量alg={name:'AES-GCM',iv:iv};
const key=await crypto.minute.importKey('raw',pwHash,alg,false,['encrypt']);
返回crypto.minute.encrypt(alg,key,ptUtf8);
};
异步函数encrbufftoutp8test(){
const plainData=“知识产权”是一种对精英的尊重+
“这是一个临时的劳动和就业机会。”;
const password='臭名昭著';
const randomValues=crypto.getRandomValues(新的Uint8Array(12));
const encryptionResult1=等待此.encryptext(纯数据、随机值、密码);
const buff1=Buffer.from(encryptionResult1);
const str=new textcoder().encode(buff1);
const utf8buff=新的文本解码器('utf-8')。解码(buff);
const encryptionResult2=utf8buff.buffer;
const resTest=encryptionResult1===encryptionResult2;
如果(!resTest){
console.log(encryptionResult1);
console.log(encryptionResult2);
}
}
我想我几乎每天都在写这篇文章作为答案,但还是用标准字符编码。它根本不是那样工作的
考虑改用base64或hex。我想我几乎每天都会写这篇文章作为答案,但还是要使用标准字符编码。它根本不是那样工作的
考虑改用base64或hex。So;当我昨天问这个问题时,我已经深夜了,我对这个问题比我本该做的要沮丧得多(我在这个问题上花的时间也比我本该做的多)。我对你的回答有点直率/粗鲁,这就是我删除它们的原因。我想为我的反应道歉,请不要担心。我们都去过,谢谢你花时间回复和道歉。我一直很感激:)你最终解决了你的问题吗?我昨天遇到的部分挫折是我已经尝试了base64/hex编码。问题是,base64和十六进制编码的很多解决方案都是从一个普通字符串开始的,当你像我一样有一个加密的字节数组时,它们就不起作用了。标准的atob/btoa解决方案也不起作用。我想它只接受ASCII码,但我不确定。对于那些正在寻找实用解决方案并正在使用NodeJS的用户:const b64str=Buffer.from(encryptionResult1.toString('base64');const buff2=Buffer.from(b64str,'base64');所以当我昨天问这个问题时,我已经深夜了,我对这个问题比我本该做的要沮丧得多(我在这个问题上花的时间也比我本该做的多)。我对你的回答有点直率/粗鲁,这就是我删除它们的原因。我想为我的反应道歉,请不要担心。我们都去过,谢谢你花时间回复和道歉。我一直很感激:)你最终解决了你的问题吗?我昨天遇到的部分挫折是我已经尝试了base64/hex编码。问题是,base64和十六进制编码的很多解决方案都是从一个普通字符串开始的,当你像我一样有一个加密的字节数组时,它们就不起作用了。标准的atob/btoa解决方案也不起作用。我想它只接受ASCII码,但我不确定。对于那些正在寻找实用解决方案并正在使用NodeJS的用户:const b64str=Buffer.from(encryptionResult1.toString('base64');const buff2=Buffer.from(b64str,'base64');