Bifid cipher: Difference between revisions
Content added Content deleted
(Add C# implementation) |
No edit summary |
||
Line 715: | Line 715: | ||
print decr h$ key$ |
print decr h$ key$ |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|FutureBasic}}== |
|||
<syntaxhighlight lang="futurebasic"> |
|||
clear local fn recode( u as CFStringRef, code as CFStringRef ) as CFStringRef |
|||
CFStringRef s = @"" |
|||
Short i, k, w = sqr( len( code ) ) |
|||
for i = 0 to len( u ) - 1 step 2 |
|||
k = intval( mid( u, i, 2 ) ) // Get ‘coordinates’ of char in code string |
|||
k = w * ( k / 10 ) + k mod 10 |
|||
s = fn StringByAppendingString( s, mid( code, k, 1 ) ) |
|||
next |
|||
end fn = s |
|||
// |
|||
clear local fn encode( t as CFStringRef, code as CFStringRef ) as CFStringRef |
|||
CFStringRef a = @"", b = @"", c |
|||
CFMutableStringRef s = fn MutableStringWithString( t ) |
|||
Short i, k, w = sqr( len( code ) ) |
|||
if w == 5 then MutableStringReplaceAllOccurrencesOfString( s, @"J", @"I" ) |
|||
print s |
|||
for i = 0 to len( s ) - 1 |
|||
c = mid( s, i, 1 ) |
|||
k = instr( 0, code, c ) // Put row in one string, column in the other |
|||
a = fn StringByAppendingString( a, fn StringWithFormat( @"%d", k / w ) ) |
|||
b = fn StringByAppendingString( b, fn StringWithFormat( @"%d", k mod w ) ) |
|||
next |
|||
a = fn StringByAppendingString( a, b ) // Combine the two strings, and recode |
|||
end fn = fn recode( a, code ) |
|||
// |
|||
clear local fn decode( s as CFStringRef, code as CFStringRef ) as CFStringRef |
|||
CFStringRef a = @"", b = @"", c |
|||
Short i, k, w = sqr( len( code ) ) |
|||
for i = 0 to ( len( s ) - 1 ) |
|||
c = mid( s, i, 1 ) |
|||
k = instr( 0, code, c ) // Put row and columm in one long string |
|||
a = fn StringByAppendingString( a, fn StringWithFormat( @"%d%d", k / w, k mod w ) ) |
|||
next |
|||
for i = 0 to len( a ) / 2 - 1 // Take row from first half of string, column from second |
|||
c = fn StringByAppendingString( mid( a, i, 1 ), mid( a, i + len( a ) / 2 , 1 ) ) |
|||
b = fn StringByAppendingString( b , c ) // Combine, and recode |
|||
next |
|||
end fn = fn recode( b, code ) |
|||
// |
|||
print fn encode( @"ATTACKATDAWN", @"ABCDEFGHIKLMNOPQRSTUVWXYZ" ) |
|||
print fn decode( @"DQBDAXDQPDQH", @"ABCDEFGHIKLMNOPQRSTUVWXYZ" ) |
|||
print |
|||
print fn encode( @"FLEEATONCE", @"BGWKZQPDNSIOAXEFCLUMTHYVR" ) |
|||
print fn decode( @"UAEOLWRINS", @"BGWKZQPDNSIOAXEFCLUMTHYVR" ) |
|||
print |
|||
print fn encode( @"HAPPY40THDAD", @"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" ) |
|||
print fn decode( @"GO31GAGVANJD", @"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" ) |
|||
handleevents |
|||
</syntaxhighlight> |
|||
{{ out }} |
|||
<pre> |
|||
ATTACKATDAWN |
|||
DQBDAXDQPDQH |
|||
ATTACKATDAWN |
|||
FLEEATONCE |
|||
UAEOLWRINS |
|||
FLEEATONCE |
|||
HAPPY40THDAD |
|||
GO31GAGVANJD |
|||
HAPPY40THDAD |
|||
</pre> |
|||
=={{header|J}}== |
=={{header|J}}== |