Vigenère cipher: Difference between revisions
m
→{{header|Wren}}: Minor tidy
No edit summary |
m (→{{header|Wren}}: Minor tidy) |
||
(7 intermediate revisions by 4 users not shown) | |||
Line 1,143:
<syntaxhighlight lang="easylang">
r$ = ""▼
txt$[] = strchars txt$
for c$ in strchars pw$
Line 1,160 ⟶ 1,159:
.
.
return r$
.
s$ = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
pw$ = "VIGENERECIPHER"
call encr r$ pw$ -1 r$▼
print r$
</syntaxhighlight>
=={{header|Elena}}==
{{trans|C#}}
ELENA
<syntaxhighlight lang="elena">import system'text;
import system'culture;
import system'math;
import system'routines;
Line 1,180 ⟶ 1,179:
class VCipher
{
var TXT := txt.toUpper();
foreach(char t; in TXT)
if (t <
int tmp := t.toInt() - 65 + d * (PW[pwi].toInt() - 65);▼
int tmp := t - 65 +
if (tmp < 0) tmp +=
output.write((65
if (pwi == PW.Length) { pwi :=
if (pwi == PW.Length) { pwi := 0 }▼
^
^ output.Value▼
}▼
}
Line 1,213 ⟶ 1,209:
var pw := "VIGENERECIPHER";
console.printLine(s0,
var s1 := v.encrypt(s0, pw, 1);
console.printLine("Encrypted:",s1);
Line 3,808 ⟶ 3,804:
ciphertext = LXFOPVEFRNHR
decrypted = ATTACKATDAWN
</pre>
=={{header|RPL}}==
As the encoding and decoding programs would differ by only one instruction (plus instead of minus), they have been merged into a single function that decodes if the input text is in uppercase only, and encodes otherwise. To encode a uppercase-only text, a space (or any punctuation sign) must be put somwehere.
≪ → key
1 3 PICK SIZE '''FOR''' j
OVER j DUP SUB NUM
'''IF''' DUP 97 ≥ OVER 122 ≤ AND '''THEN''' 32 - '''END'''
'''IF''' DUP 65 ≥ OVER 90 ≤ AND '''THEN''' 65 - + '''ELSE''' 1 SF DROP '''END'''
'''NEXT'''
SWAP DROP ""
1 3 PICK SIZE '''FOR''' j
key j 1 - key SIZE MOD 1 + DUP SUB NUM 64 -
'''IF''' 1 FC? '''THEN''' + '''ELSE''' - '''END'''
26 MOD 65 + CHR +
'''NEXT'''
SWAP DROP
≫ ≫ ‘<span style="color:blue">VIGENERE</span>’ STO
"Beware the Jabberwock!" <span style="color:blue">VIGENERE</span>
DUP <span style="color:blue">VIGENERE</span>
{{out}}▼
<pre>
2: "FVPVDZBCIATWNZZRSTD"
1: "BEWARETHEJABBERWOCK"
</pre>
Line 4,610 ⟶ 4,633:
{{trans|Kotlin}}
{{libheader|Wren-str}}
<syntaxhighlight lang="
var vigenere = Fn.new { |text, key, encrypt|
Line 4,676 ⟶ 4,699:
=={{header|Zig}}==
{{works with|Zig|0.11.
<syntaxhighlight lang="zig">
const std = @import("std");
const Allocator = std.mem.Allocator;
</syntaxhighlight><syntaxhighlight lang="zig">
const Vigenere = enum {
<syntaxhighlight lang="zig">/// Caller owns the returned slice memory.▼
encode,
fn vigenere(allocator: Allocator, text: []const u8, key: []const u8, encrypt: bool) Allocator.Error![]u8 {▼
decode,
var dynamic_string = std.ArrayList(u8).init(allocator);▼
};
var key_index: usize = 0;▼
</syntaxhighlight><syntaxhighlight lang="zig">
for (text) |letter| {▼
const c = if (std.ascii.isLower(letter)) std.ascii.toUpper(letter) else letter;▼
// ------------------------------------------ allocator
if (std.ascii.isUpper(c)) {▼
const k = key[key_index];▼
try dynamic_string.append(n % 26 + 'A'); // A-Z▼
key_index += 1;▼
key_index %= key.len;▼
▲ }
return dynamic_string.toOwnedSlice();▼
}</syntaxhighlight>▼
▲<syntaxhighlight lang="zig">pub fn main() anyerror!void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer {
Line 4,704 ⟶ 4,717:
}
const allocator = gpa.allocator();
// --------------------------------------------- stdout
const stdout = std.io.getStdOut().writer();
// ----------------------------------------------------
const key = "VIGENERECIPHER";
const text = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
const encoded = try vigenere(allocator, text, key,
defer allocator.free(encoded);
const decoded = try vigenere(allocator, encoded, key,
defer allocator.free(decoded);
}
</syntaxhighlight><syntaxhighlight lang="zig">
▲{{out}}
▲fn vigenere(allocator: Allocator, text: []const u8, key: []const u8,
▲ var dynamic_string = std.ArrayList(u8).init(allocator);
▲ var key_index: usize = 0;
▲ for (text) |letter| {
▲ const c = if (std.ascii.isLower(letter)) std.ascii.toUpper(letter) else letter;
▲ if (std.ascii.isUpper(c)) {
▲ const k = key[key_index];
const n = switch (mode) {
};
▲ try dynamic_string.append(n % 26 + 'A'); // A-Z
▲ key_index += 1;
▲ key_index %= key.len;
▲ }
▲ return dynamic_string.toOwnedSlice();
}
<pre>WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY
BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH</pre>
|