Implement this key cipher

13

2

Implement this key cipher

Goal

Use the algorithm (explained in the Algorithm section) to implement a certain cipher.

The program must read input from STDIN or the closest available equivalent, use the algorithm to generate the ciphertext and a key.

The ciphertext and the key will be written to STDOUT or the closest available equivalent. Any format is allowed, as long as it outputs the ciphertext and the key.

Algorithm

Convert the characters in the string into the respective ASCII values. For example:

Hello -> 72 101 108 108 111

Next, you will need to generate a key as long as the string with random numbers in the range of 0-9.

Hello -> 62841

Add the integers in the random number sequence to the ASCII values of the string. In the above examples, 72 would become 78, and 101 would become 104.

72 + 6 = 78, 101 + 2 = 103, 108 + 8 = 116, etc

Next, convert the new values back to characters. In the above examples, the text Hello has become Ngtpp.

Examples

(These are simply examples of what the output might look like. The output can and will vary.)

Hello World

Lfrlu)_supg
41606984343

This will be encoded

Zhjs$~koo gj$iuhofgj
60104723305544750226

Rules

  • You can assume that the input will only contain characters in the range a-z, A-Z, and spaces.
  • Submissions must be full programs or functions.
  • Submissions will be scored in bytes.
  • Standard loopholes are forbidden.
  • This is code-golf, so the shortest code wins.

(This is one of my first challenges, if there's something wrong with it, feel free to tell me how I could improve it.)

m654

Posted 2016-08-19T13:39:28.793

Reputation: 765

5This challenge looks good to me, except for a couple of thoughts. 1. Is a function allowed instead of a full program? A related question is could the values be returned instead of printed? 2. You said preferably with the format (ciphertext)\n(key). "preferred features" and code golf don't mix very well. Your should make that mandatory or allow other output formats. 3. Does the key have to be printed without spaces? What about printing it in list format, e.g. [0, 5, 2, ...]? – James – 2016-08-19T13:48:47.160

Can the key have leading zeroes? – TheBikingViking – 2016-08-19T13:53:51.590

Can I print the key first and then the cipher text? – Maltysen – 2016-08-19T13:54:36.503

@DJMcMayhem I updated the challenge. – m654 – 2016-08-19T14:00:22.590

@TheBikingViking Updated. – m654 – 2016-08-19T14:00:32.263

@Maltysen Yes, you can. They have to be separated with a newline though. – m654 – 2016-08-19T14:00:54.763

@TimmyD Yep, it was a typo. I'll update it – m654 – 2016-08-19T14:01:23.027

@DJMcMayhem Well, I changed the rules a little, you can post it now. – m654 – 2016-08-19T15:26:00.840

@m654 Thanks! Generally it's best to go with a very permissive IO format. I've actually already posted it, but I'll edit it to be shorter now. – James – 2016-08-19T15:28:57.050

1Nice first challenge but I'm not so sure on the strict IO formats. Usually functions are allowed and usually answers can read from one of the accepted IO methods. This includes outputting an array with the items – Downgoat – 2016-08-19T15:54:45.263

1Do the digits of the key have to be generated with an uniform distribution? – Dennis – 2016-08-19T15:58:11.377

I made the format more permissive. @Dennis, no, it doesn't have to. Pseudorandom numbers are alright. – m654 – 2016-08-19T16:15:47.437

Pseudo-random is not the same as not uniform. A non-uniform distribution could generate 0 with probability 1/2, 1 with probability 1/3, etc. – Dennis – 2016-08-19T16:16:48.083

Oh, by the way, convert the new ASCII values back to characters isn't entirely correct. If you add 9 to an ASCII code point, the result could be an integer larger than 127, so it would fall outside the ASCII range. Also, you say the input will only contain characters in the range a-z and A-Z, but your examples have spaces. – Dennis – 2016-08-19T16:18:44.490

@Dennis ._. Fixed. – m654 – 2016-08-19T16:20:52.847

1Uh ... 101 + 2 is 103, not 104. :-) – YetiCGN – 2016-08-19T19:27:07.660

@YetiCGN Haha, sorry. It was a typo. – m654 – 2016-08-20T06:28:13.303

I though as much. ;-) You still have 104 in the text, in the line above it. – YetiCGN – 2016-08-20T07:48:32.933

Answers

5

Jelly, 12 9 bytes

⁵ṁX€’Ṅ+OỌ

Try it online!

How it works

⁵ṁX€’Ṅ+OỌ  Main link. Argument: s (string)

