473,626 Members | 3,240 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

encrypt a letter in Javascript

Well .. i have a text box .. and in that i will enter a letter say"A"
and in return there should be a message box saying the encrypted value
say "J".
In simple :
how to encrypt a letter in Javascript...?? ?

thanks,
Jayender

May 25 '06 #1
8 1869
ja*********@gma il.com writes:
Well .. i have a text box .. and in that i will enter a letter say"A"
and in return there should be a message box saying the encrypted value
say "J".
Why "J"?
In simple :
how to encrypt a letter in Javascript...?? ?


It's really simple:
----
<script type="text/javascript">
function encrypt(text) {
// your encryption algorithm here
}
function update(input) {
setTimeout(func tion(){
document.getEle mentById("outpu t").value = encrypt(input.v alue);
},1);
}
</script>
<textarea
onkeypress="upd ate(this)">
</textarea>

<textarea id="output"></textarea>
----

You will have to specify which letters are mapped to which other letter,
i.e., why "A" becomes "J", what "B" becomes, etc.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
May 25 '06 #2
ja*********@gma il.com said the following on 5/25/2006 8:43 AM:
Well .. i have a text box .. and in that i will enter a letter say"A"
and in return there should be a message box saying the encrypted value
say "J".
In simple :
how to encrypt a letter in Javascript...?? ?


You need a lookup table with the values to be transposed:

var lookup = new Object();
lookup[a] = "j";
lookup[A] = "J";

And so on.

Then, you loop through each character of the input's value and convert
it. Or, use the onkeyup event handler to convert it each time a letter
is pressed. That can get messy with the backspace and delete keys though.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 25 '06 #3
Lasse Reichstein Nielsen said the following on 5/25/2006 9:11 AM:
ja*********@gma il.com writes:
Well .. i have a text box .. and in that i will enter a letter say"A"
and in return there should be a message box saying the encrypted value
say "J".


Why "J"?
In simple :
how to encrypt a letter in Javascript...?? ?


It's really simple:
----
<script type="text/javascript">
function encrypt(text) {
// your encryption algorithm here
}
function update(input) {
setTimeout(func tion(){
document.getEle mentById("outpu t").value = encrypt(input.v alue);
},1);
}
</script>
<textarea
onkeypress="upd ate(this)">
</textarea>

<textarea id="output"></textarea>
----

You will have to specify which letters are mapped to which other letter,
i.e., why "A" becomes "J", what "B" becomes, etc.


And decide how to handle the delete and backspace keys :)

Use the onchange and do it only once.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 25 '06 #4
Lasse Reichstein Nielsen wrote:
<script type="text/javascript">
function encrypt(text) {
// your encryption algorithm here
}
function update(input) {
setTimeout(func tion(){
document.getEle mentById("outpu t").value = encrypt(input.v alue);
},1);
}
</script>
<textarea
onkeypress="upd ate(this)">
</textarea>

<textarea id="output"></textarea>


Could you please elaborate on why you deem the setTimeout()
construction and the gEBI reference worm necessary?
PointedEars
--
Indiana Jones: The Name of God. Jehovah.
Professor Henry Jones: But in the Latin alphabet,
"Jehovah" begins with an "I".
Indiana Jones: J-...
May 26 '06 #5
Thomas 'PointedEars' Lahn <Po*********@we b.de> writes:
Could you please elaborate on why you deem the setTimeout()
construction and the gEBI reference worm necessary?


gEBI: It's the simplest way to refer to an element (notice that
neither textarea is inside a form).

setTimeout: The conversion is triggered by a "keypress" event.
This is a cancellable event that happens before the default
behavior (e.g., inserting the pressed letter). The timeout waits
for the default behavior to have happened, so the encryption is
performed on the fully updated input.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
May 27 '06 #6
Lasse Reichstein Nielsen wrote:
Thomas 'PointedEars' Lahn <Po*********@we b.de> writes:
Could you please elaborate on why you deem the setTimeout()
construction and the gEBI reference worm necessary?
gEBI: It's the simplest way to refer to an element (notice that
neither textarea is inside a form).


It would have been better (and more simple) to give the controls a name
(instead of an ID) and to make them part of a form. Because that would
have allowed the developer to use both backwards-compatible and standards
compliant references to access the element objects that represent the
controls in the DOM.

However, even if the markup would be kept as is, certainly it would have
been better to test if gEBI returned a viable reference before using its
return value in a property access. Such reference worms are the direct
result of the ill-advised preconception that what works in one UA also
must work in another.

<URL:http://pointedears.de/scripts/test/whatami#inferen ce>
setTimeout: The conversion is triggered by a "keypress" event.
Yes, it is.
This is a cancellable event that happens before the default
behavior (e.g., inserting the pressed letter).
So one should better had made use of the `keyup' event, instead of
attempting to produce an unreliable delay through an equally unreliable
proprietary host-defined feature.
The timeout waits for the default behavior to have happened,
Not at all. Instead, because it is too small for the minimum time frame
known to be assigned to the process from the scheduler, a one millisecond
timeout will either not cause a delay at all, or delay for an undefined
non-zero number of milliseconds, at least the size of the scheduled time
frame, depending on the implementation and the OS. In that time, if there
was a timing issue (which I doubt), it may as well be that the default
behavior did not happen due to some other factors we cannot know of.
so the encryption is performed on the fully updated input.


Non sequitur.
PointedEars
--
Indiana Jones: The Name of God. Jehovah.
Professor Henry Jones: But in the Latin alphabet,
"Jehovah" begins with an "I".
Indiana Jones: J-...
May 27 '06 #7
Thomas 'PointedEars' Lahn <Po*********@we b.de> writes:
Lasse Reichstein Nielsen wrote: It would have been better (and more simple) to give the controls a name
(instead of an ID) and to make them part of a form. Because that would
have allowed the developer to use both backwards-compatible and standards
compliant references to access the element objects that represent the
controls in the DOM.
That would be a workaround. There is no form because there is no
relevant action attribute to give the form, i.e., the form isn't
really a form, it's just a programming hack to allow another way
to address elements.
However, even if the markup would be kept as is, certainly it would have
been better to test if gEBI returned a viable reference before using its
return value in a property access. Such reference worms are the direct
result of the ill-advised preconception that what works in one UA also
must work in another.
In this case it's te result of deliberatly not writing for non-W3C-DOM
compliant browsers. Should fallback for such browsers be deemed
necessary, obviously it should be added.
<URL:http://pointedears.de/scripts/test/whatami#inferen ce>
No inference here, just assumptions (of requirements being satisfied).
setTimeout: The conversion is triggered by a "keypress" event.
Yes, it is.
This is a cancellable event that happens before the default
behavior (e.g., inserting the pressed letter).


So one should better had made use of the `keyup' event, instead of
attempting to produce an unreliable delay through an equally unreliable
proprietary host-defined feature.


I can see that the keyup event happens after the character is added to
the textarea in my browser. However, since we are mentioning standards,
there is no specification of key events in the DOM2 Events specification,
so that could be a conincidence.

Personally, I would prefer writing the entire text and the pressing a
key to convert it, but the OP asked for continuous conversion.
Another approach would be using setInterval to convert repeatedly with
a small interval. This would be just as non-standard, obviously.
The timeout waits for the default behavior to have happened,


Not at all. Instead, because it is too small for the minimum time frame
known to be assigned to the process from the scheduler, a one millisecond
timeout will either not cause a delay at all, or delay for an undefined
non-zero number of milliseconds, at least the size of the scheduled time
frame, depending on the implementation and the OS.


I assume here that the script is being executed single-threaded, so at
least the timer event won't trigger until after the current event is
finished.
In that time, if there was a timing issue (which I doubt), it may as
well be that the default behavior did not happen due to some other
factors we cannot know of.


It is true that if any other execution comes between the key event and
the timer event, the content of the textarea could have changed. I can
live with that. In fact, it would be desireable that the conversion
happens after any change to the input textarea.

Yet another approach is to have each keypress/keyup schedule a
conversion 500ms later, and having new key events in the meantime
cancel the timer and create a new. Especially if conversion is
time consuming.

The possibilities are endless, but there is no completely standard
compliant way of having continous updates when neither timers
nor key events are standardized.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
May 27 '06 #8
Lasse Reichstein Nielsen wrote:
Thomas 'PointedEars' Lahn <Po*********@we b.de> writes:
Lasse Reichstein Nielsen wrote:
It would have been better (and more simple) to give the controls a name
(instead of an ID) and to make them part of a form. Because that would
have allowed the developer to use both backwards-compatible and standards
compliant references to access the element objects that represent the
controls in the DOM.
That would be a workaround. There is no form because there is no
relevant action attribute to give the form,


Of course there is: action=""
i.e., the form isn't really a form, it's just a programming hack to allow
another way to address elements.
That does not matter. First, with only two textareas and without a submit
button it is usually not possible to submit the form. Second, even if we
ignore that, this requires client-side scripting to work. So a) it is
possible to cancel the a submit event that could be triggered somehow and
b) it is possible to generate the whole form with client-side scripting.

On the other hand, it would be possible to extend the functionality of
the form so that in case client-side scripting is not available, the
server-side alternative would be triggered instead (which required an
additional control to trigger either of those, like a submit button,
of course.)
However, even if the markup would be kept as is, certainly it would have
been better to test if gEBI returned a viable reference before using its
return value in a property access. Such reference worms are the direct
result of the ill-advised preconception that what works in one UA also
must work in another.


In this case it's te result of deliberatly not writing for non-W3C-DOM
compliant browsers.


A questionable statement, for the implementation of the HTMLDocument
interface in the proprietary host-defined property `document' of the
Global Object (or the proprietary host-defined `window' property),
is itself not fully "W3C-DOM compliant", and the interface may still
be implemented any other, maybe more "W3C-DOM compliant" way.
Should fallback for such browsers be deemed necessary, obviously it
should be added.


ACK
<URL:http://pointedears.de/scripts/test/whatami#inferen ce>


No inference here, just assumptions (of requirements being satisfied).


Yes, unfortunately there is.
[keypress] is a cancellable event that happens before the default
behavior (e.g., inserting the pressed letter).


So one should better had made use of the `keyup' event, instead of
attempting to produce an unreliable delay through an equally unreliable
proprietary host-defined feature.


I can see that the keyup event happens after the character is added to
the textarea in my browser. However, since we are mentioning standards,
there is no specification of key events in the DOM2 Events specification,
so that could be a conincidence.


Not really:

<URL:http://www.w3.org/TR/html4/interact/scripts.html#h-18.2.3>

And <http://www.w3.org/TR/DOM-Level-3-Events/events.html#eve nt-keyup>,
although not being usable as reference material due to its early WD
status, can provide some indication of how keyboard events are going to be
standardized (and are implemented already, at least partially in Geckos).
As you can see, the `keypress' event is intentionally missing there (there
is `keydown' already to detect a key being pressed but not yet released).
The timeout waits for the default behavior to have happened,


Not at all. Instead, because it is too small for the minimum time frame
known to be assigned to the process from the scheduler, a one millisecond
timeout will either not cause a delay at all, or delay for an undefined ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^ non-zero number of milliseconds, at least the size of the scheduled time
frame, depending on the implementation and the OS.


I assume here that the script is being executed single-threaded, so at
least the timer event won't trigger until after the current event is
finished.


You cannot know this.
In that time, if there was a timing issue (which I doubt), it may as
well be that the default behavior did not happen due to some other
factors we cannot know of.


It is true that if any other execution comes between the key event and
the timer event, the content of the textarea could have changed.
I can live with that.


But that was not my point at all. Instead, I was pointing out that it is
entirely possible that the default behavior (change of the value/content
of the control/textarea) did _not_ happen yet then. So you are retrieving
and encrypting the _old_ value then. This cannot (well, SHOULD NOT) happen
with `keyup'.
PointedEars
--
Alcohol and Math don't mix. So please don't drink and derive!
May 27 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3973
by: Fungii | last post by:
Hello, I have a stylesheet that sets p:first-letter to a certain size and colour. I was playing around with Javascript to change paragraph stylesheets using an array like this: var paras = document.all.tags("P"); Although I can change the paragraph colours using this array, the first letter of all the paragraphs remain the same
7
9671
by: Dung Ping | last post by:
Such as: <script> //code aaaa zzzz ccc
3
1719
by: JellyON | last post by:
Do you know about an encryption script which accepts the accentued characters. I've found the one below (from WebExpert premade scripts), but when you encrypt "assurément", then reverse the process, you retrieve "assurment" : the "é" is gone ! Here is this script (maybe you have another) in an HTML page (simply copy/paste to see the result) : <html> <head>
10
9594
by: Javier Gomez | last post by:
I have a table with 15.000 records. How can encrypt all information if after will shown in a form (text box)decryted ????? Thanks in advance Javier Gomez
12
3113
by: googlegroups | last post by:
Hi, I'm making a javascript program for rolling dice for a roleplaying game that's played in a forum. The die roll gets generated, gets stored as text in a hidden form field, and then gets written to the mySQL database upon form submission. What I want to do is prevent cheaters from being able to create their own die roll, and the best way I've come up with to do this is to encrypt what gets stored in the hidden form field. However,...
3
3939
by: Eduardo F. Sandino | last post by:
Any one knows how to encrypt javascript code... other way than escape() and unescape() [not is encrypt but a way to protect source code ????
4
10823
by: Tom | last post by:
Is it possible to encrypt a value in the my.settings area in VB.NET 2005? I.E. Can I add a settings value (via My Project / Settings) and have it encrypt that value so that if anyone looks at the resulting app.config file the value is encrypted? If so, (1) How do you specify the value to be encrypted? And (2) How do you access it now from VB? Can you still go through My.Settings?? Tom --
1
2435
by: oldrich.svec | last post by:
Is there any way how to encrypt some html text in php and afterwards decrypt it with javascript and send it with form? Example: <input type="hidden" value="password"/> I don't want to have seen value "password" in html source code but I need to send the value by form...
1
1383
by: technosavvy | last post by:
ECMAv262 (in section 7.6) says: A token is identified as an identifier if it consists of some combination of '$', '_', Unicode Letter ,Unicode Escape sequence and Unicode Digit..(i know this is not the complete and exact statement..I just want to talk about Unicode Letter right now) Now, the Unicode Letter contains Uppercase, Lowercase, Titlecase, Modifier and Other letters...and as far as i know that there is no sequence that these letters...
0
8203
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8642
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8512
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7203
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5576
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4094
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4206
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2630
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.