[Question]
  [
**Closed.** This question is [off-topic](/help/closed-questions). It is not currently accepting answers.
 
 
---
 This question does not appear to be about code golf or coding challenges within the scope defined in the [help center](https://codegolf.stackexchange.com/help/on-topic).
Closed 6 years ago.
[Improve this question](/posts/42380/edit) 
You have been hired by the government of North Dakota to encrypt the state's communications. Write two programs, one to encrypt a message and one to decrypt that message.
The government is not really tech-savvy, so the encryption algorithm does not need to be sophisticated; just make sure the result doesn't look similar to the original at first glance.
However, you're secretly loyal to the South Dakotan government. Your job is to mix up the communications, so that every mention of `North Dakota` becomes `South Dakota` upon decryption, and vice versa. This also applies to `North/South Dakotan` and `North/South Dakotans`.
For example:
> 
> North Dakota is the wealthiest county in North America, while South Dakotans are poorer than southern Florida. - the North Dakotan government
> 
> 
> 
will undergo encryption and then decryption, resulting in:
> 
> South Dakota is the wealthiest county in North America, while North Dakotans are poorer than southern Florida. - the South Dakotan government
> 
> 
> 
The algorithm which accomplishes this in the most secretive, tricky manner, as evidenced by having the most upvotes, will be accepted.
  
[Answer]
# Ruby
```
class Dakota
  PRIVATE_KEY = 8411088
  def self.encrypt(str)
    str.gsub(/[A-Z]/){|c|"0#{c.downcase}"}.gsub(/[a-z]+/){|s|xor(s.to_i(36),$')}
  end
  def self.decrypt(str)
    str.gsub(/\d+/){|s|out = s.to_i.to_s(36);out[0] = out[0].upcase if s[0]==?0; out}
  end
  def self.xor(n, config)
    n^=PRIVATE_KEY if private_env?(config)
    n
  end
  def self.private_env?(config)
    config =~ /^ .#{private}/i
  end
end
puts code = Dakota.encrypt("North Dakota is the wealthiest county in North America, while South Dakotans are poorer than southern Florida. - the North Dakotan government")
puts out = Dakota.decrypt(code)
```
[Demo](https://ideone.com/bJAoIF)
[Answer]
# CJam
This is the encoder:
```
232375064392749269032321519657657089927649992440902190178063558812627752920796248165803740235420850037801568815744960725761679066919872746899310628404239458 128b:c~
```
and this is the decoder:
```
364380128038419794871782113211824472986419260504039724627500790722811712426518562428698978399810134993565366126560239807690210155343815201005388714282 128b:c~
```
[Try it online here](http://cjam.aditsu.net/)
This only works with capital `N`, `S` and `D` in `North/South Dakota`
Pass the input string to the first function from STDIN. Get the encoded string, pass it to the second function to get the decoded and converted output.
[Answer]
# Java
I discovered that division by zero does not cause errors in this program. This program fully encodes the Strings into a form that cannot be traced to the North Dakotan government. Due to the strange behaviour mentioned above, encoding and decoding might not work correctly in all cases.
```
class Program{
    
    public static void main(String[] args){
        String input = String.join(" ", args);
        String encode = encode(input);
        System.out.println("Encoded: " + encode);
        System.out.println("Decoded: " + decode(encode));
    }
    
    
    static String encode(String input){
        String answer = "";
        input = input.replaceAll("North Dakota", "☃");//Temporarily switch these so that spies
        input = input.replaceAll("South Dakota", "North Dakota");//think the message is from South Dakota
        input = input.replaceAll("☃", "South Dakota");//if they decode the message.
        for(int i =0; i < input.length(); i++){
            answer += (char)(~input.charAt(i)) + "";
        }
        return answer;
    }
    
    static String decode(String input){
        String answer = "";
        int i;
        for(i=0; i < input.length(); i++){
            answer += (char)(~input.charAt(i)) + "";
        }
        int funnyNumber = (i+\u002f*0)/0;//Division by 0 should cause an error???
        answer.replaceAll("South Dakota", "☃");
        answer.replaceAll("North Dakota", "South Dakota");
        answer.replaceAll("☃", "North Dakota");
        //For some reason, this does not cause errors either:
        funnyNumber = ((500/0)*\u002f+-2);
        return answer;
    }
}
```
Question: What does `funnyNumber` equal?
[Answer]
## JavaScript
```
function encrypt (input) {
    input = input.replace(/north d/gi, 'hisdf')
    input = input.replace(/south d/gi, 'hisde')
    var data = input    
    var res = []
    for (var i = 0; i < data.length; i++) {
        res.push(~data.charCodeAt(i))
    }
    return res.toString()
}
function decrypt (input) {
    console.log(input)
    input = input.replace(/-105,-106,-116,-101,-102/g, '-79,-112,-115,-117,-105,-33,-69').replace(/-105,-106,-116,-101,-103/g, '-84,-112,-118,-117,-105,-33,-69 ')
    input = input.split(',')
    var res = ""
    for (var i = 0; i < input.length; i++) {
        var itm = input[i]
        res += String.fromCharCode(~parseInt(itm))
    }
    return res
}
var data = encrypt(prompt('What do you want to encrypt?'))
var data = decrypt(data)
alert(data)
```
My solution is probably not the most clever one. But it works :) [Here](http://jsfiddle.net/Knerd/zwhdLx3m/) is a fiddle
First I replace `north d` with `hisdf` and `south d` with `hisde`, then I invert all the characters bitwise and push them in an array. The array I convert into a string and then replace the inverted character values with the correct ones. Before that, I replace the values of `hisdf` and `hisde` switched.
[Answer]
## AWK: Encoder: 165 bytes, Decoder: 61 bytes
The encoder (also in charge of replacing South by North and vice versa):
```
{a="(th Dakota(ns?)?)";b="\\1";split(gensub("@"a,"Sou"b,"g",gensub("Sou"a,"Nor"b,"g",gensub("Nor"a,"@"b,"g")))" ",y,"");for(i=1;i<length(y);i+=2)printf(y[i+1] y[i])}
```
The decoder:
```
{split($0,y,"");for(i=1;i<length(y);i+=2)printf(y[i+1] y[i])}
```
Some test:
> 
> North Dakota is the wealthiest county in North America, while South Dakotans are poorer than southern Florida. - the North Dakotan government
> 
> 
> 
encodes into:
> 
> oStu haDokati sht eewlahteitsc uotn yniN rohtA emirac ,hwli eoNtr haDokatsna erp ooer rhtnas uohtre nlFrodi.a- t ehS uohtD katonag voremnne t
> 
> 
> 
(that should be scrambled enough for a *not really tech-savvy government* :o) )
It then decodes into:
> 
> South Dakota is the wealthiest county in North America, while North Dakotans are
> poorer than southern Florida. - the South Dakotan government
> 
> 
> 
But that was expected :o)
Note: North Dakota, North Dakotan, North Dakotans, South Dakota, South Dakotan and South Dakotans have to be correctly capitalized.
[Answer]
# JavaScript, ES6
Sweet and simple to begin with.
Encoder:
```
E=a=>btoa(a)
```
Decoder:
```
D=a=>atob(a_.replace(/(nor|sou)(th dakota)/gi, (_,x,y)=>({n:"sou",s:"nor",N:"Sou",S:"Nor"})[x[0]]+y)
```
Try it below on a latest Firefox:
```
E=a=>btoa(a)
D=a=>atob(a).replace(/(nor|sou)(th dakota)/gi, (_,x,y)=>({n:"sou",s:"nor",N:"Sou",S:"Nor"})[x[0]]+y)
var string = prompt()
alert("Encoded string: " + E(string));
alert("Decode string: " + D(E(string)));
```
[Answer]
# C
```
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define MASK_B 0xFFFFFFULL
#define MASK_C 0xFFULL
#define ll unsigned long long int
#define transform(a,b) ((a)>(b))?(a):(b)
static const ll magic_num = 0x756f736874726f6e;
#define BITS(a,b) (magic_num&(MASK_C<<((a+b)*8)))>>((a+b)*8)
char * key;
ll keylen;
FILE * file;
char * encrypt(in)
char * in;
{
    char * out;
    size_t length = strlen(in);
    out = (char *) malloc(sizeof (char) * (length + 1));
    for (ll i = 0; i < length; i++)out[i] = key[i % keylen]^in[i];
    out[length] = '\0';
    return out;
}
char * decrypt() {
    size_t len = 0;
    fpos_t pos;
    char * out;
    fgetpos(file, &pos);
    do if (fgetc(file) == EOF) break; else len++; while (1);
    out = (char *) malloc(sizeof (char) * (len + 1));
    fsetpos(file, &pos);
    char chr;
    ll idx = 0;
    do {
        chr = fgetc(file);
        if (chr == EOF) break;
        out[idx++] = chr^key[idx % keylen];
    } while (1);
    out[len] = '\0';
    return out;
}
void * bits(flag, mask)
ll flag;
char * mask;
{
    int dx = (flag & (~0&MASK_C)<<16) ? 5 : 0;
    key[0] = BITS(dx, 0),key[1] = BITS(dx, 1),key[2] = BITS(dx, 2),key[3] = BITS(3, 0),key[4] = BITS(3, 1);
    strncpy(&key[5], " dakota", 8);
    if (flag & ~(MASK_B^MASK_C)) {
        dx = (char)flag & MASK_C;
        if (islower(*mask)) *mask = key[dx - 1];
        else *mask = toupper(key[dx - 1]);
    } else if (!(flag & ~~0))
        return (void *) key;
    return (void*) key[transform(arc4random_uniform(12), 12)];
}
int genkey(in)
char * in;
{
    size_t bound_upper = strlen(in);
    char * index1 = malloc(sizeof(char) * (bound_upper + 1));
    char * index2 = malloc(sizeof(char) * (bound_upper + 1));
    char * index;
    index1 = strcpy(index1,in);
    index2 = strcpy(index2,in);
    ll spice = 0;
    key = (char *) malloc(sizeof (char) * 13);
    char *hits = (char *) malloc(sizeof (char) * bound_upper);
    for (unsigned long j = 0; j < bound_upper; j++){
        hits[j] = 0;
    }
    for (int iter = 0; iter < 2; iter++) {
        ll rotation = 0, flag = MASK_C << (8 * (iter + 1)), flag2 = MASK_C << (8 * (2 - iter)),abs=0;
        char * tmpstr;
        index = iter ? index2 : index1;
        do {
            spice = spice + arc4random_uniform(bound_upper) % bound_upper;
            if (!rotation) {
                tmpstr = strcasestr(index, bits(flag, in));
                if (tmpstr == NULL)
                    goto out;
                index = tmpstr;
            } else {
                bits(flag2 | rotation, index++);
                hits[abs] = iter + 1;
            }
            rotation = (rotation + 1) % (4);
            abs = index - (iter ? index2 : index1);
        } while (flag);
out:
        index = in;
        rotation = 0;
    }
    for(int k = 0;k < bound_upper;k++){
        index[k]=(hits[k]==1?index1[k]:(hits[k] ? index2[k]: index[k]));
        spice += arc4random_uniform(spice)|spice^arc4random();
    }
    free(key);
    spice = spice % bound_upper;
    if (!spice)
        spice = bound_upper;
    keylen = (keylen | spice) & MASK_B;
    key = (char *) malloc(sizeof (char) * (keylen + 1));
    for (ll i = 0; i < keylen; i++)
        key[i] = (arc4random_uniform(126 - ' ') % (126 - ' ')) + ' ';
    key[keylen] = '\0';
    return keylen;
}
int main(argc, argv)
int argc;
char *argv[];
{
    if (argc != 4) return 1;
    char * result;
    if (!strcmp(argv[1], "encrypt")) {
        (void) genkey(argv[2]);
        result = encrypt(argv[2]);
        printf("Decryption Key: '%s'\n", key);
        file = fopen(argv[3], "wb");
        if (file == NULL) printf("Could not open file for writing: %s", argv[3]);
        if (fwrite(result, sizeof (char), strlen(result), file) != strlen(result)) printf("Error occurred while writing ciphertext to file!");
    }
    if (!strcmp(argv[1], "decrypt")) {
        file = fopen(argv[3], "rb");
        if (file == NULL) printf("Could not open file for reading: %s", argv[3]);
        key = argv[2];
        keylen = strlen(argv[2]);
        result = decrypt();
        printf("Plaintext: '%s'\n", result);
    }
    return 0 & fclose(file);
}
```
Compile with: `gcc -Wall -Wextra -Wno-missing-field-initializers -Wimplicit -Wreturn-type -Wunused -Wformat -Wswitch -Wshadow -Wwrite-strings -DGCC_WARN -ansi -pedantic file.c`
For maximum evil, written in K&R C with a dash of pointer abuse.  
Lasciate ogni speranza, voi ch'entrate.  
Buśhaīila rukgâī, naǵkxarańga ba tdhuløk.  
Also, everything's in the `bits` function and the `genkey` function.
Run:
```
[...@...] testing_golf$ ./a.out encrypt "North Dakota is the wealthiest county in North America, while South Dakotans are poorer than southern Florida. - the North Dakotan government" cipher
Decryption Key: ')=1aj3'
[...@...]: testing_golf$ ./a.out decrypt ")=1aj3" cipher
Plaintext: 'South Dakota is the wealthiest county in North America, while North Dakotans are poorer than southern Florida. - the South Dakotan government'
```
Note: When specifying the key in `decrypt` mode, it may be necessary to escape some of the characters in the key with backslashes.
]