⁵             Set the return value to 10.
 ṁ            Mold; create an array of 10's with the length of s.
  X€          Pseudo-randomly pick a integer between 1 and 10, for each 10.
    ’         Decrement, so the integers fall in the range [0, ..., 9].
     Ṅ        Print the key, as an array, followed by a linefeed.
      +O      Add the integers to the ordinals (code points) of s.
        Ọ     Unordinal; convert back to characters.

Dennis

Posted 2016-08-19T13:39:28.793

Reputation: 196 637

5

Python 3, 130 bytes

Thanks to @Rod for pointing out a bug

from random import*
def f(x):l=10**len(x);k=str(randint(0,l-1)+l)[1:];print(''.join(chr(ord(i)+int(j))for i,j in zip(x,k))+'\n'+k)

A function that takes input via argument as a string and prints to STDOUT.

How it works

from random import*  Import everything from the random module
def f(x):            Function with input string x
l=10**len(x)         Define l for later use as 10^length(x)
randint(0,l-1)+l     Generate a random integer in the range [0, l-1] and add l, giving a
                     number with l+1 digits...
k=str(...)[1:]       ...convert to a string and remove the first character, giving a key of
                     length l that can include leading zeroes, and store in k
for i,j in zip(x,k)  For each character pair i,j in x and k:
chr(ord(i)+int(j))    Find the UTF-8 code-point (same as ASCII for the ASCII characters),
                      add the relevant key digit and convert back to character
''.join(...)         Concatenate the characters of the ciphertext
print(...+'\n'+k)    Add newline and key, then print to STDOUT

Try it on Ideone

TheBikingViking

Posted 2016-08-19T13:39:28.793

Reputation: 3 674

your key generator do not generates keys starting with 0. increasing the boundaries by a factor of 10 and removing the 1st digit should fix: m=10**len(x);k=str(randint(m,m*10))[1:]; and you even save a byte in the process c: – Rod – 2016-08-19T14:45:51.853

@Rod Thanks for pointing out the bug. That won't save any bytes, however, since randint is inclusive, meaning that you would need to do m*10-1. I've just thought of a way to fix it for the same byte count. – TheBikingViking – 2016-08-19T17:09:59.537

3

Pyth - 16 bytes

Waiting for decision by OP on the output formats.

sCM+VCMQKmOTQjkK

Test Suite.

Maltysen

Posted 2016-08-19T13:39:28.793

Reputation: 25 023

I decided on the format. – m654 – 2016-08-19T14:02:52.823

3

Actually, 17 bytes

