import { cryptoFramework } from '@ohos.security.cryptoFramework'; import { base64 } from '@ohos.util.base64'; async function encryptWithRSA(publicKeyStr: string, plainText: string): Promise<string> { let context = cryptoFramework.createContext(); context.setPublicKey(publicKeyStr); let encryptedData = await context.encrypt(plainText); let encryptedStr = base64.encode(encryptedData); return encryptedStr; } // 使用示例 let publicKey = '...'; // 你的公钥字符串 let plainText = 'Hello, World!'; encryptWithRSA(publicKey, plainText).then(encryptedStr => { console.log('Encrypted text:', encryptedStr); }).catch(error => { console.error('Encryption failed:', error); });