Data update

This commit is contained in:
Ingy döt Net 2026-04-30 12:34:36 -04:00
parent 4bb20c9b71
commit cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions

View file

@ -0,0 +1,55 @@
WITH Ada.Text_IO, Ada.Characters.Handling;
USE Ada.Text_IO, Ada.Characters.Handling;
PROCEDURE Main IS
SUBTYPE Alpha IS Character RANGE 'A' .. 'Z';
TYPE Ring IS MOD (Alpha'Range_length);
TYPE Seq IS ARRAY (Integer RANGE <>) OF Ring;
FUNCTION "+" (S, Key : Seq) RETURN Seq IS
R : Seq (S'Range);
BEGIN
FOR I IN R'Range LOOP
R (I) := S (I) + Key (Key'First + (I - R'First) MOD Key'Length);
END LOOP;
RETURN R;
END "+";
FUNCTION "-" (S : Seq) RETURN Seq IS
R : Seq (S'Range);
BEGIN
FOR I IN R'Range LOOP
R (I) := - S (I);
END LOOP;
RETURN R;
END "-";
FUNCTION To_Seq (S : String) RETURN Seq IS
R : Seq (S'Range);
I : Integer := R'First;
BEGIN
FOR C OF To_Upper (S) LOOP
IF C IN Alpha THEN
R (I) := Ring'Mod (Alpha'Pos (C) - Alpha'Pos (Alpha'First));
I := I + 1;
END IF;
END LOOP;
RETURN R (R'First .. I - 1);
END To_Seq;
FUNCTION To_String (S : Seq ) RETURN String IS
R : String (S'Range);
BEGIN
FOR I IN R'Range LOOP
R (I) := Alpha'Val ( Integer (S (I)) + Alpha'Pos (Alpha'First));
END LOOP;
RETURN R;
END To_String;
Input : Seq := To_Seq (Get_Line);
Key : Seq := To_Seq (Get_Line);
Crypt : Seq := Input + Key;
BEGIN
Put_Line ("Encrypted: " & To_String (Crypt));
Put_Line ("Decrypted: " & To_String (Crypt + (-Key)));
END Main;

View file

@ -1,43 +1,43 @@
function Filtrar(cadorigen)
filtrado = ""
for i = 1 to length(cadorigen)
letra = upper(mid(cadorigen, i, 1))
if instr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", letra) then filtrado += letra
next i
return filtrado
filtrado = ""
for i = 1 to length(cadorigen)
letra = upper(mid(cadorigen, i, 1))
if instr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", letra) then filtrado += letra
next i
return filtrado
end function
function Encriptar(texto, llave)
texto = Filtrar(texto)
cifrado = ""
j = 1
for i = 1 to length(texto)
mSS = mid(texto, i, 1)
m = asc(mSS) - asc("A")
kSS = mid(llave, j, 1)
k = asc(kSS) - asc("A")
j = (j mod length(llave)) + 1
c = (m + k) mod 26
letra = chr(asc("A") + c)
cifrado += letra
next i
return cifrado
texto = Filtrar(texto)
cifrado = ""
j = 1
for i = 1 to length(texto)
mSS = mid(texto, i, 1)
m = asc(mSS) - asc("A")
kSS = mid(llave, j, 1)
k = asc(kSS) - asc("A")
j = (j mod length(llave)) + 1
c = (m + k) mod 26
letra = chr(asc("A") + c)
cifrado += letra
next i
return cifrado
end function
function DesEncriptar(texto, llave)
descifrado = ""
j = 1
for i = 1 to length(texto)
mSS = mid(texto, i, 1)
m = asc(mSS) - asc("A")
kSS = mid(llave, j, 1)
k = asc(kSS) - asc("A")
j = (j mod length(llave)) + 1
c = (m - k + 26) mod 26
letra = chr(asc("A")+c)
descifrado += letra
next i
return descifrado
descifrado = ""
j = 1
for i = 1 to length(texto)
mSS = mid(texto, i, 1)
m = asc(mSS) - asc("A")
kSS = mid(llave, j, 1)
k = asc(kSS) - asc("A")
j = (j mod length(llave)) + 1
c = (m - k + 26) mod 26
letra = chr(asc("A")+c)
descifrado += letra
next i
return descifrado
end function
llave = Filtrar("vigenerecipher")

View file

@ -0,0 +1,126 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. VIGENERE-CIPHER.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-VARIABLES.
05 WS-KEY PIC X(14) VALUE "VIGENERECIPHER".
05 WS-KEY-LEN PIC 9(3) VALUE 14.
*> Group definition to safely handle long string literal
05 WS-ORI-DEF.
10 FILLER PIC X(35) VALUE "Beware the Jabberwock, my son! The ".
10 FILLER PIC X(37) VALUE "jaws that bite, the claws that catch!".
05 WS-ORI REDEFINES WS-ORI-DEF PIC X(72).
05 WS-ORI-LEN PIC 9(3) VALUE 72.
05 WS-TEXT-UPPER PIC X(100).
05 WS-ENC PIC X(100).
05 WS-ENC-LEN PIC 9(3) VALUE 0.
05 WS-DEC PIC X(100).
05 WS-DEC-LEN PIC 9(3) VALUE 0.
05 ALPHABET-STR PIC X(26) VALUE
"ABCDEFGHIJKLMNOPQRSTUVWXYZ".
01 WS-COUNTERS.
05 I PIC S9(4) COMP.
05 J PIC S9(4) COMP.
05 C-VAL PIC S9(4) COMP.
05 K-VAL PIC S9(4) COMP.
05 TEMP-VAL PIC S9(4) COMP.
05 RES-POS PIC S9(4) COMP.
05 TEXT-CHAR PIC X(1).
05 KEY-CHAR PIC X(1).
PROCEDURE DIVISION.
MAIN-LOGIC.
*> Java: text.toUpperCase()
MOVE FUNCTION UPPER-CASE(WS-ORI) TO WS-TEXT-UPPER
PERFORM ENCRYPT-ROUTINE
IF WS-ENC-LEN > 0
DISPLAY WS-ENC(1:WS-ENC-LEN)
ELSE
DISPLAY " "
END-IF
PERFORM DECRYPT-ROUTINE
IF WS-DEC-LEN > 0
DISPLAY WS-DEC(1:WS-DEC-LEN)
ELSE
DISPLAY " "
END-IF
STOP RUN.
ENCRYPT-ROUTINE.
MOVE SPACES TO WS-ENC
MOVE 0 TO WS-ENC-LEN
MOVE 1 TO J
PERFORM VARYING I FROM 1 BY 1 UNTIL I > WS-ORI-LEN
MOVE WS-TEXT-UPPER(I:1) TO TEXT-CHAR
MOVE 0 TO C-VAL
*> Find position of char (0 for A, 25 for Z, 26 if not found)
INSPECT ALPHABET-STR TALLYING C-VAL
FOR CHARACTERS BEFORE INITIAL TEXT-CHAR
*> Simulates Java's `if (c < 'A' || c > 'Z') continue;`
IF C-VAL < 26 THEN
MOVE WS-KEY(J:1) TO KEY-CHAR
MOVE 0 TO K-VAL
INSPECT ALPHABET-STR TALLYING K-VAL
FOR CHARACTERS BEFORE INITIAL KEY-CHAR
*> Math: (C-VAL + K-VAL) % 26
COMPUTE TEMP-VAL = C-VAL + K-VAL
COMPUTE TEMP-VAL = FUNCTION MOD(TEMP-VAL, 26)
*> Convert back to 1-based index for COBOL strings
COMPUTE RES-POS = TEMP-VAL + 1
ADD 1 TO WS-ENC-LEN
MOVE ALPHABET-STR(RES-POS:1)
TO WS-ENC(WS-ENC-LEN:1)
*> Java: j = ++j % key.length()
ADD 1 TO J
IF J > WS-KEY-LEN THEN
MOVE 1 TO J
END-IF
END-IF
END-PERFORM.
DECRYPT-ROUTINE.
MOVE SPACES TO WS-DEC
MOVE 0 TO WS-DEC-LEN
MOVE 1 TO J
PERFORM VARYING I FROM 1 BY 1 UNTIL I > WS-ENC-LEN
MOVE WS-ENC(I:1) TO TEXT-CHAR
MOVE 0 TO C-VAL
INSPECT ALPHABET-STR TALLYING C-VAL
FOR CHARACTERS BEFORE INITIAL TEXT-CHAR
IF C-VAL < 26 THEN
MOVE WS-KEY(J:1) TO KEY-CHAR
MOVE 0 TO K-VAL
INSPECT ALPHABET-STR TALLYING K-VAL
FOR CHARACTERS BEFORE INITIAL KEY-CHAR
*> Math: (C-VAL - K-VAL + 26) % 26
COMPUTE TEMP-VAL = C-VAL - K-VAL + 26
COMPUTE TEMP-VAL = FUNCTION MOD(TEMP-VAL, 26)
COMPUTE RES-POS = TEMP-VAL + 1
ADD 1 TO WS-DEC-LEN
MOVE ALPHABET-STR(RES-POS:1)
TO WS-DEC(WS-DEC-LEN:1)
ADD 1 TO J
IF J > WS-KEY-LEN THEN
MOVE 1 TO J
END-IF
END-IF
END-PERFORM.

View file

@ -9,11 +9,11 @@ shared void run() {
function encrypt(String clearText, String key) =>
crypt(clearText, key, (Character a, Character b) =>
('A'.integer + ((a.integer + b.integer - 130) % 26)).character);
('A'.integer + ((a.integer + b.integer - 130) % 26)).character);
function decrypt(String cipherText, String key) =>
crypt(cipherText, key, (Character a, Character b) =>
('A'.integer + ((a.integer - b.integer + 26) % 26)).character);
('A'.integer + ((a.integer - b.integer + 26) % 26)).character);
value key = "VIGENERECIPHER";
value message = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";

View file

@ -2,28 +2,28 @@
code = (char) -> char.charCodeAt()
encrypt = (text, key) ->
res = []
j = 0
for c in text.toUpperCase()
continue if c < 'A' or c > 'Z'
res.push ((code c) + (code key[j]) - 130) % 26 + 65
j = ++j % key.length
String.fromCharCode res...
res = []
j = 0
for c in text.toUpperCase()
continue if c < 'A' or c > 'Z'
res.push ((code c) + (code key[j]) - 130) % 26 + 65
j = ++j % key.length
String.fromCharCode res...
decrypt = (text, key) ->
res = []
j = 0
for c in text.toUpperCase()
continue if c < 'A' or c > 'Z'
res.push ((code c) - (code key[j]) + 26) % 26 + 65
j = ++j % key.length
String.fromCharCode res...
res = []
j = 0
for c in text.toUpperCase()
continue if c < 'A' or c > 'Z'
res.push ((code c) - (code key[j]) + 26) % 26 + 65
j = ++j % key.length
String.fromCharCode res...
# Trying it out
key = "VIGENERECIPHER"

View file

@ -4,20 +4,20 @@
(string-upcase s)))
(defun vigenère (s key &key decipher
&aux (A (char-code #\A))
(op (if decipher #'- #'+)))
&aux (A (char-code #\A))
(op (if decipher #'- #'+)))
(labels
((to-char (c) (code-char (+ c A)))
(to-code (c) (- (char-code c) A)))
(let ((k (map 'list #'to-code (strip key))))
(setf (cdr (last k)) k)
(map 'string
(lambda (c)
(prog1
(to-char
(mod (funcall op (to-code c) (car k)) 26))
(setf k (cdr k))))
(strip s)))))
(lambda (c)
(prog1
(to-char
(mod (funcall op (to-code c) (car k)) 26))
(setf k (cdr k))))
(strip s)))))
(let* ((msg "Beware the Jabberwock... The jaws that... the claws that catch!")
(key "vigenere cipher")

View file

@ -21,18 +21,18 @@ Key:=UpperAlphaOnly(Key);
{Point to first Key-character}
KInx:=1;
for I:=1 to Length(Text) do
begin
{Offset Text-char by key-char amount}
TC:=byte(Text[I])-byte('A')+Byte(Key[KInx]);
{if it is shifted past "Z", wrap back around past "A"}
if TC>Byte('Z') then TC:=byte('@')+(TC-Byte('Z'));
{Store in output string}
Result:=Result+Char(TC);
{Point to next Key-char}
Inc(Kinx);
{If index post end of key, start over}
if KInx>Length(Key) then KInx:=1;
end;
begin
{Offset Text-char by key-char amount}
TC:=byte(Text[I])-byte('A')+Byte(Key[KInx]);
{if it is shifted past "Z", wrap back around past "A"}
if TC>Byte('Z') then TC:=byte('@')+(TC-Byte('Z'));
{Store in output string}
Result:=Result+Char(TC);
{Point to next Key-char}
Inc(Kinx);
{If index post end of key, start over}
if KInx>Length(Key) then KInx:=1;
end;
end;
@ -47,18 +47,18 @@ Text:=UpperAlphaOnly(Text);
Key:=UpperAlphaOnly(Key);
KInx:=1;
for I:=1 to Length(Text) do
begin
{subtrack key-char from text-char}
TC:=byte(Text[I])-Byte(Key[Kinx])+Byte('A');
{if result below "A" wrap back around to "Z"}
if TC<Byte('A') then TC:=(byte('Z')-((Byte('A')-TC)))+1;
{store in result}
Result:=Result+Char(TC);
{Point to next key char}
Inc(Kinx);
{Past the end, start over}
if KInx>Length(Key) then KInx:=1;
end;
begin
{subtrack key-char from text-char}
TC:=byte(Text[I])-Byte(Key[Kinx])+Byte('A');
{if result below "A" wrap back around to "Z"}
if TC<Byte('A') then TC:=(byte('Z')-((Byte('A')-TC)))+1;
{store in result}
Result:=Result+Char(TC);
{Point to next key char}
Inc(Kinx);
{Past the end, start over}
if KInx>Length(Key) then KInx:=1;
end;
end;
const TestKey = 'VIGENERECIPHER';

View file

@ -5,11 +5,11 @@ def vigenere(text; key; encryptp):
(text | n) as [$xtext, $length]
| (key | n) as [$xkey, $keylength]
| reduce range(0; $length) as $i (null;
($i % $keylength) as $ki
| . + [if encryptp
then (($xtext[$i] + $xkey[$ki] - 130) % 26) + 65
($i % $keylength) as $ki
| . + [if encryptp
then (($xtext[$i] + $xkey[$ki] - 130) % 26) + 65
else (($xtext[$i] - $xkey[$ki] + 26) % 26) + 65
end] )
end] )
| implode;
# Input: sample text

View file

@ -4,100 +4,100 @@ PROGRAM Vigenere;
// get a letter's alphabetic position (A=0)
FUNCTION letternum(letter: CHAR): BYTE;
BEGIN
letternum := (ord(letter)-ord('A'));
END;
BEGIN
letternum := (ord(letter)-ord('A'));
END;
// convert a character to uppercase
FUNCTION uch(ch: CHAR): CHAR;
BEGIN
uch := ch;
IF ch IN ['a'..'z'] THEN
uch := chr(ord(ch) AND $5F);
END;
BEGIN
uch := ch;
IF ch IN ['a'..'z'] THEN
uch := chr(ord(ch) AND $5F);
END;
// convert a string to uppercase
FUNCTION ucase(str: STRING): STRING;
VAR i: BYTE;
BEGIN
ucase := '';
FOR i := 1 TO Length(str) DO
ucase := ucase + uch(str[i]);
END;
VAR i: BYTE;
BEGIN
ucase := '';
FOR i := 1 TO Length(str) DO
ucase := ucase + uch(str[i]);
END;
// construct a Vigenere-compatible string:
// uppercase; no spaces or punctuation.
FUNCTION vstr(pt: STRING): STRING;
VAR c: Cardinal;
s: STRING;
BEGIN
vstr:= '';
s := ucase(pt);
FOR c := 1 TO Length(s) DO BEGIN
IF s[c] IN ['A'..'Z'] THEN
vstr += s[c];
END;
END;
VAR c: Cardinal;
s: STRING;
BEGIN
vstr:= '';
s := ucase(pt);
FOR c := 1 TO Length(s) DO BEGIN
IF s[c] IN ['A'..'Z'] THEN
vstr += s[c];
END;
END;
// construct a repeating Vigenere key
FUNCTION vkey(pt, key: STRING): STRING;
VAR c,n: Cardinal;
k : STRING;
BEGIN
k := vstr(key);
vkey := '';
FOR c := 1 TO Length(pt) DO BEGIN
n := c mod Length(k);
IF n>0 THEN vkey += k[n] ELSE vkey += k[Length(k)];
END;
END;
// Vigenere encipher
VAR c,n: Cardinal;
k : STRING;
BEGIN
k := vstr(key);
vkey := '';
FOR c := 1 TO Length(pt) DO BEGIN
n := c mod Length(k);
IF n>0 THEN vkey += k[n] ELSE vkey += k[Length(k)];
END;
END;
// Vigenere encipher
FUNCTION enVig(pt,key:STRING): STRING;
VAR ct: STRING;
c,n : Cardinal;
BEGIN
ct := pt;
FOR c := 1 TO Length(pt) DO BEGIN
n := letternum(pt[c])+letternum(key[c]);
n := n mod 26;
ct[c]:=chr(ord('A')+n);
END;
enVig := ct;
END;
VAR ct: STRING;
c,n : Cardinal;
BEGIN
ct := pt;
FOR c := 1 TO Length(pt) DO BEGIN
n := letternum(pt[c])+letternum(key[c]);
n := n mod 26;
ct[c]:=chr(ord('A')+n);
END;
enVig := ct;
END;
// Vigenere decipher
FUNCTION deVig(ct,key:STRING): STRING;
VAR pt : STRING;
c,n : INTEGER;
BEGIN
pt := ct;
FOR c := 1 TO Length(ct) DO BEGIN
n := letternum(ct[c])-letternum(key[c]);
IF n<0 THEN n:=26+n;
pt[c]:=chr(ord('A')+n);
END;
deVig := pt;
END;
VAR pt : STRING;
c,n : INTEGER;
BEGIN
pt := ct;
FOR c := 1 TO Length(ct) DO BEGIN
n := letternum(ct[c])-letternum(key[c]);
IF n<0 THEN n:=26+n;
pt[c]:=chr(ord('A')+n);
END;
deVig := pt;
END;
VAR key: STRING = 'Vigenere cipher';
msg: STRING = 'Beware the Jabberwock! The jaws that bite, the claws that catch!';
vtx: STRING = '';
ctx: STRING = '';
ptx: STRING = '';
VAR key: STRING = 'Vigenere cipher';
msg: STRING = 'Beware the Jabberwock! The jaws that bite, the claws that catch!';
vtx: STRING = '';
ctx: STRING = '';
ptx: STRING = '';
BEGIN
// make Vigenere-compatible
vtx := vstr(msg);
key := vkey(vtx,key);
// Vigenere encipher / decipher
ctx := enVig(vtx,key);
ptx := deVig(ctx,key);
// display results
Writeln('Message : ',msg);
Writeln('Plaintext : ',vtx);
Writeln('Key : ',key);
Writeln('Ciphertext : ',ctx);
Writeln('Plaintext : ',ptx);
// make Vigenere-compatible
vtx := vstr(msg);
key := vkey(vtx,key);
// Vigenere encipher / decipher
ctx := enVig(vtx,key);
ptx := deVig(ctx,key);
// display results
Writeln('Message : ',msg);
Writeln('Plaintext : ',vtx);
Writeln('Key : ',key);
Writeln('Ciphertext : ',ctx);
Writeln('Plaintext : ',ptx);
END.

View file

@ -0,0 +1,85 @@
# Author: D. Cudnohufsky
function Get-VigenereCipher
{
Param
(
[Parameter(Mandatory=$true)]
[string] $Text,
[Parameter(Mandatory=$true)]
[string] $Key,
[switch] $Decode
)
begin
{
$map = [char]'A'..[char]'Z'
}
process
{
$Key = $Key -replace '[^a-zA-Z]',''
$Text = $Text -replace '[^a-zA-Z]',''
$keyChars = $Key.toUpper().ToCharArray()
$Chars = $Text.toUpper().ToCharArray()
function encode
{
param
(
$Char,
$keyChar,
$Alpha = [char]'A'..[char]'Z'
)
$charIndex = $Alpha.IndexOf([int]$Char)
$keyIndex = $Alpha.IndexOf([int]$keyChar)
$NewIndex = ($charIndex + $KeyIndex) - $Alpha.Length
$Alpha[$NewIndex]
}
function decode
{
param
(
$Char,
$keyChar,
$Alpha = [char]'A'..[char]'Z'
)
$charIndex = $Alpha.IndexOf([int]$Char)
$keyIndex = $Alpha.IndexOf([int]$keyChar)
$int = $charIndex - $keyIndex
if ($int -lt 0) { $NewIndex = $int + $Alpha.Length }
else { $NewIndex = $int }
$Alpha[$NewIndex]
}
while ( $keyChars.Length -lt $Chars.Length )
{
$keyChars = $keyChars + $keyChars
}
for ( $i = 0; $i -lt $Chars.Length; $i++ )
{
if ( [int]$Chars[$i] -in $map -and [int]$keyChars[$i] -in $map )
{
if ($Decode) {$Chars[$i] = decode $Chars[$i] $keyChars[$i] $map}
else {$Chars[$i] = encode $Chars[$i] $keyChars[$i] $map}
$Chars[$i] = [char]$Chars[$i]
[string]$OutText += $Chars[$i]
}
}
$OutText
$OutText = $null
}
}

View file

@ -0,0 +1,51 @@
Rebol [
title: "Rosetta code: Vigenère cipher"
file: %Vigenere_cipher.r3
url: https://rosettacode.org/wiki/Vigenère_cipher
]
vigenere: context [
letters: charset [#"A" - #"Z"]
encrypt: function [
"Encrypts a message using the Vigenère cipher"
msg [string!] "plaintext string (mixed case, may contain non-alpha characters)"
key [string!] "uppercase cipher key string (e.g. VIGENERECIPHER)"
][
pos: 1
result: copy ""
foreach c msg [
c: uppercase c
;; skip non-alphabetic characters (spaces, punctuation, etc.)
if find letters c [
append result #"A" + ((key/:pos - #"A" + c - #"A") % 26)
pos: 1 + (pos % length? key) ; advance key position, wrapping at key length
]
]
result ; return encrypted string
]
decrypt: function [
"Decrypts a Vigenère-encrypted message back to uppercase plaintext"
msg [string!] "ciphertext string produced by encrypt (uppercase letters only)"
key [string!] "same cipher key used during encryption"
] [
pos: 1
result: copy ""
foreach c msg [
c: uppercase c
append result #"A" + ((26 + c - key/:pos) % 26)
pos: 1 + (pos % length? key) ; advance key position, wrapping at key length
]
result ; return decrypted string
]
]
text: "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
key: "VIGENERECIPHER"
encr: vigenere/encrypt text key ;; encrypt the plaintext
decr: vigenere/decrypt encr key ;; decrypt it back — should match original letters (uppercased)
print text ;= original mixed-case text
print encr ;= encrypted ciphertext
print decr ;= decrypted result (uppercase, punctuation stripped during encryption)

View file

@ -60,7 +60,7 @@ either decrypt [ ;; collect utf-8 characters
;----------------------------------------------------------
; start of program
;----------------------------------------------------------
view layout [title "vigenere cyphre" ;Define nice GUI :- )
view layout [title "vigenere cyphre" ;Define nice GUI :- )
;----------------------------------------------------------
backdrop silver ;; define window background colour
text "message:" pad 99x1 button "get-clip" [tx1/text: read-clipboard]

View file

@ -3,34 +3,34 @@ package require Tcl 8.6
oo::class create Vigenere {
variable key
constructor {protoKey} {
foreach c [split $protoKey ""] {
if {[regexp {[A-Za-z]} $c]} {
lappend key [scan [string toupper $c] %c]
}
}
foreach c [split $protoKey ""] {
if {[regexp {[A-Za-z]} $c]} {
lappend key [scan [string toupper $c] %c]
}
}
}
method encrypt {text} {
set out ""
set j 0
foreach c [split $text ""] {
if {[regexp {[^a-zA-Z]} $c]} continue
scan [string toupper $c] %c c
append out [format %c [expr {($c+[lindex $key $j]-130)%26+65}]]
set j [expr {($j+1) % [llength $key]}]
}
return $out
set out ""
set j 0
foreach c [split $text ""] {
if {[regexp {[^a-zA-Z]} $c]} continue
scan [string toupper $c] %c c
append out [format %c [expr {($c+[lindex $key $j]-130)%26+65}]]
set j [expr {($j+1) % [llength $key]}]
}
return $out
}
method decrypt {text} {
set out ""
set j 0
foreach c [split $text ""] {
if {[regexp {[^A-Z]} $c]} continue
scan $c %c c
append out [format %c [expr {($c-[lindex $key $j]+26)%26+65}]]
set j [expr {($j+1) % [llength $key]}]
}
return $out
set out ""
set j 0
foreach c [split $text ""] {
if {[regexp {[^A-Z]} $c]} continue
scan $c %c c
append out [format %c [expr {($c-[lindex $key $j]+26)%26+65}]]
set j [expr {($j+1) % [llength $key]}]
}
return $out
}
}

View file

@ -1,27 +1,25 @@
const (
key = "VIGENERECIPHER"
text = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
)
const key = "VIGENERECIPHER"
const text = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
fn main() {
encoded := vigenere(text, key, true)
println(encoded)
decoded := vigenere(encoded, key, false)
println(decoded)
encoded := vigenere(text, key, true)
println(encoded)
decoded := vigenere(encoded, key, false)
println(decoded)
}
fn vigenere(str string, key string, encrypt bool) string {
mut txt :=''
mut chr_arr := []u8{}
mut kidx, mut cidx := 0, 0
if encrypt == true {txt = str.to_upper()}
else {txt = str}
for chr in txt {
if (chr > 64 && chr < 91) == false {continue}
if encrypt == true {cidx = (chr + key[kidx] - 130) % 26}
else {cidx = (chr - key[kidx] + 26) % 26}
chr_arr << u8(cidx + 65)
kidx = (kidx + 1) % key.len
}
return chr_arr.bytestr()
mut txt :=''
mut chr_arr := []u8{}
mut kidx, mut cidx := 0, 0
if encrypt == true {txt = str.to_upper()}
else {txt = str}
for chr in txt {
if (chr > 64 && chr < 91) == false {continue}
if encrypt == true {cidx = (chr + key[kidx] - 130) % 26}
else {cidx = (chr - key[kidx] + 26) % 26}
chr_arr << u8(cidx + 65)
kidx = (kidx + 1) % key.len
}
return chr_arr.bytestr()
}

View file

@ -0,0 +1,47 @@
Function Encrypt(text,key)
text = OnlyCaps(text)
key = OnlyCaps(key)
j = 1
For i = 1 To Len(text)
ms = Mid(text,i,1)
m = Asc(ms) - Asc("A")
ks = Mid(key,j,1)
k = Asc(ks) - Asc("A")
j = (j Mod Len(key)) + 1
c = (m + k) Mod 26
c = Chr(Asc("A")+c)
Encrypt = Encrypt & c
Next
End Function
Function Decrypt(text,key)
key = OnlyCaps(key)
j = 1
For i = 1 To Len(text)
ms = Mid(text,i,1)
m = Asc(ms) - Asc("A")
ks = Mid(key,j,1)
k = Asc(ks) - Asc("A")
j = (j Mod Len(key)) + 1
c = (m - k + 26) Mod 26
c = Chr(Asc("A")+c)
Decrypt = Decrypt & c
Next
End Function
Function OnlyCaps(s)
For i = 1 To Len(s)
char = UCase(Mid(s,i,1))
If Asc(char) >= 65 And Asc(char) <= 90 Then
OnlyCaps = OnlyCaps & char
End If
Next
End Function
'testing the functions
orig_text = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
orig_key = "vigenerecipher"
WScript.StdOut.WriteLine "Original: " & orig_text
WScript.StdOut.WriteLine "Key: " & orig_key
WScript.StdOut.WriteLine "Encrypted: " & Encrypt(orig_text,orig_key)
WScript.StdOut.WriteLine "Decrypted: " & Decrypt(Encrypt(orig_text,orig_key),orig_key)

View file

@ -0,0 +1,34 @@
'vigenere cypher
option explicit
const asca =65 'ascii(a)
function filter(s)
with new regexp
.pattern="[^A-Z]"
.global=1
filter=.replace(ucase(s),"")
end with
end function
function vigenere (s,k,sign)
dim s1,i,a,b
for i=0 to len(s)-1
a=asc(mid(s,i+1,1))-asca
b=sign * (asc(mid(k,(i mod len(k))+1,1))-asca)
s1=s1 & chr(((a+b+26) mod 26) +asca)
next
vigenere=s1
end function
function encrypt(s,k): encrypt=vigenere(s,k,1) :end function
function decrypt(s,k): decrypt=vigenere(s,k,-1) :end function
'test--------------------------
dim plaintext,filtered,key,encoded
key="VIGENERECYPHER"
plaintext = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
filtered= filter(plaintext)
wscript.echo filtered
encoded=encrypt(filtered,key)
wscript.echo encoded
wscript.echo decrypt(encoded,key)

View file

@ -1,5 +1,5 @@
Get_Input(10, "Key: ", STATLINE+NOCR) // @10 = key
Reg_Copy_Block(11, Cur_Pos, EOL_Pos) // @11 = copy of original text
Get_Input(10, "Key: ", STATLINE+NOCR) // @10 = key
Reg_Copy_Block(11, Cur_Pos, EOL_Pos) // @11 = copy of original text
EOL Ins_Newline
Ins_Text("Key = ") Reg_Ins(10) Ins_Newline
@ -8,7 +8,7 @@ Buf_Switch(Buf_Free)
Reg_Ins(10)
Case_Upper_Block(0, Cur_Pos)
BOF
#2 = Reg_Size(10) // #2 = key length
#2 = Reg_Size(10) // #2 = key length
for (#3=130; #3 < 130+#2; #3++) {
#@3 = Cur_Char
Char(1)
@ -17,16 +17,16 @@ Buf_Quit(OK)
Ins_Text("Encrypted: ")
#4 = Cur_Pos
Reg_Ins(11) // copy of original text
Reg_Ins(11) // copy of original text
Replace_Block("|!|A", "", #4, EOL_Pos, BEGIN+ALL+NOERR) // remove non-alpha chars
Case_Upper_Block(#4, EOL_Pos) // convert to upper case
Case_Upper_Block(#4, EOL_Pos) // convert to upper case
Goto_Pos(#4)
#1 = 1; Call("ENCRYPT_DECRYPT") // Encrypt the line
Reg_Copy_Block(11, #4, Cur_Pos) // Copy encrypted text text to next line
#1 = 1; Call("ENCRYPT_DECRYPT") // Encrypt the line
Reg_Copy_Block(11, #4, Cur_Pos) // Copy encrypted text text to next line
Ins_Newline
Ins_Text("Decrypted: ")
Reg_Ins(11, BEGIN)
#1 = -1; Call("ENCRYPT_DECRYPT") // Decrypt the line
#1 = -1; Call("ENCRYPT_DECRYPT") // Decrypt the line
Return
@ -38,10 +38,10 @@ Return
Num_Push(6,9)
#6 = 0
While (!At_EOL) {
#7 = #6+130 // pointer to key array
#8 = #@7 // get key character
#9 = (Cur_Char + #8*#1 + 26) % 26 + 'A' // decrypt/encrypt
Ins_Char(#9, OVERWRITE) // write the converted char
#7 = #6+130 // pointer to key array
#8 = #@7 // get key character
#9 = (Cur_Char + #8*#1 + 26) % 26 + 'A' // decrypt/encrypt
Ins_Char(#9, OVERWRITE) // write the converted char
#6 = (#6+1) % #2
}
Num_Pop(6,9)

View file

@ -7,6 +7,6 @@ fcn encipher(src,key,is_encode){
klen:=Walker.cycle(key.len()); // 0,1,2,3,..,keyLen-1,0,1,2,3, ...
src.pump(String,'wrap(c){ i:=klen.next(); c=c.toAsc();
(A + ( if(is_encode) c - A + key[i] - A;
else c - key[i] + 26 ) % 26).toChar()
else c - key[i] + 26 ) % 26).toChar()
});
}