package com.ar.imagetracear.upload

import android.app.Activity
import android.util.Base64
import java.security.SecureRandom
import javax.crypto.Cipher
import javax.crypto.KeyGenerator
import javax.crypto.SecretKey
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec


object AESUtils {
//    private const val AES_MODE = "AES/CBC/PKCS5Padding"
    private const val AES_MODE = "AES"
    private const val AES_ALGORITHM = "AES"
    private const val AES_KEY_SIZE = 256 // 支持 128/192/256

    /**
     * 生成 AES 密钥
     */
    fun generateAESKey(): String {
        val keyGenerator = KeyGenerator.getInstance(AES_ALGORITHM)
        keyGenerator.init(AES_KEY_SIZE, SecureRandom())
        val secretKey: SecretKey = keyGenerator.generateKey()
        return Base64.encodeToString(secretKey.encoded, Base64.DEFAULT)
    }

    /**
     * 生成 16 字节 IV（初始化向量）
     */
    fun generateIV(): String {
        val iv = ByteArray(16)
        SecureRandom().nextBytes(iv)
        return Base64.encodeToString(iv, Base64.DEFAULT)
    }

    /**
     * AES 加密
     */
    fun encrypt(jsonString: String, key: String): String {
        val keySpec = SecretKeySpec(key.toByteArray(Charsets.UTF_8), AES_ALGORITHM)
        val ivSpec = IvParameterSpec(key.toByteArray(Charsets.UTF_8))

        val cipher = Cipher.getInstance(AES_MODE)
        cipher.init(Cipher.ENCRYPT_MODE, keySpec)

        val encryptedBytes = cipher.doFinal(jsonString.toByteArray(Charsets.UTF_8))
        return Base64.encodeToString(encryptedBytes, Base64.DEFAULT) // 返回 Base64 加密数据
    }



    fun encryptNew(plainText: String,key: String): String {
        val secretKey = SecretKeySpec(key.toByteArray(), AES_MODE)
        val cipher = Cipher.getInstance(AES_MODE)
        cipher.init(Cipher.ENCRYPT_MODE, secretKey)
        val encryptedBytes = cipher.doFinal(plainText.toByteArray())
        return Base64.encodeToString(encryptedBytes, Base64.DEFAULT)
    }


    /**
     * AES 解密
     */
    fun decrypt(encryptedData: String, key: String): String {
        val keySpec = SecretKeySpec(key.toByteArray(Charsets.UTF_8), AES_ALGORITHM)
        val ivSpec = IvParameterSpec(key.toByteArray(Charsets.UTF_8))

        val cipher = Cipher.getInstance(AES_MODE)
        cipher.init(Cipher.DECRYPT_MODE, keySpec)

        val decryptedBytes = cipher.doFinal(Base64.decode(encryptedData, Base64.DEFAULT))
        return String(decryptedBytes, Charsets.UTF_8) // 返回解密后的 JSON 字符串
    }


    fun testAES(context:Activity) {
        try {
            // 原始 JSON 字符串
//            val json = """{"username":"Alice","password":"123456"}"""

            val json = Upload.getData(context)

            // 生成 AES 密钥和 IV
            val aesKey = "e67cbcee5e573d1b"
            val aesIV = generateIV()

            println("AES 密钥: $aesKey")
//            println("AES IV: $aesIV")

            // 加密 JSON
            val encryptedData = encrypt(json, aesKey)
            println("加密后: $encryptedData")

            // 解密 JSON
            val decryptedData = decrypt(encryptedData, aesKey)
            println("解密后: $decryptedData")

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

}
