js_lis 1.0.15 → 1.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/VirtualKeyboard.js +30 -65
  2. package/package.json +1 -1
@@ -12,17 +12,16 @@ export class VirtualKeyboard {
12
12
  this.offsetY = 0;
13
13
  this.shiftActive = false;
14
14
  this.capsLockActive = false;
15
- this.secretKey = '1234567890abcdef'; // คีย์ที่ใช้ในการเข้ารหัส
16
- // ใช้ Web Crypto API สำหรับการจัดการคีย์
17
- this.cryptoKey = null;
18
- this.iv = window.crypto.getRandomValues(new Uint8Array(12)); // สร้าง IV สำหรับการเข้ารหัส/ถอดรหัส
19
-
15
+
16
+ // กำหนดคีย์และ IV สำหรับการเข้ารหัส
17
+ this.secretKey = '1234567890abcdef1234567890abcdef'; // คีย์ความยาว 32 bytes
18
+ this.iv = CryptoJS.lib.WordArray.random(16); // สร้าง IV ความยาว 16 bytes
19
+
20
20
  this.initialize();
21
21
  }
22
22
 
23
23
  async initialize() {
24
24
  try {
25
- await this.generateKey();
26
25
  this.render();
27
26
  this.initializeInputListeners();
28
27
  console.log("VirtualKeyboard initialized successfully.");
@@ -30,65 +29,30 @@ export class VirtualKeyboard {
30
29
  console.error("Error initializing VirtualKeyboard:", error);
31
30
  }
32
31
  }
33
-
34
32
 
35
- async generateKey() {
36
- if (window.crypto && window.crypto.subtle) {
37
- try {
38
- this.cryptoKey = await window.crypto.subtle.generateKey(
39
- { name: "AES-GCM", length: 256 },
40
- true,
41
- ["encrypt", "decrypt"]
42
- );
43
- } catch (error) {
44
- console.error("Error generating key:", error);
45
- throw error;
46
- }
47
- } else {
48
- console.error("Web Crypto API is not supported in this environment.");
49
- throw new Error("Web Crypto API is not supported.");
50
- }
51
- }
52
-
53
-
54
- async encodeText(text) {
55
- const encodedData = new TextEncoder().encode(text);
56
- const encryptedData = await window.crypto.subtle.encrypt(
57
- { name: "AES-GCM", iv: this.iv },
58
- this.cryptoKey,
59
- encodedData
60
- );
61
- return this.arrayBufferToBase64(encryptedData); // แปลงเป็น Base64
33
+ encodeText(text) {
34
+ const encrypted = CryptoJS.AES.encrypt(text, CryptoJS.enc.Hex.parse(this.secretKey), {
35
+ iv: this.iv,
36
+ mode: CryptoJS.mode.CBC,
37
+ padding: CryptoJS.pad.Pkcs7
38
+ });
39
+ return {
40
+ encrypted: encrypted.ciphertext.toString(CryptoJS.enc.Base64),
41
+ iv: this.iv.toString(CryptoJS.enc.Base64)
42
+ };
62
43
  }
63
44
 
64
- async decodeText(base64Text) {
65
- const encryptedData = this.base64ToArrayBuffer(base64Text);
66
- const decryptedData = await window.crypto.subtle.decrypt(
67
- { name: "AES-GCM", iv: this.iv },
68
- this.cryptoKey,
69
- encryptedData
45
+ decodeText(encryptedData) {
46
+ const decrypted = CryptoJS.AES.decrypt(
47
+ { ciphertext: CryptoJS.enc.Base64.parse(encryptedData.encrypted) },
48
+ CryptoJS.enc.Hex.parse(this.secretKey),
49
+ {
50
+ iv: CryptoJS.enc.Base64.parse(encryptedData.iv),
51
+ mode: CryptoJS.mode.CBC,
52
+ padding: CryptoJS.pad.Pkcs7
53
+ }
70
54
  );
71
- return new TextDecoder().decode(decryptedData);
72
- }
73
-
74
- arrayBufferToBase64(buffer) {
75
- let binary = '';
76
- const bytes = new Uint8Array(buffer);
77
- const len = bytes.byteLength;
78
- for (let i = 0; i < len; i++) {
79
- binary += String.fromCharCode(bytes[i]);
80
- }
81
- return window.btoa(binary);
82
- }
83
-
84
- base64ToArrayBuffer(base64) {
85
- const binaryString = window.atob(base64);
86
- const len = binaryString.length;
87
- const bytes = new Uint8Array(len);
88
- for (let i = 0; i < len; i++) {
89
- bytes[i] = binaryString.charCodeAt(i);
90
- }
91
- return bytes.buffer;
55
+ return decrypted.toString(CryptoJS.enc.Utf8);
92
56
  }
93
57
 
94
58
  getLayoutName(layout) {
@@ -99,6 +63,7 @@ export class VirtualKeyboard {
99
63
  case 'thSc': return 'Thai scrambled';
100
64
  case 'numpad': return 'Numpad Keyboard';
101
65
  case 'scNum': return 'Scrambled Keyboard';
66
+ default: return 'Unknown Layout';
102
67
  }
103
68
  }
104
69
 
@@ -117,10 +82,10 @@ export class VirtualKeyboard {
117
82
  }
118
83
  }, true);
119
84
 
120
- document.addEventListener('click', (e) => {
121
- const target = document.getElementById("toggle");
122
- target.addEventListener('click', this.toggle.bind(this), { once: true });
123
- });
85
+ const toggle = document.getElementById("toggle");
86
+ if (toggle) {
87
+ toggle.addEventListener('click', this.toggle.bind(this));
88
+ }
124
89
  }
125
90
 
126
91
  setCurrentInput(inputElement) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js_lis",
3
- "version": "1.0.15",
3
+ "version": "1.0.17",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"