1570af302Sopenharmony_ci/**
2570af302Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3570af302Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4570af302Sopenharmony_ci * you may not use this file except in compliance with the License.
5570af302Sopenharmony_ci * You may obtain a copy of the License at
6570af302Sopenharmony_ci *
7570af302Sopenharmony_ci *   http://www.apache.org/licenses/LICENSE-2.0
8570af302Sopenharmony_ci *
9570af302Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10570af302Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11570af302Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12570af302Sopenharmony_ci * See the License for the specific language governing permissions and
13570af302Sopenharmony_ci * limitations under the License.
14570af302Sopenharmony_ci */
15570af302Sopenharmony_ci
16570af302Sopenharmony_ci#include <unistd.h>
17570af302Sopenharmony_ci#include <stdlib.h>
18570af302Sopenharmony_ci#include <stdio.h>
19570af302Sopenharmony_ci#include <string.h>
20570af302Sopenharmony_ci#include <crypt.h>
21570af302Sopenharmony_ci#include "functionalext.h"
22570af302Sopenharmony_ci
23570af302Sopenharmony_ci#define BITS_COUNT_OF_ONE_BYTE 8
24570af302Sopenharmony_ci#define LENGTH 8
25570af302Sopenharmony_ci
26570af302Sopenharmony_ci// 验证加解密之后是否还与原文相同
27570af302Sopenharmony_cistatic void test_encrypt_and_decrypt(void)
28570af302Sopenharmony_ci{
29570af302Sopenharmony_ci    char keytext[] = "ABCDEFGH";
30570af302Sopenharmony_ci    char key[65];
31570af302Sopenharmony_ci    char plaintext[] = "HELLO123";
32570af302Sopenharmony_ci    char buf[65];
33570af302Sopenharmony_ci    char block_cipher[9];
34570af302Sopenharmony_ci    char block_decrypt[9];
35570af302Sopenharmony_ci
36570af302Sopenharmony_ci    // 处理密钥
37570af302Sopenharmony_ci    for (int i = 0; i < LENGTH; i++) {
38570af302Sopenharmony_ci        for (int j = 0; j < BITS_COUNT_OF_ONE_BYTE; j++) {
39570af302Sopenharmony_ci            key[i * BITS_COUNT_OF_ONE_BYTE + j] = (keytext[i] >> j) & 1;
40570af302Sopenharmony_ci        }
41570af302Sopenharmony_ci    }
42570af302Sopenharmony_ci
43570af302Sopenharmony_ci    // 将明文处理为二进制
44570af302Sopenharmony_ci    for (int i = 0; i < LENGTH; i++) {
45570af302Sopenharmony_ci        for (int j = 0; j < BITS_COUNT_OF_ONE_BYTE; j++) {
46570af302Sopenharmony_ci            buf[i * BITS_COUNT_OF_ONE_BYTE + j] = (plaintext[i] >> j) & 1;
47570af302Sopenharmony_ci        }
48570af302Sopenharmony_ci        setkey(key);
49570af302Sopenharmony_ci    }
50570af302Sopenharmony_ci    printf("Before encrypting: %s\n", plaintext);
51570af302Sopenharmony_ci
52570af302Sopenharmony_ci    // 加密并将二进制数据处理为字符串
53570af302Sopenharmony_ci    encrypt(buf, 0);
54570af302Sopenharmony_ci    for (int i = 0; i < LENGTH; i++) {
55570af302Sopenharmony_ci        block_cipher[i] = '\0';
56570af302Sopenharmony_ci        for (int j = 0; j < BITS_COUNT_OF_ONE_BYTE; j++) {
57570af302Sopenharmony_ci            block_cipher[i] |= buf[i * BITS_COUNT_OF_ONE_BYTE + j] << j;
58570af302Sopenharmony_ci        }
59570af302Sopenharmony_ci        block_cipher[LENGTH] = '\0';
60570af302Sopenharmony_ci    }
61570af302Sopenharmony_ci    printf("After encrypting:  %s\n", block_cipher);
62570af302Sopenharmony_ci
63570af302Sopenharmony_ci    // 解密并将二进制数据处理为字符串
64570af302Sopenharmony_ci    encrypt(buf, 1);
65570af302Sopenharmony_ci    for (int i = 0; i < LENGTH; i++) {
66570af302Sopenharmony_ci        block_decrypt[i] = '\0';
67570af302Sopenharmony_ci        for (int j = 0; j < BITS_COUNT_OF_ONE_BYTE; j++) {
68570af302Sopenharmony_ci            block_decrypt[i] |= buf[i * BITS_COUNT_OF_ONE_BYTE + j] << j;
69570af302Sopenharmony_ci        }
70570af302Sopenharmony_ci        block_decrypt[LENGTH] = '\0';
71570af302Sopenharmony_ci    }
72570af302Sopenharmony_ci    printf("After decrypting:  %s\n", block_decrypt);
73570af302Sopenharmony_ci    EXPECT_STREQ("test_encrypt_and_decrypt", plaintext, block_decrypt);
74570af302Sopenharmony_ci}
75570af302Sopenharmony_ci
76570af302Sopenharmony_ciint main(void)
77570af302Sopenharmony_ci{
78570af302Sopenharmony_ci    test_encrypt_and_decrypt();
79570af302Sopenharmony_ci    return t_status;
80570af302Sopenharmony_ci}