;`X9J`M;(O¥♂cΣ@εj

Try it online!

Explanation:

;`X9J`M;(O¥♂cΣ@εj
;                  dupe input
 `X9J`M            for each character in input copy:
  X9J                discard the character, push a random integer in [0, 9]
       ;           duplicate the offset array
        (O         bring input to top of stack, ordinal array
          ¥♂c      pairwise addition with offset array, turn each ordinal into a character
             Σ     concatenate
              @εj  concatenate the copy of the offset array

Mego

Posted 2016-08-19T13:39:28.793

Reputation: 32 998

2

PowerShell v2+, 79 77 bytes

param($n)-join(($x=[char[]]$n|%{0..9|Random})|%{[char]($_+$n[$i++])});-join$x

Takes input $n, loops over every character and gets a Random element from 0..9 each iteration. Stores those numbers (as an array) into $x. Pipes that array into another loop. Each iteration, takes the current element $_, adds it to the positional char sliced out of $n (implicit char-to-int cast), then re-casts as [char]. Leaves that on the pipeline. That's encapsulated in parens and -joined together to form the word. That's left on the pipeline. Additionally, the number $x is also -joined together and left on the pipeline. Those are implicitly printed with a Write-Output at the end of execution, which results in them being printed with a newline by default.

Example

PS C:\Tools\Scripts\golfing> .\implement-this-key-cipher.ps1 'Hello World!'
Lhoot(Yt{mf"
433358259121

AdmBorkBork

Posted 2016-08-19T13:39:28.793

Reputation: 41 581

2

CJam - 14 bytes

When I saw the ascii code math, I knew I had to write a CJam answer.

q_{;Amr}%_@.+p

Try it online here.

Maltysen

Posted 2016-08-19T13:39:28.793

Reputation: 25 023

2

MATL, 13 bytes

"10r*]v!kGy+c

The output looks like this:

9 5 8 2 1
Qjtnp

Try it online!

Explanation:

"    ]          % For each character:
 10             % Push a 10 onto the stack
   r            % Push a random float in [O, 1)
    *           % Multiply. This essentially the same thing as pushing a number in [0, 10)
      v!k       % Join all of these together, and take the floor
         G      % Push the input again
          y     % Duplicate the array of random numbers
           +    % And add these together. Since MATL treats strings as an array of chars, we don't need to explicitly convert types
            c   % Display as string

James

Posted 2016-08-19T13:39:28.793

Reputation: 54 537

I'm not sure if that's the right format... – Leaky Nun – 2016-08-19T15:51:13.760

@Leaky Nun I changed the rules a little. – m654 – 2016-08-19T15:56:10.607

@m654 Where did you say there can be spaces between the values? – Leaky Nun – 2016-08-19T15:57:38.567

@LeakyNun Originally there was a rule against them but I removed it. – m654 – 2016-08-19T15:59:19.813

@m654 You haven't allowed it either. – Leaky Nun – 2016-08-19T16:01:28.077

1Good idea to use the loop. It's actually shorter than the multiple-input version of r or Yr – Luis Mendo – 2016-08-19T16:29:51.100

@LuisMendo I've never grokked multiple input versions well. How exactly do you call that? I tried tn&r but that doesn't seem to work right. Same thing with $r and #r – James – 2016-08-19T16:32:49.763

Also, isn't there a shortcut for 10? – James – 2016-08-19T16:33:13.903

I haven't defined & for r yet. So to generate the 1x10 random array without loop you'd need somethinkg like n1H$r!. This is: push input length (n), push 1, next function will take two input arguments (H$), call r with arguments "length" and "1" to produce the random array, transpose (!). There is no shortcut for 10. 2# would specify two outputs for the next function (but r doesn't have a two-output version). These are the possible signagtures of r

– Luis Mendo – 2016-08-19T16:37:26.077

2

05AB1E, 18 17 bytes

vžh.RDyÇ+ç`?}J¶?,

Explanation

v           }      # for each char in input
 žh.RD             # push 2 copies of a random number in [0..9]
      yÇ+          # add 1 copy to the current chars ascii value
         ç`?       # convert to char, flatten and print
             J     # join stack (which contain the digits of the key)
              ¶?,  # print a newline followed by the key

Try it online

Emigna

Posted 2016-08-19T13:39:28.793

Reputation: 50 798

2

C#, 252 247 245 232 216 Bytes

The size is pretty bad compared to the other solutions but nevertheless...

using System;using System.Linq;class p{static void Main(){var c="";var i=Console.ReadLine();var r=new Random();for(int b=0;b++<i.Count();){int d=r.Next(10);Console.Write((char)(i[b]+d));c+=d;}Console.Write("\n"+c);}}

This is my second ever answer to a codegolf and I'm quite a beginner considering C# so I'd appreciate to hear how to get it shorter :)

Ungolfed:

using System;
using System.Linq;

class p
{
    static void Main()
    {
        var c = "";
        var i = Console.ReadLine();
        var r = new Random();
        for (int b = 0; b++ < i.Count();)
        {
            int d = r.Next(10);
            Console.Write((char)(i[b] + d));
            c += d;
        }
        Console.Write("\n" + c);
    }
}
  • Saved 5 Bytes thanks to @FryAmTheEggman
  • Saved 2 Bytes thanks to @theLambGoat
  • Saved 7 Bytes by removing static from class p
  • Saved 24 Bytes thanks to @milk

Tom Doodler

Posted 2016-08-19T13:39:28.793

Reputation: 121

1The trick is not to compare with other languages ;) I'm not particularly well versed in C# golf, but can you do b++<i.Count() and leave the third clause empty? Also, I don't think you need a trailing newline, so the last call to WriteLine could be Write instead. – FryAmTheEggman – 2016-08-19T17:01:46.423

I'm also not well versed in C# but I think you can move the =r.Next(10) up to the declaration of d and save on a set of parenthesis in the write. Or does the random not return an int so you can't do that? – theLambGoat – 2016-08-19T17:26:18.530

I think I can do that, let me check – Tom Doodler – 2016-08-19T18:00:17.143

You can replace types with var. ie- var c= instead of string c= to shave a few bytes. – milk – 2016-08-19T19:55:06.940

Why not leave the result of Console.ReadLine() as string? i.Length is shorter than i.Count(), you won't need System.Linq. string has a char indexer. Also creating new Random objects in the loop is less bytes: new Random().Next(10). – milk – 2016-08-19T20:01:20.923

Creating new Randoms for every random number does always give me the same number for each iteration then, which is certainly not what OP wants, but thanks anyway :) – Tom Doodler – 2016-08-19T20:04:52.330

2

CJam, 11 bytes

Nq{Amr_o+}/

Try it online!

How it works

N            Push a linefeed on the stack.
 q           Read all input from STDIN and push it on the stack.
  {      }/  For each character in the input:
   Amr       Pseudo-randomly pick an integer in [0 ... 9].
      _o     Print a copy.
        +    Add the integer to the character.
             (implicit) Print the linefeed, followed by the modified characters.

Dennis

Posted 2016-08-19T13:39:28.793

Reputation: 196 637

2

Python 3, 112 bytes

c is a function that returns the encrypted text and the key

from random import*
c=lambda t:map(''.join,zip(*[(chr(a+b),str(b))for a,b in((ord(i),randint(0,9))for i in t)]))

Here is a code that does the same thing and is a bit more readable

def encrypt(text):
    # keep the codes of the letters in the input and a random key
    # that will be used later to encrypt this letter
    letter_and_key = ((ord(letter),randint(0,9)) for letter in text)

    # encrypt the letter and keep the key used as a string
    output_and_key = [(chr(letter_code+key), str(key))
                      for letter_code, key in letter_and_key]

    # At this point the values are kept in the format:
    # [(firstletter, firstkey), (secondletter, secondkey), ...]

    # to reorder the list to be able to output in the format "text key"
    text, key = map(''.join, zip(*output_and_key))

    # same as print(*output_and_key)
    return text, key

Output:

>>> text, key = c('Hello World')
>>> print(text, key, sep='\n')
Liuot#`oylk
44935390707

odrling

Posted 2016-08-19T13:39:28.793

Reputation: 21

Welcome to this site! – James – 2016-08-23T21:43:51.707

1

PHP, 63 86 82 bytes

Edit: forgot to print the key...

Thanks to Alex Howansky for saving me 4 bytes.

for(;$i<strlen($a=$argv[1]);$s.=$r)echo chr(ord($a[$i++])+$r=rand(0,9));echo"
$s";

Input is given through a command line argument. Takes each character in the string and adds a random int from 0-9 to its ASCII code, then converts the code back to ASCII. Every random number is appended to $s, which is printed at the end.

Business Cat

Posted 2016-08-19T13:39:28.793

Reputation: 8 927

You need to print the key as well. – Alex Howansky – 2016-08-19T16:05:34.127

You can put the $s.=$r after the 2nd semi in the for loop, saving a byte because you can dump its trailing semi. Then your loop will only be one statement so you can cut out the wrapping braces, saving 2 more bytes. Then at the end, you can put the $s inside the quoted string saving the . operator for one more byte. :) – Alex Howansky – 2016-08-19T16:26:05.210

@AlexHowansky: That's very true. Thanks – Business Cat – 2016-08-19T16:31:41.337

1

J, 32 bytes

<@:e,:~[:<[:u:3&u:+e=.[:?[:$&10#

python equivalent:

from random import randint
def encrypt(message):
    rand_list = list(map(lambda x: randint(0, 9), range(len(message))))
    return (''.join(list(map(lambda x,y: chr(x+y), rand_list, map(ord, message)))), rand_list)

ljeabmreosn

Posted 2016-08-19T13:39:28.793

Reputation: 341

1

Perl, 34 bytes

Includes +1 for -p

#!/usr/bin/perl -p
s%.%$\.=$==rand 10;chr$=+ord$&%eg

Ton Hospel

Posted 2016-08-19T13:39:28.793

Reputation: 14 114

0

Perl, 65 bytes

for(split'',$ARGV[0]){$;.=$a=int rand 9;$b.=chr$a+ord}say"$b\n$;"

Took me awhile to figure out how to get the input without a new line at the end. Takes it as a command line arg

theLambGoat

Posted 2016-08-19T13:39:28.793

Reputation: 119

Your solution has some problems. Input is not read form STDIN, $; does not start empty so it prints the old content and the rand can never generate 9. They are easy to fix and using STDIN will make your code shorter :-) – Ton Hospel – 2016-08-23T14:19:08.497

@TonHospel Usually input requirements are loose and args are acceptable over STDIN and while taking input from STDIN is shorter, having to remove the newline from it makes it longer. And while rand does generate numbers < 9, Perl's int method rounds rather than floors so anything >=8.5 should end up as 9 – theLambGoat – 2016-08-23T14:40:00.937

Input requirements usually are loose, but here it wasn't. Getting non newline from STDIN is easy: <>=~/./g. And no, int in perl truncates towards 0, it doesn't round. perl -wle 'print int 8.6' outputs 8 – Ton Hospel – 2016-08-23T14:50:51.843

0

Python 2, 84 99 bytes

def f(x):y=`id(x)**len(x)`[1:len(x)+1];return''.join(map(chr,[ord(a)+int(b)for a,b in zip(x,y)])),y

Uses the id() value of the string to generate random numbers.

Try it

atlasologist

Posted 2016-08-19T13:39:28.793

Reputation: 2 945

You have to output the key as well as the ciphertext. – TheBikingViking – 2016-08-19T19:33:59.837

@TheBikingViking don't know how I missed that. Thanks--fixed – atlasologist – 2016-08-19T19:47:10.023

I think that this also has the same problem as an earlier version of my Python answer; it never produces keys with leading zeroes. – TheBikingViking – 2016-08-19T20:13:13.430

@TheBikingViking Fixed again – atlasologist – 2016-08-19T20:25:09.593

change map(chr,[ord(a)+int(b)for a,b in zip(x,y)]) to map(lambda x,y:chr(ord(x)+int(y)),x,y)? that should save something – ljeabmreosn – 2016-08-20T03:05:52.287

0

Senva, 74 bytes

Here is the shortest program I've made :

2'(`>0.>{@}0'{v}2-2'0,{@}1'{v}0'{+}{'}9%+{^}{1-}1'"{+}{~}>$10.~0'2+"0,-:>$

A little explanation ? (Note: BM means Back-Memory) :

// === Input and informations storing ===

2'  // Go to the 3rd cell (the two first will be used to store informations)
(   // Ask the user for a string (it will be stored as a suite of ASCII codes)
`   // Go the end of the string
>   // Make a new cell
0.  // Put a 0 to mark the end of the string
>   // Make a new cell, here will be stored the first random number
{@} // Store its adress in BM
0'  // Go to the 1st cell
{v} // Paste the adress, now the 1st cell contains the adress of the first random number
2-  // Subtract 2 because the string starts at adress 2 (the 3rd cell)
2'  // Go to the 3rd cell (where the string begins)

// === String encryption and displaying ===

0,  // While the current cell doesn't contain 0 (while we didn't reach the string's end)
  {@}  // Store the character's adress into the memory
  1'   // Go to the 2nd cell
  {v}  // Paste the value, now the 1st cell contains the adress of the current char
  0'   // Go to the 1st cell
  {+}  // Add the adress of the first random number to the current char adress
  {'}  // Go to this adrses
  9%+  // A random number between 0 and 10
  {^}  // Store this number in BM
  {1-} // Decrease BM (random number between 0 and 9)
  1'   // Go to the 1st cell
  "    // Go to the adress pointed by the cell (the adress of the current char)
  {+}  // Add it to the random number value
  {~}  // Display it as an ASCII character
  >    // Go to the next cell (the next character)
$   // End of the loop
10. // Set the new line's ASCII code into the current cell (which is now useless, so it can be overwritten)
~   // Display the new line
0'  // Go to the first cell
2+  // Add 2 to the adress, because we are not in the string loop : we cancel the 2 substraction
"   // Go to the pointed adress (the first random number's one)

// === Display the random numbers ===

0,  // While we didn't reach the end of the random numbers suite
    // That was why I stored numbers between 1 and 10, the first equal to 0 will be the end of the suite
  - // Decrease it (number between 0 and 9)
  : // Display the current random number as an integer
  > // Go to the next cell (the next number)
$ // End of the loop

That appears bigger now, true :p ? Maybe it's possible to optimize this code, but for the moment that's the shortest I've found.

ClementNerma

Posted 2016-08-19T13:39:28.793

Reputation: 111

0

C#, 174 bytes

using static System.Console;class b{static void Main(){var c=new System.Random();var d="\n";foreach(var e in ReadLine()){var f=c.Next(10);Write((char)(e+f));d+=f;}Write(d);}}

Ungolfed:

using static System.Console;

class b
{
    static void Main()
    {
        var c = new System.Random();
        var d = "\n";

        foreach (var e in ReadLine())
        {
            var f = c.Next(10);
            Write((char)(e + f));
            d += f;
        }

        Write(d);
    }
}

Pretty straightforward, really.

Scepheo

Posted 2016-08-19T13:39:28.793

Reputation: 466

0

Perl 6: 55 or 70 bytes

As an anonymous function that takes a string parameter, and returns a list of two strings (54 characters, 55 bytes):

{my @n=^9 .roll(.ords);(.ords Z+@n)».chr.join,@n.join}

As a program that reads from STDIN and writes to STDOUT (69 characters, 70 bytes):

my @a=get.ords;my @n=^9 .roll(@a);say (@a Z+@n)».chr.join;say @n.join

smls

Posted 2016-08-19T13:39:28.793

Reputation: 4 352