RSA Encryption example


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<!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:

1
2
3
4
5
6
7
CACHE MANIFEST
 
CACHE:
index.html
 
NETWORK:
*

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

1
2
3
4
5
6
7
<?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 © 2015 and Confidential to Pegasystems Inc. All rights reserved. | Feedback
Advanced...