RSA Encryption example


The index.html file containing JavaScript that makes use of the RSA Encryption API functionality is listed below.

<!DOCTYPE html>
<html manifest="manifest.appcache">
  <head>
    <title>Hybrid Client RSA Encryption API usage example</title>
    <link rel="x-antenna-managed-webapp-descriptor" href="webapp-descriptor.xml" />
  </head>

  <body>
    <p>
      <table border="0">
        <tr>
          <td>Key Size:</td>
          <td><input type="text" name="keySize" id="keySize" size="38" maxlength="16" value="1024"/>
		  </td>
        </tr>
        <tr>
          <td>Message:</td>
          <td><input type="text" name="dataString" id="dataString" size="38" maxlength="100"
                        value="Some String...." />
          </td>
        </tr>
      </table>
    </p>
    <p>
      <input type="button"
             onclick="generateKeys(
               window.launchbox.encryption.RSA.PEM_FORMAT
			   );"value="Generate RSA Key Pair (PEM)" />
      <input type="button"
             onclick="generateKeys(
               window.launchbox.encryption.RSA.DER_FORMAT);"
               value="Generate RSA Key Pair (DER)" />
      <input type="button" onclick="encrypt();" value="Encrypt" />
      <input type="button" onclick="decrypt();" value="Decrypt" />
      <input type="button" onclick="encryptBinary();" value="EncryptBinary" />
      <input type="button" onclick="decryptBinary();" value="DecryptBinary" />
    </p>
    Public Key: <div id="publicKey"></div><hr/>
    Private Key: <div id="privateKey"></div><hr/>
    Encrypted: <div id="encrypted"></div><hr/>
    Decrypted: <div id="decrypted"></div><hr/>
    EncryptedBinary: <div id="encryptedBinary"></div><hr/>
    DecryptedBinary: <div id="decryptedBinary"></div><hr/>

    <script>
      function generateKeys(format) {
        var keySize = parseInt(document.getElementById('keySize').value);
          window.launchbox.encryption.RSA.generateKeys(keySize, format, {
            onSuccess: function (args) {
              console.log('RSA gen success: ' + args);
              console.log('public key: ' + args.publicKey);
              console.log('private key: ' + args.privateKey);
              document.getElementById('publicKey').innerHTML = 
                args.publicKey;
              document.getElementById('privateKey').innerHTML = 
                args.privateKey;
            },
            onFailure: function (error) {
              console.log('RSA gen failed: ' + error.code);
              document.getElementById('publicKey').innerHTML = error.code;
              document.getElementById('privateKey').innerHTML = error.code;
            }
          });
      }

      function encrypt() {
        var message = document.getElementById('dataString').value;
        var publicKey = document.getElementById('publicKey').innerHTML;
        window.launchbox.encryption.RSA.encrypt(message, publicKey, {
          onSuccess: function (encryptedString) {
            console.log('RSA enc success: ' + encryptedString);
            document.getElementById('encrypted').innerHTML = 
              encryptedString;
          },
          onFailure: function (error) {
            console.log('RSA enc failed: ' + error.code);
            document.getElementById('encrypted').innerHTML = error.code;
          }
        });
      }

      function encryptBinary() {
        var message = document.getElementById('dataString').value;
        var publicKey = document.getElementById('publicKey').innerHTML;
        window.launchbox.encryption.RSA.encryptBinary(message, publicKey, {
          onSuccess: function (encryptedString) {
            console.log('RSA binary enc success: ' + encryptedString);
            document.getElementById('encryptedBinary').innerHTML = 
              encryptedString;
          },
          onFailure: function (error) {
            console.log('RSA binary enc failed: ' + error.code);
              document.getElementById('encryptedBinary').innerHTML = 
                error.code;
          }
        });
      }

      function decrypt() {
        var message = document.getElementById('encrypted').innerHTML;
        var privateKey = document.getElementById('privateKey').innerHTML;
        window.launchbox.encryption.RSA.decrypt(message, privateKey, {
          onSuccess: function (decryptedString) {
            console.log('RSA dec success: ' + decryptedString);
            document.getElementById('decrypted').innerHTML = 
              decryptedString;
          },
          onFailure: function (error) {
            console.log('RSA dec failed: ' + error.code);
            document.getElementById('decrypted').innerHTML = 
              error.code;
          }
        });
      }

      function decryptBinary() {
        var message = document.getElementById('encryptedBinary').innerHTML;
        var privateKey = document.getElementById('privateKey').innerHTML;
        window.launchbox.encryption.RSA.decryptBinary(message, privateKey, {
          onSuccess: function (decryptedString) {
            console.log('RSA binary dec success: ' + decryptedString);
            document.getElementById('decryptedBinary').innerHTML = 
              decryptedString;
          },
          onFailure: function (error) {
            console.log('RSA binary dec failed: ' + error.code);
            document.getElementById('decryptedBinary').innerHTML = 
              error.code;
          }
        });
      }
    </script>
  </body>
</html>

The contents of the cache manifest file called manifest.appcache for this application are listed below:

CACHE MANIFEST

CACHE:
index.html

NETWORK:
*

The webapp-descriptor.xml file for this application is defined in the following way:

<?xml version="1.0" encoding="UTF-8"?>
<webapp-descriptor xmlns="http://www.pega.com/application-hosting/
web-app-descriptor/2.0">
    <id>com.pega.sample.rsaencryption</id>
    <version>1.0.0</version>
    <name>RSA Encryption API usage example</name>
</webapp-descriptor>

Related topics

Public API reference
Legal notice | Copyright © 2017 and Confidential to Pegasystems Inc. All rights reserved
PDN | Feedback
Advanced...