Q:

Issue with Decryption Algorithm

I am having a little trouble attempting to reverse my encryption algorithm. The algorithm is a variation on the Caesar Cipher, and works like so:
(V1 + V2 + D) % 26 + 1

Where V1 is the letter of the phrase to be encrypted mapped to the relevant number (A = 1, etc), V2 is the letter of the users chosen key (the same length as the phrase), again mapped to the relevant number, and D is a chosen displacement value, ranging from 1 - 10.
The issue I am having is in attempting to reverse this. I have tried simply reversing the algorithm:
(V1 - V2 - D) % 26 - 1

but this obviously fails, due to the modulus involved. I have also attempted:
(V1 - V2 - D + 26) % 26

on the advice of here, but again this failed. It would be extremely helpful if somebody would be able to show me the solution to this, or at least point me in the correct direction.

A:

(v1 - v2 - d + 50) % 26 + 1

which is equivalent to
(v1 - v2 - d - 1 - 1 + 26 + 26) % 26 + 1

One of the - 1 is to compensate for the + 1 in the encryption. The other - 1 and the matching + 1 outside of the modulus is to wrap 0 back to 26 while leaving the rest untouched. And the reason for having 26 twice is simply that one 26 is not enough, since v1 - v2 - d - 1 - 1 can go down to 1 - 26 - 10 - 1 - 1, or -37.
import itertools

def encode(v1, v2, d):
    return (v1 + v2 + d) % 26 + 1

def decode(v1, v2, d):
    return (v1 - v2 - d + 50) % 26 + 1

all_combinations = map(tuple, itertools.product(
    range(1, 27), range(1, 27), range(1, 11)))

all(decode(encode(v1, v2, d), v2, d) == v1 for v1, v2, d in all_combinations)
# => True

