SJIS and BASE64 conversions with javascript

Asked 1 years ago, Updated 1 years ago, 31 views

Convert the value (UTF-16) from the screen to SJIS and use it after hash code conversion.
Perform a BASE64 conversion and re-use (POST to an external system)
Eventually, the value received by the external system will be ???
Please let me know if there is anything wrong with the SJIS conversion or BASE64 conversion.

I tried to make a try & error such as performing a conversion with an array, but I can't do it properly.
We use encoding.js published on GitHub for SJIS conversion and crypto.js for hash value generation.

■ Excerpts of only the relevant parts

// Get screen settings (hoge1,2 is alphanumeric, itemName only mixed in full-width)
let hoge1 = 'hoge1';
let hoge2 = 'hoge2';
letitemName='Product Name'

// SJIS Conversion (using encoding.js as external js)
hoge1 = Encoding.convert(hoge1, 'SJIS');
hoge2 = Encoding.convert(hoge2, 'SJIS');
itemName = Encoding.convert(itemName, 'SJIS');

// Concatenate all items after SJIS conversion
let result=hoge1+hoge2+itemName; 

// Converts concatenated values to hash values (using crypto-js as external js)
letspsHashcode=new CryptoJS.SHA1(result)

// Create because you need to post in xml format to an external system
// Items containing full-width characters must be converted to BASE64
let postData=';
postData='<?xml version="1.0" encoding="Shift_JIS"?>';
postData+='<request id="XXXXX">';
postData+='<hoge1>'+hoge1+'</hoge1>';
postData+='<hoge2>'+hoge2+'</hoge2>';
postData+='<item_name>'+btoa(unescape(encodeURIComponent(itemName))))+'</item_name>'; 
postData+='<sps_hashcode>'+spsHashcode+'</sps_hashcode>';

(POST postData to external system)

javascript

2022-09-30 20:24

1 Answers

(I can't confirm the problem resolution, so this is a guess.)

https://github.com/polygonplanet/encoding.js/If this is the case, itemName=Encoding.convert(itemName, 'SJIS'); results in itemName expressing shift JIS byte strings as U+00~UFF strings.It's

btoa(unescape(encodeURIComponent(itemName)))

The encodeURIComponent() then forces the itemName to re-encode to UTF-8 and percent-encode it as ISO-8859-1.As a result, non-askey characters are corrupted.

Probably

btoa(itemName)

"I think it will be the desired ""String with base 64 shift JIS""

"


2022-09-30 20:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.