Dev : Extensions : Example Code : Encoding: Difference between revisions

From MozillaZine Knowledge Base
Jump to navigationJump to search
m (→‎Encoding data: update link to XHR)
(move to devmo)
 
Line 1: Line 1:
==Encoding data==
See [http://developer.mozilla.org/en/docs/Code_snippets:Miscellaneous#Simple_and_unsafe_encoding Code snippets: Miscellaneous - Simple and unsafe encoding] at [http://developer.mozilla.org MDC].
This is a very simple and unsafe way to encode strings, which you can then send using [http://developer.mozilla.org/en/docs/XMLHttpRequest XMLHttpRequest] or store using [[Dev : Using preferences|preferences]].


The code contains to functions, encrypt and decrypt, and it simply subtracts and adds 23 to the character code. A=64, so the encoded character will be 87.
[[Category:Redirects]]
 
This is not a safe way or anything close to a 128bit encryption, but if you want to stop someone from reading a password in the preferences then it is good.
 
<pre>
function encrypt(val) {
num_out = "";
if(val == ""){
return "";
}else {
str_in = escape(val);
for(i = 0; i < str_in.length; i++) {
num_out += str_in.charCodeAt(i) - 23;
}
return unescape(num_out);
}
}
 
function decrypt(val) {
str_out = "";
if(val == ""){
return "";
}else {
num_out = val; 
for(i = 0; i < num_out.length; i += 2) {
num_in = parseInt(num_out.substr(i,[2])) + 23;
num_in = unescape('%' + num_in.toString(16));
str_out += num_in;
}
return str_out;
}
}
</pre>
 
==Appendix==
 
[[Category:Example code]]

Latest revision as of 20:36, 23 May 2007