如何在SAP中使用加密并存储在数据库中

问题描述 投票:0回答:1

我的任务是将用户名和密码保存在 SAP 表中。密码应以加密方式 (aes128) 存储在数据库中。发送之前,应再次解密密码。解密不行啊有人有这方面的经验吗

这是我存储数据的源代码:

data : passwd  type string,
       encoded type string,
       decoded type string.


data: lv_plaintext  type xstring.
data: lv_plaintext2 type xstring.
data: lv_key        type xstring.

*----------------------------------------------------------------------*
* Selection-Screen
*----------------------------------------------------------------------*

parameters: p_name type char10.
parameters: p_usern type char10.
parameters: p_pass type text10.
parameters: p_descr type text120.


*----------------------------------------------------------------------*
* At Selection Screen Output
*----------------------------------------------------------------------*
at selection-screen output.
  loop at screen.
    if screen-name = 'P_PASS'.
      screen-invisible = 1.
      modify screen.
    endif.
  endloop.

*----------------------------------------------------------------------*
* Start of Selection
*----------------------------------------------------------------------*
start-of-selection.

* create message
  data: lr_conv_sec type ref to cl_abap_conv_out_ce .
  data: lr_conv_key type ref to cl_abap_conv_out_ce .

  try.
      call method cl_abap_conv_out_ce=>create
        exporting
          encoding    = 'DEFAULT'
*      endian      =
          replacement = '#'
          ignore_cerr = abap_false
        receiving
          conv        =  lr_conv_sec
          .
    catch cx_parameter_invalid_range .
    catch cx_sy_codepage_converter_init .
  endtry.

  lr_conv_sec->write( p_pass ).

* create key
  try.
      call method cl_abap_conv_out_ce=>create
        exporting
          encoding    = 'DEFAULT'
*      endian      =
          replacement = '#'
          ignore_cerr = abap_false
        receiving
          conv        =  lr_conv_key
          .
    catch cx_parameter_invalid_range .
    catch cx_sy_codepage_converter_init .
  endtry.

  lr_conv_key->write( p_pass ).

  try.
    call method lr_conv_sec->get_buffer
      receiving
        buffer = lv_plaintext.
  endtry.

  try.
    call method lr_conv_key->get_buffer
      receiving
        buffer = lv_key.
  endtry.


  data: lv_message type xstring.

* encrypt using AES256
  call method cl_sec_sxml_writer=>encrypt
    exporting
      plaintext  = lv_plaintext
      key        = lv_key
      algorithm  = cl_sec_sxml_writer=>co_aes128_algorithm
    importing
      ciphertext = lv_message.

  data: ls_pwdstore type /vaps/pwdstore.
  ls_pwdstore-name        = p_name.
  ls_pwdstore-username    = p_usern.
  ls_pwdstore-pwd         = lv_message.
  ls_pwdstore-pwdkey      = lv_key.
  ls_pwdstore-description = p_descr.


  modify /vaps/pwdstore from ls_pwdstore.

  write:/ 'Name', ' : ', ls_pwdstore-name.
  write:/ 'Benutzername', ' : ',  ls_pwdstore-username.
  write:/ 'Passwort', ' : ', ls_pwdstore-pwd.
  write:/ 'PWDKEY', ' : ', ls_pwdstore-pwdkey.

这是显示代码:

data : passwd  type string,
       encoded type string,
       decoded type string.

data: lv_plaintext  type xstring.
data: lv_plaintext2 type xstring.
data: lv_key        type xstring.

*----------------------------------------------------------------------*
* Selection-Screen
*----------------------------------------------------------------------*

parameters: p_name type char10.


*----------------------------------------------------------------------*
* At Selection Screen Output
*----------------------------------------------------------------------*
at selection-screen output.

*----------------------------------------------------------------------*
* Start of Selection
*----------------------------------------------------------------------*
start-of-selection.

  data: lv_message type xstring.
  data: ls_pwdstore type /vaps/pwdstore.

  data: lv_length type i,
        e_string type string,
         lt_binary type standard table of x255.


  select single * from /vaps/pwdstore into ls_pwdstore
    where name = p_name.

  lv_key = ls_pwdstore-pwdkey.
  lv_message = ls_pwdstore-pwd.

  call method cl_sec_sxml_writer=>decrypt
    exporting
      ciphertext = lv_message
      key        = lv_key
      algorithm  = cl_sec_sxml_writer=>co_aes128_algorithm
    importing
      plaintext  = lv_plaintext2.



  call function 'SCMS_XSTRING_TO_BINARY'
    exporting
      buffer        = lv_plaintext2
    importing
      output_length = lv_length
    tables
      binary_tab    = lt_binary.

  call function 'SCMS_BINARY_TO_STRING'
    exporting
      input_length = lv_length
    importing
      text_buffer  = e_string
    tables
      binary_tab   = lt_binary
    exceptions
      failed       = 1
      others       = 2.
  if sy-subrc <> 0.
*     Implement suitable error handling here
  endif.

  write:/ 'Name', ' : ', p_name.
  write:/ 'PWD', ' : ', e_string.

表格列

PWD
PWDKEY
定义为
RWASTRING

enter image description here

encryption abap sap-basis
1个回答
0
投票

我创建了示例代码,它看起来可以工作。也许您可以检查您的数据库例程,也许您在数据库操作期间丢失了一些数据。

REPORT zmky_enc_dec.
DATA: lv_key        TYPE xstring,
      lv_plaintext  TYPE xstring,
      lv_plaintext2 TYPE xstring,
      lv_message    TYPE xstring,
      lv_length     TYPE i,
      e_string      TYPE string,
      lv_ciphertext TYPE xstring.

DATA:  lt_binary TYPE STANDARD TABLE OF x255.

lv_plaintext = '646570'.
lv_key = '5A1F47FE14F72'.

* encription

* encrypt using AES256
CALL METHOD cl_sec_sxml_writer=>encrypt
  EXPORTING
    plaintext  = lv_plaintext
    key        = lv_key
    algorithm  = cl_sec_sxml_writer=>co_aes128_algorithm
  IMPORTING
    ciphertext = lv_ciphertext.

* decription
lv_ciphertext = 'D8B7F50D13B58F8C0B9B50A59891ED2F1C368D6F2DB97A789BDAC131EE346CE8'.
CALL METHOD cl_sec_sxml_writer=>decrypt
  EXPORTING
    ciphertext = lv_ciphertext
    key        = lv_key
    algorithm  = cl_sec_sxml_writer=>co_aes128_algorithm
  IMPORTING
    plaintext  = lv_plaintext2.

CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
  EXPORTING
    buffer        = lv_plaintext2
  IMPORTING
    output_length = lv_length
  TABLES
    binary_tab    = lt_binary.

CALL FUNCTION 'SCMS_BINARY_TO_STRING'
  EXPORTING
    input_length = lv_length
  IMPORTING
    text_buffer  = e_string
  TABLES
    binary_tab   = lt_binary
  EXCEPTIONS
    failed       = 1
    OTHERS       = 2.

WRITE:/ 'PWD', ' : ', e_string.
© www.soinside.com 2019 - 2024. All rights reserved.