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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript"
src="../../../../dojo/dojo.js" djConfig="isDebug: false"></script>
<script type="text/javascript" src="../../../../dojox/off/offline.js"></script>
<style type="text/css">
body{
padding: 2em;
}
#dataTable{
margin-top: 2em;
}
button{
margin-left: 1em;
}
th, tr, td{
text-align: left;
}
table{
text-align: center;
clear: both;
}
#cryptoContainer{
float: left;
width: 60%;
}
#numRowsContainer{
float: right;
width: 40%;
}
#numRowsContainer input{
margin-left: 1.5em;
width: 5em;
}
.table-columns{
font-weight: bold;
}
</style>
<script>
dojo.require("dojox.sql");
dojo.connect(window, "onload", function(){
// draw our customer table on the screen
createTable();
// create our customer table in the database
dojox.sql("DROP TABLE IF EXISTS CUSTOMERS");
dojox.sql("CREATE TABLE CUSTOMERS ("
+ "last_name TEXT, "
+ "first_name TEXT, "
+ "social_security TEXT"
+ ")"
);
});
function createTable(){
// get number of rows to create
var NUM_ROWS = document.getElementById("numRows").value;
if(!NUM_ROWS){
alert("Please enter the number of "
+ "customer rows the table should have");
return;
}
var table = document.getElementById("dataTable");
if(table){
table.parentNode.removeChild(table);
}
table = document.createElement("table");
table.setAttribute("id", "dataTable");
table.setAttribute("border", 1);
// if we don't use IE's craptacular proprietary table methods
// we get strange display glitches
var tr = (dojo.isIE) ? table.insertRow() : document.createElement("tr");
tr.className = "table-columns";
var th = (dojo.isIE) ? tr.insertCell() : document.createElement("th");
th.appendChild(document.createTextNode("Last Name"));
if(!dojo.isIE){
tr.appendChild(th);
}
th = (dojo.isIE) ? tr.insertCell() : document.createElement("th");
th.appendChild(document.createTextNode("First Name"));
if(!dojo.isIE){
tr.appendChild(th);
}
th = (dojo.isIE) ? tr.insertCell() : document.createElement("th");
th.appendChild(document.createTextNode("Social Security"));
if(!dojo.isIE){
tr.appendChild(th);
table.appendChild(tr);
}
for(var i = 1; i <= NUM_ROWS; i++){
tr = (dojo.isIE) ? table.insertRow() : document.createElement("tr");
tr.className = "data-item";
var elem = (dojo.isIE) ? tr.insertCell() : document.createElement("td");
elem.className = "last-name";
var lastName = "Doe" + i;
elem.appendChild(document.createTextNode(lastName));
if(!dojo.isIE){
tr.appendChild(elem);
}
elem = (dojo.isIE) ? tr.insertCell() : document.createElement("td");
elem.className = "first-name";
var firstName = "John" + i;
elem.appendChild(document.createTextNode(firstName));
if(!dojo.isIE){
tr.appendChild(elem);
}
elem = elem = (dojo.isIE) ? tr.insertCell() : document.createElement("td");
elem.className = "social-security";
var ss = 513121500 + i;
ss = new String(ss);
ss = ss.slice(0, 3) + "-" + ss.slice(3, 5) + "-" + ss.slice(5);
elem.appendChild(document.createTextNode(ss));
if(!dojo.isIE){
tr.appendChild(elem);
table.appendChild(tr);
}
}
document.body.appendChild(table);
// reset button state
dojo.byId("encrypt").disabled = false;
dojo.byId("decrypt").disabled = true;
}
function readTable(){
var data = [];
var rows = dojo.query(".data-item");
dojo.forEach(rows, function(row){
var td = row.getElementsByTagName("td");
var lastName = td[0].childNodes[0].nodeValue;
var firstName = td[1].childNodes[0].nodeValue;
var ssNumber = td[2].childNodes[0].nodeValue;
data.push({lastName: lastName, firstName: firstName, ssNumber: ssNumber,
toString: function(){
return "{lastName: " + lastName
+ ", firstName: " + firstName
+ ", ssNumber: " + ssNumber
+ "}";
}});
});
return data;
}
function setData(data){
var rows = document.getElementsByTagName("tr");
for(var i = 1; i < rows.length; i++){
var customer = data[i - 1];
var td = rows[i].getElementsByTagName("td");
td[2].childNodes[0].nodeValue = customer.social_security;
}
}
function encrypt(){
// disable our buttons
dojo.byId("encrypt").disabled = true;
dojo.byId("decrypt").disabled = true;
var data = readTable();
var password = document.getElementById("password").value;
// delete any old data
dojox.sql("DELETE FROM CUSTOMERS");
// insert new data
insertCustomers(data, 0, password);
}
function insertCustomers(data, i, password){
var nextIndex = i + 1;
if(i >= data.length){
var savedRows = dojox.sql("SELECT * FROM CUSTOMERS");
setData(savedRows);
return;
}
dojox.sql("INSERT INTO CUSTOMERS VALUES (?, ?, ENCRYPT(?))",
data[i].lastName, data[i].firstName,
data[i].ssNumber,
password,
function(results, error, errorMsg){
// enable our buttons
dojo.byId("encrypt").disabled = true;
dojo.byId("decrypt").disabled = false;
if(error == true){
alert(errorMsg);
return;
}
insertCustomers(data, nextIndex, password);
}
);
}
function decrypt(){
// disable our buttons
dojo.byId("encrypt").disabled = true;
dojo.byId("decrypt").disabled = true;
var password = document.getElementById("password").value;
dojox.sql("SELECT last_name, first_name, DECRYPT(social_security) FROM CUSTOMERS",
password,
function(results, error, errorMsg){
// enable our buttons
dojo.byId("encrypt").disabled = false;
dojo.byId("decrypt").disabled = true;
if(error == true){
alert(errorMsg);
return;
}
setData(results);
}
);
}
</script>
</head>
<body>
<h1>Dojo SQL Cryptography</h1>
<h2>Instructions</h2>
<p>This demo shows Dojo Offline's SQL encryption technologies. In the table below, we have a
sample SQL table that has three columns of data: a last name, a first name, and
a social security number. We don't want to store the social security numbers
in the clear, just in case they are downloaded for offline use to a laptop and the
laptop is stolen.</p>
<p>To use this demo, enter a password and press the ENCRYPT button to see the Social Security column encrypt. Enter
the same password and press DECRYPT to see it decrypt. If you enter an incorrect password and
press DECRYPT, the Social Security column will remain encrypted and only show gibberish.</p>
<p>Under the covers we use 256-bit AES encryption and your password to derive the crypto key; we use
a facility in Google Gears to do the cryptography in such a way that the browser does not lock up
during processing. Dojo Offline ties this cryptography into Dojo SQL, providing convenient ENCRYPT()
and DECRYPT() SQL keywords you can use to easily have this functionality in your
own offline applications. To learn how you can use this feature
<a href="http://docs.google.com/View?docid=dhkhksk4_8gdp9gr#crypto" target="_blank">see here</a>.</p>
<div id="cryptoContainer">
<label for="password">
Password:
</label>
<input type="input" name="password" id="password" value="sample_password">
<button id="encrypt" onclick="window.setTimeout(encrypt, 1)">Encrypt</button>
<button id="decrypt" onclick="window.setTimeout(decrypt, 1)" disabled="true">Decrypt</button>
</div>
<div id="numRowsContainer">
<label for="numRows">
Number of Customer Rows in Table:
</label>
<input id="numRows" type="input" value="30">
<button onclick="createTable()">Update</button>
</div>
</body>
</html>
|