基于3DES实现DES
【摘要】 最近在绿泡泡群,有开发者问到一个问题,在ArkTs里,使用 cryptoJs.库对字符串加密后的结果别的程序生成的不一致,兴趣之余做了个复测确实不一致,翻阅鸿蒙社区的示例工程,和API文档,只有3DES的实现,以学习研究为目的,我实现了一个给这位开发者使用,反馈效果还不错,特分享下。发现问题:解决问题(CryptoUtil.ets):import { cryptoFramework } fr...
最近在绿泡泡群,有开发者问到一个问题,在ArkTs里,使用 cryptoJs.库对字符串加密后的结果别的程序生成的不一致,兴趣之余做了个复测确实不一致,翻阅鸿蒙社区的示例工程,和API文档,只有3DES的实现,以学习研究为目的,我实现了一个给这位开发者使用,反馈效果还不错,特分享下。
发现问题:
解决问题(CryptoUtil.ets):
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { buffer, util } from '@kit.ArkTS';
export class CryptoUtil {
private key: cryptoFramework.SymKey;
private iv: cryptoFramework.IvParamsSpec;
private cipher: cryptoFramework.Cipher;
private decoder: cryptoFramework.Cipher;
private static instance: CryptoUtil;
private constructor(key: string, iv: string) {
this.key = this.genSymKeyByData(key);
this.iv = this.genIvParamsSpec(iv);
this.cipher = cryptoFramework.createCipher('3DES192|CBC|PKCS7');
this.decoder = cryptoFramework.createCipher('3DES192|CBC|PKCS7');
}
public static getInstance(key: string, iv: string): CryptoUtil {
if (!CryptoUtil.instance) {
CryptoUtil.instance = new CryptoUtil(key, iv)
}
return CryptoUtil.instance;
}
// 加密消息
public encryptMessageWithDESCBC(message: string): string {
if(message.length==0){
return "";
}
let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) };
this.cipher.initSync(cryptoFramework.CryptoMode.ENCRYPT_MODE, this.key, this.iv);
let encryptData = this.cipher.doFinalSync(plainText);
let base = new util.Base64Helper();
return base.encodeToStringSync(encryptData.data);
}
// 解密消息
public decryptMessageWithDESCBC(message:string|undefined): string {
if(message==undefined|| message.length==0){
return "";
}
this.decoder.initSync(cryptoFramework.CryptoMode.DECRYPT_MODE, this.key, this.iv);
let base = new util.Base64Helper();
let plainText: cryptoFramework.DataBlob = { data: base.decodeSync(message) };
let decryptData = this.decoder.doFinalSync(plainText);
if (decryptData.data.toString() === decryptData.data.toString()) {
let textDecoder = util.TextDecoder.create('utf-8');
return textDecoder.decodeWithStream(decryptData.data);
} else {
console.error('decrypt failed');
return "";
}
}
private genSymKeyByData(key: string) {
key = key.substring(0, 8);
let keyData = new Uint8Array(24);
keyData.set(new util.TextEncoder().encodeInto(key))
let symKeyBlob: cryptoFramework.DataBlob = { data: keyData };
let symGenerator = cryptoFramework.createSymKeyGenerator('3DES192');
let symKey = symGenerator.convertKeySync(symKeyBlob);
return symKey;
}
private genIvParamsSpec(iv: string): cryptoFramework.IvParamsSpec {
iv = iv.substring(0, 8);
let dataIv = new Uint8Array(24);
dataIv.set(new util.TextEncoder().encodeInto(iv))
let ivBlob: cryptoFramework.DataBlob = { data: dataIv };
let ivParamsSpec: cryptoFramework.IvParamsSpec = {
algName: 'IvParamsSpec',
iv: ivBlob
};
return ivParamsSpec;
}
}
验证效果:
在线工具的结果:
Yes!!!
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)