How to use Encryption in SAP and Store in Database

I have the task to save Username and Passwords in SAP Table. The Password should be stored encrypted ( aes128) in the database. Before sending, the password should be decrypted again. Decrypting doesn’t work. Does anyone have experience with this?

Hier Sourcecode für Storring the Data:

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.

Here the code for Display:

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.

The Table field PWD and PWDKEY are of Type RWASTRING

enter image description here

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật