Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Dc (computer program)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Diffie–Hellman key exchange=== A more complex example of dc use embedded in a [[Perl]] script performs a [[Diffie–Hellman key exchange]]. This was popular as a [[signature block]] among [[cypherpunk]]s during the [[ITAR]] debates, where the short script could be run with only Perl and dc, ubiquitous programs on Unix-like operating systems:<ref>{{cite web |url=http://www.cypherspace.org/adam/rsa/perl-dh.html |title=Diffie–Hellman in 2 lines of Perl |access-date=5 Jan 2009 |author=Adam Back }}</ref> <syntaxhighlight lang="perl"> #!/usr/bin/env perl -- -export-a-crypto-system-sig Diffie-Hellman-2-lines ($g, $e, $m) = @ARGV, $m || die "$0 gen exp mod\n"; print `echo "16dio1[d2%Sa2/d0<X+d*La1=z\U$m%0]SX$e"[$g*]\EszlXx+p | dc` </syntaxhighlight> A commented version is slightly easier to understand and shows how to use loops, conditionals, and the <code>q</code> command to return from a macro. With the GNU version of dc, the <code>|</code> command can be used to do arbitrary precision modular exponentiation without needing to write the X function. <syntaxhighlight lang="perl"> #!/usr/bin/env perl my ($g, $e, $m) = map { "\U$_" } @ARGV; die "$0 gen exp mod\n" unless $m; print `echo $g $e $m | dc -e ' # Hex input and output 16dio # Read m, e and g from stdin on one line ?SmSeSg # Function z: return g * top of stack [lg*]sz # Function Q: remove the top of the stack and return 1 [sb1q]sQ # Function X(e): recursively compute g^e % m # It is the same as Sm^Lm%, but handles arbitrarily large exponents. # Stack at entry: e # Stack at exit: g^e % m # Since e may be very large, this uses the property that g^e % m == # if( e == 0 ) # return 1 # x = (g^(e/2)) ^ 2 # if( e % 2 == 1 ) # x *= g # return x % [ d 0=Q # return 1 if e==0 (otherwise, stack: e) d 2% Sa # Store e%2 in a (stack: e) 2/ # compute e/2 lXx # call X(e/2) d* # compute X(e/2)^2 La1=z # multiply by g if e%2==1 lm % # compute (g^e) % m ] SX le # Load e from the register lXx # compute g^e % m p # Print the result '`; </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)