Key Dependent Encoding
                    
                        Encryption, or Encoding, simply is the art of leaving data in
                        obfuscated form in order to keep it secure.
                        
                        It could be as simple as replacing all characters in a text file with those
                        from a predetermined set in a manner that has the original text having
                        a direct correlation with the encrypted / encoded version.
                        An example of this is seen in the Base-64 Encoding System.
                        
                        Encryption could also mean adding junk data to an original data
                        in such a way that the original data is completely defaced but can
                        still be comfortably extracted from the encrypted version.
                        
                        Encryption processes must always be consistent: i.e. the same type of
                        encryption, or encoding, carried out on the same data or file must always
                        produce exactly the same obfuscated output.
                    
                        But with key dependent encryption, every unique key produces a completely
                        different encoded set or obfuscated data.
                        This ensures better security of data since brute-forcing becomes very difficult
                        without knowledge of the secret key used.
                        Also, the longer the key used, the more useless brute-forcing becomes.
                        
                        This means that different individuals or firms can employ the same encryption process,
                        but have their own unique secret key (Private Key) to encrypt data with.
                        Such encrypted data cannot be comfortably decrypted by a second firm using the 
                        same encryption process if the first firm can keep its key secret enough.
                    
Recurrent Sequences or Series
                        Remember Sequences and Series from Ordinary Level Mathematics;
                        Recurrent Series to be precise? They become as useful as they can be here!
                        
                        Recurrent Series has the unique characteristic that all succeeding terms in
                        a progression are totally dependent on all preceding terms - i.e. for any
                        Recurrent Series, any n+1th term cannot be determined unless the
                        value of the nth term and its predecessors are known;
                        and even more true is the fact that every other term in the progression
                        is absolutely dependent on the 1st term of the series.
                    
So given the recurrent series
                        tn+1 = 3tn + 1;
                    
                        a canonical form of the same equation can be extrapolated where any term tn
                        can be found only with the value of t1 known.
                        This canonical equation is derived by noting and generalising the 
                        pattern that successive terms exhibit, viz:
                    
                           tn+1 = 3tn + 1;
                        Finding t2 and onward terms
                        When n = 1:
                           t2 = 3t1 + 1;
                        When n = 2:
                           t3 = 3(3t1 + 1) + 1
                              = 32t1 + 3 + 1;
                        When n = 3:
                           t4 = 3(32t1 + 3 + 1) + 1
                              = 33t1 + 32 + 3 + 1;
                        When n = 4:
                           t5 = 3(33t1 + 32 + 3 + 1) + 1
                              = 34t1 + 33 + 32 + 3 + 1;
                        
                        Following the observed pattern, it can be generalised that for any n:
                        Tn = 3n-1t1 + 3n-2 + 3n-3 + … + 32 + 31 + 30;
                        
                        The progression of terms after the first term suggests the summation of terms in a geometric sequence.
                        For any geometric progression, the nth term, Tn, is given by
                           Tn = arn-1;
                        and Sum of Terms, Sn is given viz:
                           Sn = a + ar + ar2 + … + arn-1;
                    
| Sn = | a(rn - 1) | 
| r - 1 | 
                        For our culminating geometric series, first term is 1, common ratio is 3,
                        but last term is arn-2 which leaves a shorting of 1 in the power of r
                        for any normal term.
                        Hence for our geometric sequence,
                    
| Sn = | a(rn-1 - 1) | 
| r - 1 | |
| ⇒ Sn = | 3n-1 - 1 | 
| 2 | 
Hence, the resulting general relation for our recurrent series becomes
| Tn = 3n-1t1 + | 3n-1 - 1 | 
| 2 | |
| or | |
| Tn = | 3n-1(2t1 + 1) - 1 | 
| 2 | |
                        The afore property is what we will be exploiting in the Java algorithm for encrypting
                        data with reference to single secret keys.
                        
                        
                        Create a new Java class file;
                        Call it SoleKeyEncryption
.
                        
                        Type out the adjoining Java code for encrypting a chunk of data
                        with a secret key.
                    
                        Important: BigInteger is inbuilt in Java.
                        You only need to import the java.math.BigInteger
 library.
                    
Java Code for SoleKeyEncryption Class
import java.math.BigInteger;
public class SoleKeyEncryption {
public SoleKeyEncryption() {
}
public String[] encodeWord(char[] msg, char[] key) {
// encoding eqn { Tn = 3^n-1(2t1 + 1) - 1 } - please use your own eqn
// 2
String[] encryption = new String[msg.length];
int n;
int t1;
BigInteger Tn;
for (int i = 0; i < msg.length; i++) {
// get unicode of this character as t1
t1 = (int) msg[i];
// get next key digit as n
n = Integer.parseUnsignedInt(Character.toString(key[i % (key.length - 1)]), 16);
// use recurrence series equation to encrypt & save in base 16
Tn = BigInteger.valueOf(3).pow(n - 1).multiply(BigInteger.valueOf(2 * t1 + 1)).subtract(BigInteger.ONE).divide(BigInteger.valueOf(2));
encryption[i] = Tn.toString(16);
}
return encryption;
}
public String decodeWord(String[] code, char[] key) {
// decoding eqn { t1 = 3^1-n(2Tn + 1) - 1 }
// 2
String decryption = "";
int n;
BigInteger t1;
BigInteger Tn;
for (int i = 0; i < code.length; i++) {
Tn = new BigInteger(code[i], 16);
// get next key digit as n
n = Integer.parseUnsignedInt(Character.toString(key[i % (key.length - 1)]), 16);
// use recurrence series equation to decrypt
t1 = BigInteger.valueOf(2 * Tn.intValue() + 1).divide(BigInteger.valueOf(3).pow(n - 1)).subtract(BigInteger.ONE).divide(BigInteger.valueOf(2));
decryption += Character.toString((char) t1.intValue());
}
return decryption;
}
}
Main Class
import java.util.Arrays;
public class Miscellaneous {
public static void main(String[] args) {
char[] message = "merry xmas".toCharArray();
char[] key = "A5FB17C4D8".toCharArray(); // you might want to avoid zeroes
SoleKeyEncryption go_secure = new SoleKeyEncryption();
String[] encrypted = go_secure.encodeWord(message, key);
System.out.println("Message is '" + String.valueOf(message) +
"';\nEncrypted version is " + Arrays.toString(encrypted));
String decrypted = go_secure.decodeWord(encrypted, key);
System.out.println("\n\nDecrypted version is '" + decrypted + "'.");
}
}