1# 使用3DES对称密钥(ECB模式)加解密(ArkTS) 2 3 4对应的算法规格请查看[对称密钥加解密算法规格:3DES](crypto-sym-encrypt-decrypt-spec.md#3des)。 5 6 7**加密** 8 9 101. 调用[cryptoFramework.createSymKeyGenerator](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreatesymkeygenerator)、[SymKeyGenerator.convertKey](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#convertkey-1),生成密钥算法为3DES、密钥长度为192位的对称密钥(SymKey)。 11 12 如何生成3DES对称密钥,开发者可参考下文示例,并结合[对称密钥生成和转换规格:3DES](crypto-sym-key-generation-conversion-spec.md#3des)和[指定二进制数据转换对称密钥](crypto-convert-binary-data-to-sym-key.md)理解,参考文档与当前示例可能存在入参差异,请在阅读时注意区分。 13 142. 调用[cryptoFramework.createCipher](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreatecipher),指定字符串参数'3DES192|ECB|PKCS7',创建对称密钥类型为3DES192、分组模式为ECB、填充模式为PKCS7的Cipher实例,用于完成加解密操作。 15 163. 调用[Cipher.init](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#init-1),设置模式为加密(CryptoMode.ENCRYPT_MODE),指定加密密钥(SymKey),初始化加密Cipher实例。 17 18 ECB模式无加密参数,直接传入null。 19 204. 调用[Cipher.update](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#update-1),更新数据(明文)。 21 22 - 当数据量较小时,可以在init完成后直接调用doFinal。 23 - 当数据量较大时,可以多次调用update,即分段加解密。 24 255. 调用[Cipher.doFinal](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#dofinal-1),获取加密后的数据。 26 27 - 由于已使用update传入数据,此处data传入null。 28 - doFinal输出结果可能为null,在访问具体数据前,需要先判断结果是否为null,避免产生异常。 29 30 31**解密** 32 33 341. 调用[Cipher.init](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#init-1),设置模式为解密(CryptoMode.DECRYPT_MODE),指定解密密钥(SymKey)初始化解密Cipher实例。ECB模式无加密参数,直接传入null。 35 362. 调用[Cipher.update](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#update-1),更新数据(密文)。 37 383. 调用[Cipher.doFinal](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#dofinal-1),获取解密后的数据。 39 40- 异步方法示例: 41 42 ```ts 43 import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 44 import { buffer } from '@kit.ArkTS'; 45 46 // 加密消息 47 async function encryptMessagePromise(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) { 48 let cipher = cryptoFramework.createCipher('3DES192|ECB|PKCS7'); 49 await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, null); 50 let encryptData = await cipher.doFinal(plainText); 51 return encryptData; 52 } 53 // 解密消息 54 async function decryptMessagePromise(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) { 55 let decoder = cryptoFramework.createCipher('3DES192|ECB|PKCS7'); 56 await decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, null); 57 let decryptData = await decoder.doFinal(cipherText); 58 return decryptData; 59 } 60 async function genSymKeyByData(symKeyData: Uint8Array) { 61 let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData }; 62 let symGenerator = cryptoFramework.createSymKeyGenerator('3DES192'); 63 let symKey = await symGenerator.convertKey(symKeyBlob); 64 console.info('convertKey success'); 65 return symKey; 66 } 67 async function main() { 68 let keyData = new Uint8Array([238, 249, 61, 55, 128, 220, 183, 224, 139, 253, 248, 239, 239, 41, 71, 25, 235, 206, 230, 162, 249, 27, 234, 114]); 69 let symKey = await genSymKeyByData(keyData); 70 let message = "This is a test"; 71 let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) }; 72 let encryptText = await encryptMessagePromise(symKey, plainText); 73 let decryptText = await decryptMessagePromise(symKey, encryptText); 74 if (plainText.data.toString() === decryptText.data.toString()) { 75 console.info('decrypt ok'); 76 console.info('decrypt plainText: ' + buffer.from(decryptText.data).toString('utf-8')); 77 } else { 78 console.error('decrypt failed'); 79 } 80 } 81 ``` 82 83- 同步方法示例: 84 85 ```ts 86 import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 87 import { buffer } from '@kit.ArkTS'; 88 89 // 加密消息 90 function encryptMessage(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) { 91 let cipher = cryptoFramework.createCipher('3DES192|ECB|PKCS7'); 92 cipher.initSync(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, null); 93 let encryptData = cipher.doFinalSync(plainText); 94 return encryptData; 95 } 96 // 解密消息 97 function decryptMessage(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) { 98 let decoder = cryptoFramework.createCipher('3DES192|ECB|PKCS7'); 99 decoder.initSync(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, null); 100 let decryptData = decoder.doFinalSync(cipherText); 101 return decryptData; 102 } 103 function genSymKeyByData(symKeyData: Uint8Array) { 104 let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData }; 105 let symGenerator = cryptoFramework.createSymKeyGenerator('3DES192'); 106 let symKey = symGenerator.convertKeySync(symKeyBlob); 107 console.info('convertKeySync success'); 108 return symKey; 109 } 110 function main() { 111 let keyData = new Uint8Array([238, 249, 61, 55, 128, 220, 183, 224, 139, 253, 248, 239, 239, 41, 71, 25, 235, 206, 230, 162, 249, 27, 234, 114]); 112 let symKey = genSymKeyByData(keyData); 113 let message = "This is a test"; 114 let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) }; 115 let encryptText = encryptMessage(symKey, plainText); 116 let decryptText = decryptMessage(symKey, encryptText); 117 if (plainText.data.toString() === decryptText.data.toString()) { 118 console.info('decrypt ok'); 119 console.info('decrypt plainText: ' + buffer.from(decryptText.data).toString('utf-8')); 120 } else { 121 console.error('decrypt failed'); 122 } 123 } 124 ``` 125