打印
[Security]

st加密库V3.0.0+AES256

[复制链接]
99|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
1021256354|  楼主 | 2025-6-10 10:42 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
加解密数据不对
status = STM32_AES_ECB_Encrypt( (uint8_t *) Plaintext, 16, Key, OutputMessage,
                                  &OutputMessageLength);
status = STM32_AES_ECB_Decrypt( (uint8_t *) Expected_Ciphertext, 16, Key, OutputMessage,
                                  &OutputMessageLength);

int32_t STM32_AES_ECB_Encrypt(uint8_t* InputMessage,
                              uint32_t InputMessageLength,
                              uint8_t  *AES256_Key,
                              uint8_t  *OutputMessage,
                              uint32_t *OutputMessageLength)
{
  AESECBctx_stt AESctx;

  uint32_t error_status = AES_SUCCESS;

  int32_t outputLength = 0;

  /* Set flag field to default value */
  AESctx.mFlags = E_SK_DEFAULT;

  /* Set key size to 32 (corresponding to AES-256) */
  AESctx.mKeySize = 32;

  /* Initialize the operation, by passing the key.
   * Third parameter is NULL because ECB doesn't use any IV */
  error_status = AES_ECB_Encrypt_Init(&AESctx, AES256_Key, NULL );

  /* check for initialization errors */
  if (error_status == AES_SUCCESS)
  {
    /* Encrypt Data */
    error_status = AES_ECB_Encrypt_Append(&AESctx,
                                          InputMessage,
                                          InputMessageLength,
                                          OutputMessage,
                                          &outputLength);

    if (error_status == AES_SUCCESS)
    {
      /* Write the number of data written*/
      *OutputMessageLength = outputLength;
      /* Do the Finalization */
      error_status = AES_ECB_Encrypt_Finish(&AESctx, OutputMessage + *OutputMessageLength, &outputLength);
      /* Add data written to the information to be returned */
      *OutputMessageLength += outputLength;
    }
  }

  return error_status;
}

int32_t STM32_AES_ECB_Decrypt(uint8_t* InputMessage,
                              uint32_t InputMessageLength,
                              uint8_t  *AES256_Key,
                              uint8_t  *OutputMessage,
                              uint32_t *OutputMessageLength)
{
  AESECBctx_stt AESctx;

  uint32_t error_status = AES_SUCCESS;

  int32_t outputLength = 0;

  /* Set flag field to default value */
  AESctx.mFlags = E_SK_DEFAULT;

  /* Set key size to 32 (corresponding to AES-256) */
  AESctx.mKeySize = 32;

  /* Initialize the operation, by passing the key.
   * Third parameter is NULL because ECB doesn't use any IV */
  error_status = AES_ECB_Decrypt_Init(&AESctx, AES256_Key, NULL );

  /* check for initialization errors */
  if (error_status == AES_SUCCESS)
  {
    /* Decrypt Data */
    error_status = AES_ECB_Decrypt_Append(&AESctx,
                                          InputMessage,
                                          InputMessageLength,
                                          OutputMessage,
                                          &outputLength);

    if (error_status == AES_SUCCESS)
    {
      /* Write the number of data written*/
      *OutputMessageLength = outputLength;
      /* Do the Finalization */
      error_status = AES_ECB_Decrypt_Finish(&AESctx, OutputMessage + *OutputMessageLength, &outputLength);
      /* Add data written to the information to be returned */
      *OutputMessageLength += outputLength;
    }
  }

  return error_status;
}

使用特权

评论回复
沙发
1021256354|  楼主 | 2025-6-10 10:43 | 只看该作者
uint8_t Plaintext[16] =
  {//
   1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8
  };
uint8_t Key[CRL_AES256_KEY] =
  {
    0x31, 0x32, 0x33, 0x00, 0x0, 0x0, 0x0, 0x0,
    0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
    0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
    0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  };

使用特权

评论回复
板凳
1021256354|  楼主 | 2025-6-10 10:43 | 只看该作者
const uint8_t Expected_Ciphertext[16] =
  {
        0x69,0xB6,0xB5,0x3F,0xBB,0xDD,0xB4,0x6B,0x38,0xDE,0xF5,0x5B,0xB2,0x51,0xCF,0x25,
  };

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

151

主题

648

帖子

3

粉丝