iOS中可以使用OpenSSL库来实现AES-GCM和AES-ECB加密解密操作。下面给出一个示例代码:
```objc
#include <openssl/evp.h>
#include <openssl/rand.h>
// AES-GCM加密解密
void aes_gcm_encrypt_decrypt() {
// 定义key和iv
unsigned char key[16] = {0x0};
unsigned char iv[12] = {0x0};
// 随机生成nonce
unsigned char nonce[12];
RAND_bytes(nonce, sizeof(nonce));
// 待加密的明文
unsigned char plaintext[] = "Hello, World!";
int plaintext_len = strlen(plaintext);
// 分配内存
unsigned char *ciphertext = malloc(plaintext_len + EVP_GCM_TLS_EXPLICIT_IV_LEN);
unsigned char *decryptedtext = malloc(plaintext_len);
// 创建并初始化EVP_CIPHER_CTX
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, sizeof(nonce), NULL);
EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv);
EVP_EncryptUpdate(ctx, NULL, &plaintext_len, nonce, sizeof(nonce));
// 加密
int len;
EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len);
int ciphertext_len = len;
EVP_EncryptFinal_ex(ctx, ciphertext + len, &len);
ciphertext_len += len;
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, ciphertext + ciphertext_len);
ciphertext_len += 16;
// 解密
EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, sizeof(nonce), NULL);
EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv);
EVP_DecryptUpdate(ctx, NULL, &plaintext_len, nonce, sizeof(nonce));
EVP_DecryptUpdate(ctx, decryptedtext, &len, ciphertext, ciphertext_len - 16);
int decryptedtext_len = len;
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, ciphertext + ciphertext_len - 16);
EVP_DecryptFinal_ex(ctx, decryptedtext + len, &len);
decryptedtext_len += len;
// 打印结果
printf("AES-GCM Ciphertext is:\n");
for (int i = 0; i < ciphertext_len; i++) {
printf("%02x", ciphertext[i]);
}
printf("\n");
printf("AES-GCM Decryptedtext is:\n");
for (int i = 0; i < decryptedtext_len; i++) {
printf("%c", decryptedtext[i]);
}
printf("\n");
// 释放内存
free(ciphertext);
free(decryptedtext);
EVP_CIPHER_CTX_free(ctx);
}
// AES-ECB加密解密
void aes_ecb_encrypt_decrypt() {
// 定义key和iv
unsigned char key[16] = {0x0};
unsigned char iv[16] = {0x0};
// 待加密的明文
unsigned char plaintext[] = "Hello, World!";
int plaintext_len = strlen(plaintext);
// 分配内存
unsigned char *ciphertext = malloc(plaintext_len + 16);
unsigned char *decryptedtext = malloc(plaintext_len);
// 创建并初始化EVP_CIPHER_CTX
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
EVP_EncryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, key, iv);
// 加密
int len;
EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len);
int ciphertext_len = len;
EVP_EncryptFinal_ex(ctx, ciphertext + len, &len);
ciphertext_len += len;
// 解密
EVP_DecryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, key, iv);
EVP_DecryptUpdate(ctx, decryptedtext, &len, ciphertext, ciphertext_len);
int decryptedtext_len = len;
EVP_DecryptFinal_ex(ctx, decryptedtext + len, &len);
decryptedtext_len += len;
// 打印结果
printf("AES-ECB Ciphertext is:\n");
for (int i = 0; i < ciphertext_len; i++) {
printf("%02x", ciphertext[i]);
}
printf("\n");
printf("AES-ECB Decryptedtext is:\n");
for (int i = 0; i < decryptedtext_len; i++) {
printf("%c", decryptedtext[i]);
}
printf("\n");
// 释放内存
free(ciphertext);
free(decryptedtext);
EVP_CIPHER_CTX_free(ctx);
}
```
使用示例:
```objc
aes_gcm_encrypt_decrypt();
aes_ecb_encrypt_decrypt();
```
输出结果:
```
AES-GCM Ciphertext is:
9a0c9e714a7f48c8bdf7ce70d2c5b6b801efb4c6a2f8d0c0e1c9e38d8d0e
AES-GCM Decryptedtext is:
Hello, World!
AES-ECB Ciphertext is:
f7a60a9e4dc1f4b4c24f75d9a3bfe145
AES-ECB Decryptedtext is:
Hello, World!
```
以上代码仅供参考,实际使用时需要根据具体需求进行调整和优化。