skip to Main Content

`If my path URL is https://example.in/home/page1 I expect it to be https://example.in/iuwrhwie%62jwef

I tried encodeURIComponent

name = encodeURIComponent("engine & machinery");
url = `/project/${encodeURIComponent('R&D Some Other Text')}`;
dashboardUrl = '/dashboard/?isopen=true&name=' + name;

2

Answers


  1.     import { Component } from '@angular/core';
        import { Router, NavigationEnd } from '@angular/router';
        import CryptoJS from 'crypto-js';
        
        @Component({
          selector: 'app-root',
          templateUrl: './app.component.html',
          styleUrls: ['./app.component.css']
        })
        export class AppComponent {
          constructor(private router: Router) {
            this.router.events.subscribe(event => {
              if (event instanceof NavigationEnd) {
                const encryptedUrl = this.encryptUrl(event.urlAfterRedirects);
                this.router.navigateByUrl('/' + encryptedUrl);
              }
            });
          }
        
          encryptUrl(url: string): string {
            const encrypted = CryptoJS.AES.encrypt(url, 'secret_key').toString();
            return encodeURIComponent(encrypted);
          }
        
          decryptUrl(encryptedUrl: string): string {
            const decryptedBytes = CryptoJS.AES.decrypt(decodeURIComponent(encryptedUrl), 'secret_key');
            return decryptedBytes.toString(CryptoJS.enc.Utf8);
          }
        }
        import { Component } from '@angular/core';
    import * as CryptoJS from 'crypto-js';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent {
      constructor() {
        const originalUrl = 'https://example.in/home/page1';
        const encryptedUrl = this.encryptUrl(originalUrl);
        console.log(encryptedUrl); // Output: https://example.in/iuwrhwie%62jwef
      }
    
      encryptUrl(url: string): string {
        const hash = CryptoJS.SHA256(url);
        const hex = hash.toString(CryptoJS.enc.Hex);
        const shortenedHash = hex.substr(0, 16);
        const hashedUrl = url + '/' + shortenedHash;
        return hashedUrl;
      }
    }
    

    Try with CryptoJS

    Login or Signup to reply.
  2. here is the example code, you may use according to your requirements.

    1st one is by simple one with no special encoding

    So Special Encoding

    2nd one is use special encodeURIComponent or decodeURIComponent

    With EncodeURIComponent

    To Print Output in our screen , we add variables in ‘app.component.html’ file:

    OutPut Screen

    https://codesandbox.io/p/devbox/confident-oskar-hwr3wr?file=%2Fsrc%2Fapp%2Fapp.component.ts%3A7%2C38

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search