// This is our Secret Message ChiperText
let data = "THIS IS MY SECRET KEY";
//initialization vector for additional secure
let iv = 'ThisIs33221321OurIV1ar234567';
//Your key length should be 32 characters, you should generate your own random key every time
let key = 'THISISOURKEY1234567WORKSTATION01';
// prepare key & iv for CryptoJS encryption
let fkey = CryptoJS.enc.Utf8.parse(key);
let fiv = CryptoJS.enc.Utf8.parse(iv);
//Encyrption
//We access the library with calling "CryptoJS"
//and it returns the encrypted value in "enc.ciphertext"
let enc = CryptoJS.AES.encrypt(data, fkey, {
iv: fiv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
});
let aFilters = [];
//Call your oData Service to send the encrypted text
//We used here just one Filter with name of "KEY"
//To send the text we change the format of "enc.ciphertext" as string
aFilters.push(new Filter("KEY", FilterOperator.EQ, enc.ciphertext.toString()));
this.getView().getModel().read("/CryptoSet", {
filters: aFilters,
success: function (oData, response) {
//do something
}.bind(this),
error: function (oError) {
//do something
}.bind(this)
});
"Encryption Parameters
DATA: lt_binary TYPE STANDARD TABLE OF x255.
DATA: i_xstring TYPE xstring,
lx_plaintext TYPE xstring,
i_iv TYPE string,
i_ivx TYPE xstring,
i_key_xstring TYPE xstring,
i_key TYPE string,
lv_text_dec TYPE string,
*this is our encrypted text from Fiori
lv_ciphertext TYPE string.
*>> Set KEY & IV parameters
i_iv = 'ThisIs33221321OurIV1ar234567'.
i_key = 'THISISOURKEY1234567WORKSTATION01'.
*Convert all to xstring format
i_xstring = lv_ciphertext.
CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
EXPORTING
text = i_key
IMPORTING
buffer = i_key_xstring
EXCEPTIONS
failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
EXPORTING
text = i_iv
IMPORTING
buffer = i_ivx
EXCEPTIONS
failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
"Add IV to ciphertext
CONCATENATE i_ivx(16) i_xstring INTO i_xstring IN BYTE MODE.
IF i_xstring IS NOT INITIAL AND
i_key_xstring IS NOT INITIAL AND
i_ivx IS NOT INITIAL.
TRY.
" DECRYPT WITH AES256 ALGORITHM
cl_sec_sxml_writer=>decrypt(
EXPORTING ciphertext = i_xstring key = i_key_xstring algorithm = cl_sec_sxml_writer=>co_aes256_algorithm
IMPORTING plaintext = lx_plaintext ).
CATCH cx_sec_sxml_encrypt_error INTO DATA(oref).
ENDTRY.
ENDIF.
* " convert xstring to string for output
lv_text_dec = /ui2/cl_abap2json=>conv_xstring_to_string( lx_plaintext ).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
7 | |
5 | |
5 | |
4 | |
4 | |
4 | |
4 | |
4 | |
3 | |
3 |