Vigenère cipher: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(4 intermediate revisions by 2 users not shown)
Line 1,170:
=={{header|Elena}}==
{{trans|C#}}
ELENA 46.x :
<syntaxhighlight lang="elena">import system'text;
import system'culture;
import system'math;
import system'routines;
Line 1,178 ⟶ 1,179:
class VCipher
{
string encrypt(string txt, string pw, int d)
{
auto output := new TextBuilder();
int pwi := 0;
string PW := pw.upperCasetoUpper();
var TXT := txt.toUpper();
 
txt.upperCase().forEach:(t)
foreach(char t; in TXT) {
if(t >= $65){
if (t < {$65) $continue;
 
int tmp := t.toInt() - 65 + d * (PW[pwi].toInt() - 65);
int tmp := t - 65 + ifd * (tmppw[pwi] <- 065);
if (tmp < 0) tmp += {26;
output.write((65 + tmp += .mod(26)).toChar());
}pwi++;
if (pwi == PW.Length) { pwi := output.write((650 + tmp.mod:26).toChar());}
pwi += 1};
 
if (pwi == PW.Length) { pwi := 0 }
^ }output.Value
};
^ output.Value
}
}
Line 1,211 ⟶ 1,209:
var pw := "VIGENERECIPHER";
console.printLine(s0,newLinenewLineConstant,pw,newLinenewLineConstant);
var s1 := v.encrypt(s0, pw, 1);
console.printLine("Encrypted:",s1);
Line 4,635 ⟶ 4,633:
{{trans|Kotlin}}
{{libheader|Wren-str}}
<syntaxhighlight lang="ecmascriptwren">import "./str" for Char, Str
 
var vigenere = Fn.new { |text, key, encrypt|
Line 4,701 ⟶ 4,699:
 
=={{header|Zig}}==
{{works with|Zig|0.11.0dev0}}
<syntaxhighlight lang="zig">const std = @import("std");
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| {
<syntaxhighlight lang="zig">pub fn main() anyerror!void {
const c = if (std.ascii.isLower(letter)) std.ascii.toUpper(letter) else letter;
// ------------------------------------------ allocator
if (std.ascii.isUpper(c)) {
const k = key[key_index];
const n = if (encrypt) ((c - 'A') + (k - 'A')) else 26 + c - k;
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 {
// allocator
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer {
Line 4,729 ⟶ 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, true.encode);
defer allocator.free(encoded);
try stdout.print("{s}\n", .{encoded});
 
const decoded = try vigenere(allocator, encoded, key, false.decode);
defer allocator.free(decoded);
try stdout.print("{s}\n", .{decoded});
}
}</syntaxhighlight>
</syntaxhighlight><syntaxhighlight lang="zig">
{{out}}
<syntaxhighlight lang="zig">/// Caller owns the returned slice memory.
fn vigenere(allocator: Allocator, text: []const u8, key: []const u8, encryptmode: boolVigenere) Allocator.Error![]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) {
int tmp.encode :=> t.toInt()(c - 65'A') + d * (PW[pwi].toInt()k - 65'A'));,
if (pwi.decode ==> PW.Length)26 {+ pwi :=c 0- }k,
^ output.Value };
try dynamic_string.append(n % 26 + 'A'); // A-Z
key_index += 1;
key_index %= key.len;
}
}
return dynamic_string.toOwnedSlice();
}
}</syntaxhighlight>{{out}}
<pre>WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY
BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH</pre>
9,482

edits