import { cryptoFramework } from “@kit.CryptoArchitectureKit”;
import { buffer, util } from ‘@kit.ArkTS’;
import Logger from ‘./Logger’;
import { BusinessError } from ‘@kit.BasicServicesKit’;
const TAG: string = ‘[CipherModel]’
const RSA_ENCRYPT_KEY: string =
‘MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALXJZEloyLbBB6UbUQzUtM3WGTkcd4dn4HgCxL5wHcdICoLbv6EiUjcaQq8c906hqv6/J7Bv9Owj59XMauKweJUCAwEAAQ==’;
const RSA_DECRYPT_KEY: string =
‘MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAtclkSWjItsEHpRtRDNS0zdYZORx3h2fgeALEvnAdx0gKgtu/oSJSNxpCrxz3TqGq/r8nsG/07CPn1cxq4rB4lQIDAQABAkA3boG2IM2TbKj4H6xHTVUUrwRh9dw83eAEuFNl/qoV6c4zRUAx+efZ29rDz6CVWuAhxaVBDUOmOHvyxOL8m8IBAiEA3EcTP1jngtiJ8lffvIVbehM6p7437+9UScKMXZSy/PkCIQDTRFj00GbAW9oKqEWTrUCWNxNFCSR82Mlw1sZvQh5LfQIgBApBrh3BUUMLdKhr8Bc6EEkeAEma2Qm4sAmjbWv2xHECIF81ux1BWj0wZ9hLs2d1Odk4ot+G2kHFdSr8L9tuIbcFAiEA2rEXmzyQTxZM1N4QDkaLJiCwSfMTYu48DxfUcevbfhA=’;
const RSA512_PRIMES_2: string = ‘RSA512|PRIMES_2’;
const RSA512_PKCS1: string = ‘RSA512|PKCS1’;
const AES128: string = ‘AES128’;
const AES128_PKCS7: string = ‘AES128|PKCS7’;
const AES256: string = ‘AES256’;
const AES256_PKCS5: string = ‘AES256|CBC|PKCS5’;
// const AES_ENCRYPT_KEY: string = ‘5QXzAbJj0TJN9OQNvxFhhw==’;
// const AES_ENCRYPT_KEY: string = ‘xxxx_aaaa22334_aaaa1111-aa11-aaa’;
// const AES_ENCRYPT_KEY: string = ‘xxxx_aaaa22334_aaaa1111-aa11-aaa’;//原
const AES_ENCRYPT_KEY: string = ‘ZwGb1EptBYzsMMT2e5ZvMdGMwZ1msuaaAxyV0JpN+6Y=’;
// const AES_IV_KEY: string = ‘xx_aaaa22334_aaa’;//原
const AES_IV_KEY: string = ‘ZXF5bvg0Ne84+R8pAZNmFg==’;
export class CipherModel {
aesEncrypt(message: string, callback) {
<span class="hljs-comment">//5QXzAbJj0TJN9OQNvxFhhw== 这种长度的key使用AES128</span>
<span class="hljs-comment">// let aesGenerator = cryptoFramework.createSymKeyGenerator(AES128);</span>
<span class="hljs-comment">//32字节的key需要使用AES256</span>
<span class="hljs-keyword">let</span> aesGenerator = cryptoFramework.createSymKeyGenerator(AES256);
<span class="hljs-keyword">let</span> cipher = cryptoFramework.createCipher(AES256_PKCS5);
<span class="hljs-keyword">let</span> that = <span class="hljs-keyword">new</span> util.Base64Helper();
<span class="hljs-comment">//密钥为普通字符串</span>
<span class="hljs-comment">// let textEncoder = new util.TextEncoder();</span>
<span class="hljs-comment">// let buffer = new ArrayBuffer(20);</span>
<span class="hljs-comment">// let result = new Uint8Array(buffer);</span>
<span class="hljs-comment">// result = textEncoder.encodeInto(AES_ENCRYPT_KEY);</span>
<span class="hljs-comment">//密钥为base64字符串</span>
<span class="hljs-comment">//当 AES_ENCRYPT_KEY为base64类型的字符串是才需要调用下述解码方法</span>
<span class="hljs-keyword">let</span> result = that.decodeSync(AES_ENCRYPT_KEY);
<span class="hljs-keyword">let</span> pubKeyBlob: cryptoFramework.DataBlob = { data: result };
aesGenerator.convertKey(pubKeyBlob, (err, symKey) => {
<span class="hljs-keyword">if</span> (err) {
console.error(<span class="hljs-string">"aesEncrypt convertKey: error."</span> + (err as BusinessError).code);
<span class="hljs-keyword">return</span>;
}
console.log(<span class="hljs-string">"--- symKey: "</span> + symKey);
<span class="hljs-comment">//偏移量为base64字符串</span>
<span class="hljs-keyword">let</span> ivData = that.decodeSync(AES_IV_KEY)
<span class="hljs-comment">//偏移量为普通字符串</span>
<span class="hljs-comment">// let ivData = this.stringToUint8Array(AES_IV_KEY);</span>
<span class="hljs-keyword">let</span> ivdata: cryptoFramework.DataBlob = { data: ivData }; <span class="hljs-comment">//偏移</span>
<span class="hljs-keyword">let</span> iv: cryptoFramework.IvParamsSpec = { iv: ivdata, algName: <span class="hljs-string">'IvParamsSpec'</span> }
<span class="hljs-comment">// cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, { algName: AES_IV_KEY }, (err, data) => {</span>
cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, iv, (err, data) => {
<span class="hljs-keyword">let</span> input: cryptoFramework.DataBlob = { data: <span class="hljs-keyword">this</span>.stringToUint8Array(message) };
cipher.doFinal(input, (err, data) => {
Logger.info(TAG, <span class="hljs-string">"EncryptOutPut is "</span> + data.data);
<span class="hljs-keyword">let</span> result = that.encodeToStringSync(data.data)
Logger.info(TAG, <span class="hljs-string">"result is "</span> + result);
callback(result)
})
})
})
}
stringToUint8Array(str) {
<span class="hljs-keyword">var</span> arr = [];
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>, j = str.length; i < j; ++i) {
arr.push(str.charCodeAt(i));
}
<span class="hljs-keyword">var</span> tmpArray = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Uint8Array</span>(arr);
<span class="hljs-keyword">return</span> tmpArray;
}
uint8ArrayToString(array: Uint8Array) {
<span class="hljs-keyword">let</span> arrayString = <span class="hljs-string">''</span>;
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i < array.length; i++) {
arrayString += <span class="hljs-built_in">String</span>.fromCharCode(array[i]);
}
<span class="hljs-keyword">return</span> arrayString;
}
rsaEncrypt(message: string, callback) {
<span class="hljs-keyword">let</span> rsaGenerator = cryptoFramework.createAsyKeyGenerator(RSA512_PRIMES_2);
<span class="hljs-keyword">let</span> cipher = cryptoFramework.createCipher(RSA512_PKCS1);
<span class="hljs-keyword">let</span> that = <span class="hljs-keyword">new</span> util.Base64Helper();
<span class="hljs-keyword">let</span> pubKey = that.decodeSync(RSA_ENCRYPT_KEY);
<span class="hljs-keyword">let</span> pubKeyBlob: cryptoFramework.DataBlob = { data: pubKey };
rsaGenerator.convertKey(pubKeyBlob, <span class="hljs-literal">null</span>, (err, keyPair) => {
<span class="hljs-keyword">if</span> (err) {
Logger.error(<span class="hljs-string">"convertKey: error."</span> + (err as BusinessError).code);
<span class="hljs-keyword">return</span>;
}
cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, keyPair.pubKey, <span class="hljs-literal">null</span>, (err, data) => {
<span class="hljs-keyword">let</span> input: cryptoFramework.DataBlob = { data: <span class="hljs-keyword">this</span>.stringToUint8Array(message) };
cipher.doFinal(input, (err, data) => {
Logger.info(TAG, <span class="hljs-string">"EncryptOutPut is "</span> + data.data);
<span class="hljs-keyword">let</span> result = that.encodeToStringSync(data.data)
Logger.info(TAG, <span class="hljs-string">"result is "</span> + result);
callback(result)
})
})
})
}
rsaDecrypt(message: string, callback) {
<span class="hljs-keyword">let</span> rsaGenerator = cryptoFramework.createAsyKeyGenerator(RSA512_PRIMES_2);
<span class="hljs-keyword">let</span> cipher = cryptoFramework.createCipher(RSA512_PKCS1);
<span class="hljs-keyword">let</span> that = <span class="hljs-keyword">new</span> util.Base64Helper();
<span class="hljs-keyword">let</span> priKey = that.decodeSync(RSA_DECRYPT_KEY);
<span class="hljs-keyword">let</span> priKeyBlob: cryptoFramework.DataBlob = { data: priKey };
rsaGenerator.convertKey(<span class="hljs-literal">null</span>, priKeyBlob, (err, keyPair) => {
<span class="hljs-keyword">if</span> (err) {
Logger.error(TAG, <span class="hljs-string">"convertKey: error."</span> + (err as BusinessError).code);
<span class="hljs-keyword">return</span>;
}
cipher.init(cryptoFramework.CryptoMode.DECRYPT_MODE, keyPair.priKey, <span class="hljs-literal">null</span>, (err, data) => {
<span class="hljs-keyword">try</span> {
<span class="hljs-keyword">let</span> newMessage = that.decodeSync(message);
<span class="hljs-keyword">let</span> input: cryptoFramework.DataBlob = { data: newMessage };
cipher.doFinal(input, (err, data) => {
<span class="hljs-keyword">if</span> (err) {
Logger.error(TAG, <span class="hljs-string">"cipher doFinal."</span> + (err as BusinessError).code);
<span class="hljs-keyword">return</span>;
}
Logger.info(TAG, <span class="hljs-string">"DecryptOutPut is "</span> + data.data);
<span class="hljs-keyword">let</span> result = <span class="hljs-keyword">this</span>.uint8ArrayToString(data.data);
Logger.info(TAG, <span class="hljs-string">"result is "</span> + result);
callback(result)
})
} <span class="hljs-keyword">catch</span> (err) {
Logger.info(TAG, <span class="hljs-string">"cipher init error: "</span> + (err as BusinessError).code);
<span class="hljs-keyword">return</span> err;
}
})
})
}
aesDecrypt(message: string, callback) {
<span class="hljs-comment">//同加密方法中的描述</span>
<span class="hljs-keyword">let</span> aesGenerator = cryptoFramework.createSymKeyGenerator(AES256);
<span class="hljs-keyword">let</span> cipher = cryptoFramework.createCipher(AES256_PKCS5);
<span class="hljs-keyword">let</span> that = <span class="hljs-keyword">new</span> util.Base64Helper();
<span class="hljs-comment">//密钥为普通字符串</span>
<span class="hljs-comment">// let textEncoder = new util.TextEncoder();</span>
<span class="hljs-comment">// let buffer = new ArrayBuffer(20);</span>
<span class="hljs-comment">// // let result = new Uint8Array(buffer);</span>
<span class="hljs-comment">// // result = textEncoder.encodeInto(AES_ENCRYPT_KEY);</span>
<span class="hljs-comment">//密钥为base64字符串</span>
<span class="hljs-keyword">let</span> result = that.decodeSync(AES_ENCRYPT_KEY)
<span class="hljs-comment">//dT/MzvKuG2VSsp+h62E/yQ==</span>
<span class="hljs-keyword">let</span> pubKeyBlob: cryptoFramework.DataBlob = { data: result };
aesGenerator.convertKey(pubKeyBlob, (err, symKey) => {
<span class="hljs-keyword">if</span> (err) {
console.error(<span class="hljs-string">"aesDecrypt convertKey: error."</span> + (err as BusinessError).code);
<span class="hljs-keyword">return</span>;
}
<span class="hljs-comment">//偏移量为普通字符串</span>
<span class="hljs-comment">// let ivData = this.stringToUint8Array(AES_IV_KEY);</span>
<span class="hljs-comment">//偏移量为base64字符串</span>
<span class="hljs-keyword">let</span> ivData = that.decodeSync(AES_IV_KEY)
<span class="hljs-keyword">let</span> ivdata: cryptoFramework.DataBlob = { data: ivData }; <span class="hljs-comment">//偏移</span>
<span class="hljs-keyword">let</span> iv: cryptoFramework.IvParamsSpec = { iv: ivdata, algName: <span class="hljs-string">'IvParamsSpec'</span> }
<span class="hljs-comment">// cipher.init(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, null, (err, data) => {</span>
cipher.init(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, iv, (err, data) => {
<span class="hljs-keyword">try</span> {
<span class="hljs-keyword">let</span> newMessage = that.decodeSync(message);
<span class="hljs-comment">//测试用</span>
<span class="hljs-comment">// let newMessage = that.decodeSync('dT/MzvKuG2VSsp+h62E/yQ==');</span>
<span class="hljs-keyword">let</span> input: cryptoFramework.DataBlob = { data: newMessage };
cipher.doFinal(input, (err, data) => {
<span class="hljs-keyword">if</span> (err) {
Logger.error(TAG, <span class="hljs-string">"cipher doFinal."</span> + (err as BusinessError).code);
<span class="hljs-keyword">return</span>;
}
Logger.info(TAG, <span class="hljs-string">"DecryptOutPut is "</span> + data?.data);
<span class="hljs-keyword">let</span> result = <span class="hljs-keyword">this</span>.uint8ArrayToString(data?.data)
Logger.info(TAG, <span class="hljs-string">"result is "</span> + result);
callback(result)
})
} <span class="hljs-keyword">catch</span> (err) {
Logger.info(TAG, <span class="hljs-string">"cipher init error: "</span> + (err as BusinessError).code);
<span class="hljs-keyword">return</span> err;
}
})
})
}