1e41f4b71Sopenharmony_ci# Encryption and Decryption by Segment with an RSA Asymmetric Key Pair 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ciFor details about the algorithm specifications, see [RSA](crypto-asym-encrypt-decrypt-spec.md#rsa). 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci**Encryption** 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci1. Use [cryptoFramework.createAsyKeyGenerator](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreateasykeygenerator) and [AsyKeyGenerator.generateKeyPair](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#generatekeypair-1) to generate a 1024-bit RSA asymmetric key pair (**KeyPair**) with two primes. The number of primes is not specified by default. The **KeyPair** object includes a public key (**PubKey**) and a private key (**PriKey**). 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci In addition to the example in this topic, [RSA](crypto-asym-key-generation-conversion-spec.md#rsa) and [Randomly Generating an Asymmetric Key Pair](crypto-generate-asym-key-pair-randomly.md) may help you better understand how to generate an RSA asymmetric key pair. Note that the input parameters in the reference documents may be different from those in the example below. 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci2. Use [cryptoFramework.createCipher](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreatecipher) with the string parameter **'RSA1024|PKCS1'** to create a **Cipher** instance for encryption. The key type is **RSA1024**, and the padding mode is **PKCS1**. 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci3. Use [Cipher.init](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#init-1) to initialize the **Cipher** instance. In **Cipher.init**, set **opMode** to **CryptoMode.ENCRYPT_MODE** (encryption) and **key** to **KeyPair.PubKey** (the key used for encryption). 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci4. Call [Cipher.doFinal](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#dofinal-1) multiple times to pass in the plaintext and encrypt it by segment. 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci The output of **doFinal** may be **null**. To avoid exceptions, always check whether the result is **null** before accessing specific data. 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci In this example, the plaintext is split by 64 bytes and encrypted multiple times by a 1024-bit key. A 128-byte ciphertext is generated each time. 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci**Decryption** 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci1. If RSA is used, the **Cipher** instance cannot be initialized repeatedly. Use [cryptoFramework.createCipher](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreatecipher) to create a new **Cipher** instance. 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci2. Use [Cipher.init](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#init-1) to initialize the **Cipher** instance. In **Cipher.init**, set **opMode** to **CryptoMode.DECRYPT_MODE** (decryption) and **key** to **KeyPair.PriKey** (the key used for decryption). When PKCS1 mode is used, pass in **null** in **params**. 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci3. Call [Cipher.doFinal](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#dofinal-1) multiple times to pass in the ciphertext and decrypt it by segment. 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci- Example (using asynchronous APIs): 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci ```ts 38e41f4b71Sopenharmony_ci import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 39e41f4b71Sopenharmony_ci import { buffer } from '@kit.ArkTS'; 40e41f4b71Sopenharmony_ci // Encrypt the message by segment. 41e41f4b71Sopenharmony_ci async function rsaEncryptBySegment(pubKey: cryptoFramework.PubKey, plainText: cryptoFramework.DataBlob) { 42e41f4b71Sopenharmony_ci let cipher = cryptoFramework.createCipher('RSA1024|PKCS1'); 43e41f4b71Sopenharmony_ci await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, pubKey, null); 44e41f4b71Sopenharmony_ci let plainTextSplitLen = 64; 45e41f4b71Sopenharmony_ci let cipherText = new Uint8Array(); 46e41f4b71Sopenharmony_ci for (let i = 0; i < plainText.data.length; i += plainTextSplitLen ) { 47e41f4b71Sopenharmony_ci let updateMessage = plainText.data.subarray(i, i + plainTextSplitLen ); 48e41f4b71Sopenharmony_ci let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage }; 49e41f4b71Sopenharmony_ci // Split the plaintext by 64 bytes and cyclically call doFinal() to encrypt the plaintext using a 1024-bit key. A 128-byte ciphertext is generated each time. 50e41f4b71Sopenharmony_ci let updateOutput = await cipher.doFinal(updateMessageBlob); 51e41f4b71Sopenharmony_ci let mergeText = new Uint8Array(cipherText.length + updateOutput.data.length); 52e41f4b71Sopenharmony_ci mergeText.set(cipherText); 53e41f4b71Sopenharmony_ci mergeText.set(updateOutput.data, cipherText.length); 54e41f4b71Sopenharmony_ci cipherText = mergeText; 55e41f4b71Sopenharmony_ci } 56e41f4b71Sopenharmony_ci let cipherBlob: cryptoFramework.DataBlob = { data: cipherText }; 57e41f4b71Sopenharmony_ci return cipherBlob; 58e41f4b71Sopenharmony_ci } 59e41f4b71Sopenharmony_ci // Decrypt the message by segment. 60e41f4b71Sopenharmony_ci async function rsaDecryptBySegment(priKey: cryptoFramework.PriKey, cipherText: cryptoFramework.DataBlob) { 61e41f4b71Sopenharmony_ci let decoder = cryptoFramework.createCipher('RSA1024|PKCS1'); 62e41f4b71Sopenharmony_ci await decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, priKey, null); 63e41f4b71Sopenharmony_ci let cipherTextSplitLen = 128; // Length of the ciphertext = Number of key bits/8 64e41f4b71Sopenharmony_ci let decryptText = new Uint8Array(); 65e41f4b71Sopenharmony_ci for (let i = 0; i < cipherText.data.length; i += cipherTextSplitLen) { 66e41f4b71Sopenharmony_ci let updateMessage = cipherText.data.subarray(i, i + cipherTextSplitLen); 67e41f4b71Sopenharmony_ci let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage }; 68e41f4b71Sopenharmony_ci // Split the ciphertext by 128 bytes, decrypt the ciphertext, and combine the plaintext obtained each time. 69e41f4b71Sopenharmony_ci let updateOutput = await decoder.doFinal(updateMessageBlob); 70e41f4b71Sopenharmony_ci let mergeText = new Uint8Array(decryptText.length + updateOutput.data.length); 71e41f4b71Sopenharmony_ci mergeText.set(decryptText); 72e41f4b71Sopenharmony_ci mergeText.set(updateOutput.data, decryptText.length); 73e41f4b71Sopenharmony_ci decryptText = mergeText; 74e41f4b71Sopenharmony_ci } 75e41f4b71Sopenharmony_ci let decryptBlob: cryptoFramework.DataBlob = { data: decryptText }; 76e41f4b71Sopenharmony_ci return decryptBlob; 77e41f4b71Sopenharmony_ci } 78e41f4b71Sopenharmony_ci async function rsaEncryptLongMessage() { 79e41f4b71Sopenharmony_ci let message = "This is a long plainTest! This is a long plainTest! This is a long plainTest!" + 80e41f4b71Sopenharmony_ci "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" + 81e41f4b71Sopenharmony_ci "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" + 82e41f4b71Sopenharmony_ci "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" + 83e41f4b71Sopenharmony_ci "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" + 84e41f4b71Sopenharmony_ci "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" + 85e41f4b71Sopenharmony_ci "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" + 86e41f4b71Sopenharmony_ci "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!"; 87e41f4b71Sopenharmony_ci let asyKeyGenerator = cryptoFramework.createAsyKeyGenerator("RSA1024"); // Create an AsyKeyGenerator object. 88e41f4b71Sopenharmony_ci let keyPair = await asyKeyGenerator.generateKeyPair(); // Randomly generate an RSA key pair. 89e41f4b71Sopenharmony_ci let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) }; 90e41f4b71Sopenharmony_ci let encryptText = await rsaEncryptBySegment(keyPair.pubKey, plainText); 91e41f4b71Sopenharmony_ci let decryptText = await rsaDecryptBySegment(keyPair.priKey, encryptText); 92e41f4b71Sopenharmony_ci if (plainText.data.toString() === decryptText.data.toString()) { 93e41f4b71Sopenharmony_ci console.info('decrypt ok'); 94e41f4b71Sopenharmony_ci console.info('decrypt plainText: ' + buffer.from(decryptText.data).toString('utf-8')); 95e41f4b71Sopenharmony_ci } else { 96e41f4b71Sopenharmony_ci console.error('decrypt failed'); 97e41f4b71Sopenharmony_ci } 98e41f4b71Sopenharmony_ci } 99e41f4b71Sopenharmony_ci ``` 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ci- Example (using synchronous APIs): 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ci ```ts 104e41f4b71Sopenharmony_ci import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 105e41f4b71Sopenharmony_ci import { buffer } from '@kit.ArkTS'; 106e41f4b71Sopenharmony_ci // Encrypt the message by segment. 107e41f4b71Sopenharmony_ci function rsaEncryptBySegment(pubKey: cryptoFramework.PubKey, plainText: cryptoFramework.DataBlob) { 108e41f4b71Sopenharmony_ci let cipher = cryptoFramework.createCipher('RSA1024|PKCS1'); 109e41f4b71Sopenharmony_ci cipher.initSync(cryptoFramework.CryptoMode.ENCRYPT_MODE, pubKey, null); 110e41f4b71Sopenharmony_ci let plainTextSplitLen = 64; 111e41f4b71Sopenharmony_ci let cipherText = new Uint8Array(); 112e41f4b71Sopenharmony_ci for (let i = 0; i < plainText.data.length; i += plainTextSplitLen ) { 113e41f4b71Sopenharmony_ci let updateMessage = plainText.data.subarray(i, i + plainTextSplitLen ); 114e41f4b71Sopenharmony_ci let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage }; 115e41f4b71Sopenharmony_ci // Split the plaintext by 64 bytes and cyclically call doFinal() to encrypt the plaintext using a 1024-bit key. The ciphertext of 128 bytes is generated each time. 116e41f4b71Sopenharmony_ci let updateOutput = cipher.doFinalSync(updateMessageBlob); 117e41f4b71Sopenharmony_ci let mergeText = new Uint8Array(cipherText.length + updateOutput.data.length); 118e41f4b71Sopenharmony_ci mergeText.set(cipherText); 119e41f4b71Sopenharmony_ci mergeText.set(updateOutput.data, cipherText.length); 120e41f4b71Sopenharmony_ci cipherText = mergeText; 121e41f4b71Sopenharmony_ci } 122e41f4b71Sopenharmony_ci let cipherBlob: cryptoFramework.DataBlob = { data: cipherText }; 123e41f4b71Sopenharmony_ci return cipherBlob; 124e41f4b71Sopenharmony_ci } 125e41f4b71Sopenharmony_ci // Decrypt the message by segment. 126e41f4b71Sopenharmony_ci function rsaDecryptBySegment(priKey: cryptoFramework.PriKey, cipherText: cryptoFramework.DataBlob) { 127e41f4b71Sopenharmony_ci let decoder = cryptoFramework.createCipher('RSA1024|PKCS1'); 128e41f4b71Sopenharmony_ci decoder.initSync(cryptoFramework.CryptoMode.DECRYPT_MODE, priKey, null); 129e41f4b71Sopenharmony_ci let cipherTextSplitLen = 128; // Length of the ciphertext = Number of key bits/8 130e41f4b71Sopenharmony_ci let decryptText = new Uint8Array(); 131e41f4b71Sopenharmony_ci for (let i = 0; i < cipherText.data.length; i += cipherTextSplitLen) { 132e41f4b71Sopenharmony_ci let updateMessage = cipherText.data.subarray(i, i + cipherTextSplitLen); 133e41f4b71Sopenharmony_ci let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage }; 134e41f4b71Sopenharmony_ci // Split the ciphertext by 128 bytes, decrypt the ciphertext, and combine the plaintext obtained each time. 135e41f4b71Sopenharmony_ci let updateOutput = decoder.doFinalSync(updateMessageBlob); 136e41f4b71Sopenharmony_ci let mergeText = new Uint8Array(decryptText.length + updateOutput.data.length); 137e41f4b71Sopenharmony_ci mergeText.set(decryptText); 138e41f4b71Sopenharmony_ci mergeText.set(updateOutput.data, decryptText.length); 139e41f4b71Sopenharmony_ci decryptText = mergeText; 140e41f4b71Sopenharmony_ci } 141e41f4b71Sopenharmony_ci let decryptBlob: cryptoFramework.DataBlob = { data: decryptText }; 142e41f4b71Sopenharmony_ci return decryptBlob; 143e41f4b71Sopenharmony_ci } 144e41f4b71Sopenharmony_ci async function main() { 145e41f4b71Sopenharmony_ci let message = "This is a long plainTest! This is a long plainTest! This is a long plainTest!" + 146e41f4b71Sopenharmony_ci "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" + 147e41f4b71Sopenharmony_ci "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" + 148e41f4b71Sopenharmony_ci "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" + 149e41f4b71Sopenharmony_ci "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" + 150e41f4b71Sopenharmony_ci "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" + 151e41f4b71Sopenharmony_ci "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" + 152e41f4b71Sopenharmony_ci "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!"; 153e41f4b71Sopenharmony_ci let asyKeyGenerator = cryptoFramework.createAsyKeyGenerator("RSA1024"); // Create an AsyKeyGenerator object. 154e41f4b71Sopenharmony_ci let keyPair = await asyKeyGenerator.generateKeyPair(); // Randomly generate an RSA key pair. 155e41f4b71Sopenharmony_ci let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) }; 156e41f4b71Sopenharmony_ci let encryptText = rsaEncryptBySegment(keyPair.pubKey, plainText); 157e41f4b71Sopenharmony_ci let decryptText = rsaDecryptBySegment(keyPair.priKey, encryptText); 158e41f4b71Sopenharmony_ci if (plainText.data.toString() === decryptText.data.toString()) { 159e41f4b71Sopenharmony_ci console.info('decrypt ok'); 160e41f4b71Sopenharmony_ci console.info('decrypt plainText: ' + buffer.from(decryptText.data).toString('utf-8')); 161e41f4b71Sopenharmony_ci } else { 162e41f4b71Sopenharmony_ci console.error('decrypt failed'); 163e41f4b71Sopenharmony_ci } 164e41f4b71Sopenharmony_ci } 165e41f4b71Sopenharmony_ci ``` 166