wuhb пре 4 месеци
родитељ
комит
ce06a0db05
1 измењених фајлова са 8 додато и 217 уклоњено
  1. 8 217
      utils/crypto.uts

+ 8 - 217
utils/crypto.uts

@@ -134,65 +134,17 @@ function decryptAESAndroid(cipherText: string, keyStr: string, ivStr: string): s
 }
 // #endif
 
-// ============ iOS 平台实现 ============
+
+// ============ iOS 平台实现(使用纯 TypeScript)============
 // #ifdef APP-IOS
 
 /**
- * iOS 平台 AES 加密实现
+ * iOS 平台 AES 加密实现(使用纯 TypeScript)
  */
 function encryptAESiOS(plainText: string, keyStr: string, ivStr: string): string {
     try {
-        // 使用 UTS 的 iOS 平台 API
-        const plainData = plainText.dataUsingEncoding(4) // NSUTF8StringEncoding = 4
-        const keyData = keyStr.dataUsingEncoding(4)
-        const ivData = ivStr.dataUsingEncoding(4)
-        
-        if (!plainData || !keyData || !ivData) {
-            console.error('AES 加密失败:数据转换错误')
-            return plainText
-        }
-        
-        // 准备加密参数
-        const keyBytes = keyData.bytes
-        const ivBytes = ivData.bytes
-        const plainBytes = plainData.bytes
-        const plainLength = plainData.length
-        
-        // 计算加密后数据大小
-        const blockSize = 16  // AES 块大小
-        const bufferSize = Math.floor((plainLength + blockSize) / blockSize) * blockSize
-        
-        // 创建输出缓冲区
-        const buffer = plus.ios.newObject('NSMutableData', 'dataWithLength:', bufferSize)
-        
-        // 执行加密
-        let encryptedLength = plus.ios.newObject('NSUInteger')
-        const status = plus.ios.invoke('CCCrypt', ':', 
-            0, // kCCEncrypt = 0
-            0, // kCCAlgorithmAES = 0
-            0x0001, // kCCOptionPKCS7Padding = 0x0001
-            keyBytes,
-            keyData.length,
-            ivBytes,
-            plainBytes,
-            plainLength,
-            buffer.bytes,
-            bufferSize,
-            plus.ios.addrOf(encryptedLength)
-        )
-        
-        if (status !== 0) { // kCCSuccess = 0
-            console.error('AES 加密失败,状态码:', status)
-            return plainText
-        }
-        
-        // 设置实际数据长度
-        buffer.setLength(encryptedLength)
-        
-        // 转为 Base64 字符串
-        const base64String = buffer.base64EncodedStringWithOptions(0)
-        
-        return String(base64String)
+        console.log('iOS 使用纯 TypeScript 加密')
+        return encryptAESPure(plainText, keyStr, ivStr)
     } catch (e) {
         console.error('AES 加密失败(iOS)', e)
         return plainText
@@ -200,181 +152,20 @@ function encryptAESiOS(plainText: string, keyStr: string, ivStr: string): string
 }
 
 /**
- * iOS 平台 AES 解密实现
+ * iOS 平台 AES 解密实现(使用纯 TypeScript)
  */
 function decryptAESiOS(cipherText: string, keyStr: string, ivStr: string): string {
     try {
-        // Base64 字符串转为 Data
-        const cipherData = plus.ios.invoke('NSData', 'alloc')
-        const initData = plus.ios.invoke(cipherData, 'initWithBase64EncodedString:options:', cipherText, 0)
-        
-        if (!initData) {
-            console.error('AES 解密失败:Base64 解码失败')
-            return cipherText
-        }
-        
-        const keyData = keyStr.dataUsingEncoding(4) // NSUTF8StringEncoding = 4
-        const ivData = ivStr.dataUsingEncoding(4)
-        
-        if (!keyData || !ivData) {
-            console.error('AES 解密失败:密钥或 IV 转换错误')
-            return cipherText
-        }
-        
-        // 准备解密参数
-        const keyBytes = keyData.bytes
-        const ivBytes = ivData.bytes
-        const cipherBytes = cipherData.bytes
-        const cipherLength = cipherData.length
-        
-        // 计算解密后数据大小(预留额外空间)
-        const bufferSize = cipherLength + 16
-        const buffer = plus.ios.newObject('NSMutableData', 'dataWithLength:', bufferSize)
-        
-        // 执行解密
-        let decryptedLength = plus.ios.newObject('NSUInteger')
-        const status = plus.ios.invoke('CCCrypt', ':',
-            1, // kCCDecrypt = 1
-            0, // kCCAlgorithmAES = 0
-            0x0001, // kCCOptionPKCS7Padding = 0x0001
-            keyBytes,
-            keyData.length,
-            ivBytes,
-            cipherBytes,
-            cipherLength,
-            buffer.bytes,
-            bufferSize,
-            plus.ios.addrOf(decryptedLength)
-        )
-        
-        if (status !== 0) { // kCCSuccess = 0
-            console.error('AES 解密失败,状态码:', status)
-            return cipherText
-        }
-        
-        // 设置实际数据长度
-        buffer.setLength(decryptedLength)
-        
-        // 转为字符串
-        const decryptedStr = plus.ios.invoke('NSString', 'alloc')
-        const resultStr = plus.ios.invoke(decryptedStr, 'initWithData:encoding:', buffer, 4) // NSUTF8StringEncoding = 4
-        
-        return String(resultStr)
+        console.log('iOS 使用纯 TypeScript 解密')
+        return decryptAESPure(cipherText, keyStr, ivStr)
     } catch (e) {
         console.error('AES 解密失败(iOS)', e)
         return cipherText
     }
 }
 
-// 或者更简洁的 iOS 实现版本(使用 UTS 的 plus.ios API)
-function encryptAESiOS_simple(plainText: string, keyStr: string, ivStr: string): string {
-    try {
-        // 创建 CCCryptor
-        const cryptorRef = plus.ios.newObject('CCCryptorRef')
-        
-        // 创建密钥和 IV 数据
-        const keyData = plus.ios.invoke('NSData', 'dataWithBytes:length:', 
-            plus.ios.newObject('NSString', 'stringWithString:', keyStr).UTF8String, 
-            keyStr.length)
-        
-        const ivData = plus.ios.invoke('NSData', 'dataWithBytes:length:', 
-            plus.ios.newObject('NSString', 'stringWithString:', ivStr).UTF8String, 
-            ivStr.length)
-        
-        const plainData = plus.ios.invoke('NSData', 'dataWithBytes:length:', 
-            plus.ios.newObject('NSString', 'stringWithString:', plainText).UTF8String, 
-            plainText.length)
-        
-        // 创建输出缓冲区
-        const bufferSize = plainData.length + 16
-        const buffer = plus.ios.invoke('NSMutableData', 'dataWithLength:', bufferSize)
-        
-        // 执行加密
-        let dataOutMoved = 0
-        const status = plus.ios.invoke('CCCrypt', 
-            0, // kCCEncrypt
-            0, // kCCAlgorithmAES128
-            1, // kCCOptionPKCS7Padding
-            keyData.bytes,
-            keyData.length,
-            ivData.bytes,
-            plainData.bytes,
-            plainData.length,
-            buffer.bytes,
-            buffer.length,
-            dataOutMoved
-        )
-        
-        if (status === 0) { // kCCSuccess
-            buffer.setLength(dataOutMoved)
-            const base64Str = buffer.base64EncodedStringWithOptions(0)
-            return String(base64Str)
-        } else {
-            console.error('加密失败,错误码:', status)
-            return plainText
-        }
-    } catch (e) {
-        console.error('AES 加密失败(iOS)', e)
-        return plainText
-    }
-}
-
-function decryptAESiOS_simple(cipherText: string, keyStr: string, ivStr: string): string {
-    try {
-        // Base64 解码
-        const cipherData = plus.ios.invoke('NSData', 'alloc')
-        const initData = plus.ios.invoke(cipherData, 'initWithBase64EncodedString:options:', cipherText, 0)
-        
-        if (!initData) {
-            console.error('解密失败:Base64 解码失败')
-            return cipherText
-        }
-        
-        // 创建密钥和 IV 数据
-        const keyData = plus.ios.invoke('NSData', 'dataWithBytes:length:', 
-            plus.ios.newObject('NSString', 'stringWithString:', keyStr).UTF8String, 
-            keyStr.length)
-        
-        const ivData = plus.ios.invoke('NSData', 'dataWithBytes:length:', 
-            plus.ios.newObject('NSString', 'stringWithString:', ivStr).UTF8String, 
-            ivStr.length)
-        
-        // 创建输出缓冲区
-        const buffer = plus.ios.invoke('NSMutableData', 'dataWithLength:', cipherData.length)
-        
-        // 执行解密
-        let dataOutMoved = 0
-        const status = plus.ios.invoke('CCCrypt',
-            1, // kCCDecrypt
-            0, // kCCAlgorithmAES128
-            1, // kCCOptionPKCS7Padding
-            keyData.bytes,
-            keyData.length,
-            ivData.bytes,
-            cipherData.bytes,
-            cipherData.length,
-            buffer.bytes,
-            buffer.length,
-            dataOutMoved
-        )
-        
-        if (status === 0) { // kCCSuccess
-            buffer.setLength(dataOutMoved)
-            const resultStr = plus.ios.invoke('NSString', 'alloc')
-            const finalStr = plus.ios.invoke(resultStr, 'initWithData:encoding:', buffer, 4) // NSUTF8StringEncoding = 4
-            return String(finalStr)
-        } else {
-            console.error('解密失败,错误码:', status)
-            return cipherText
-        }
-    } catch (e) {
-        console.error('AES 解密失败(iOS)', e)
-        return cipherText
-    }
-}
 // #endif
 
-
 // 鸿蒙平台 AES 加密实现
 // #ifdef APP-HARMONY