Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- public class SPN {
- // Private Fields
- private int x; // plaintext
- private int y; // ciphertext
- private int k; // key
- private int r; // number of rounds
- private int m; // number of substrings
- private int l; // length of substrings (# of bits in substring)
- private int[] sbox; // sbox operation
- private int[] perm; // permutation operation
- private ArrayList<String> results = new ArrayList<String>();
- // Private Methods
- // Converts integer to binary string of m*l bits
- private String Bin2String(int bin, int len){
- String binString = Integer.toBinaryString(bin);
- while (binString.length() < len){
- binString = "0" + binString;
- }
- return binString;
- }
- // XOR Operation
- private int XOR(int w, int k) {
- return w^k;
- }
- // Sbox Operation
- private int Sbox(int u) {
- int v = 0;
- int mask = (int) (Math.pow(2, this.l) - 1);
- for(int i = 0; i < this.m; i++) {
- v += this.sbox[(mask & u) >> i*this.l] << i*this.l;
- mask = mask << this.l;
- }
- return v;
- }
- // Permutation Operation
- private int Permutation(int v) {
- String temp = Bin2String(v, this.l*this.m);
- String w = Bin2String(0, this.l*this.m);
- for(int i=0; i < this.perm.length; i++) {
- w = w.substring(0, this.perm[i]) + temp.charAt(i) + w.substring(this.perm[i]+1);
- }
- return Integer.parseInt(w, 2);
- }
- // Default Key Scheduler
- // for given round i
- private int KeyScheduler(int i){
- String key = Bin2String(this.k, this.l*(this.r + this.m));
- int start = (i-1)*this.l;
- int end = start + this.l*this.m;
- return Integer.parseInt(key.substring(start, end), 2);
- }
- // Reset results to perform another encryption/decryption
- private void reset() {
- this.results.clear();
- this.results.add("Stage: \t\t Binary: \t\t Hex: ");
- }
- // Print spaces between binary strings for easy parsing
- private String insertSpaces(String str) {
- String temp = "";
- for(int i = 1; i < this.m; i++) {
- temp += str.substring((i-1)*this.l, i*this.l) + " ";
- }
- return temp + str.substring((this.m-1)*this.l);
- }
- // Convert binary to Hex Strings
- private String Bin2HexString(int bin) {
- String hex = Integer.toHexString(bin);
- while (hex.length() < 4) {
- hex = "0" + hex;
- }
- return hex;
- }
- // Public Methods
- // Main Constructor
- public SPN(int x, int y, int k, int r, int m, int l, int[] sbox, int[] perm) {
- this.x = x;
- this.y = y;
- this.k = k;
- this.r = r;
- this.m = m;
- this.l = l;
- this.sbox = sbox;
- this.perm = perm;
- this.results.add("Stage: \t\t Binary: \t\t Hex: ");
- }
- // Set new plaintext
- public void setPlaintext(int x) {
- this.x = x;
- }
- // Set new ciphertext
- public void setCiphertext(int y) {
- this.y = y;
- }
- // Set new key
- public void setKey(String k) {
- this.k = Integer.parseUnsignedInt(k, 2);
- }
- // Encrypt with given plaintext
- public void encrypt(){
- this.reset();
- int len = this.l*this.m;
- int w = this.x;
- this.results.add("X: \t\t " + insertSpaces(Bin2String(w, len)) + "\t "
- + Bin2HexString(w));
- for (int i = 1; i < r; i++){
- int key = KeyScheduler(i);
- this.results.add("K" + i + ": \t\t " + insertSpaces(Bin2String(key, len))
- + "\t " + Bin2HexString(key));
- int u = XOR(w, key);
- this.results.add("U" + i + ": \t\t " + insertSpaces(Bin2String(u, len))
- + "\t " + Bin2HexString(u));
- int v = Sbox(u);
- this.results.add("V" + i + ": \t\t " + insertSpaces(Bin2String(v, len))
- + "\t " + Bin2HexString(v));
- w = Permutation(v);
- this.results.add("W" + i + ": \t\t " + insertSpaces(Bin2String(w, len))
- + "\t " + Bin2HexString(w));
- }
- this.results.add("K" + this.r + ": \t\t "
- + insertSpaces(Bin2String(KeyScheduler(this.r), len)) + "\t "
- + Bin2HexString(KeyScheduler(this.r)));
- int u = XOR(w, KeyScheduler(this.r));
- this.results.add("U" + this.r + ": \t\t " + insertSpaces(Bin2String(u, len))
- + "\t " + Bin2HexString(u));
- int v = Sbox(u);
- this.results.add("V" + this.r + ": \t\t " + insertSpaces(Bin2String(v, len))
- + "\t " + Bin2HexString(v));
- this.results.add("K" + (this.r + 1) + ": \t\t "
- + insertSpaces(Bin2String(KeyScheduler(this.r + 1), len))
- + "\t " + Bin2HexString(KeyScheduler(this.r + 1)));
- this.y = XOR(v, KeyScheduler(this.r + 1));
- this.results.add("Y: \t\t " + insertSpaces(Bin2String(this.y, len))
- + "\t " + Bin2HexString(this.y));
- }
- // Decrypt with given ciphertext
- public void decrypt(){
- this.reset();
- int len = this.l*this.m;
- int y = this.y;
- this.results.add("Y: \t\t " + insertSpaces(Bin2String(y, len)) + "\t "
- + Bin2HexString(y));
- int v = XOR(y, KeyScheduler(this.r + 1));
- this.results.add("K" + (this.r + 1) + ": \t\t "
- + insertSpaces(Bin2String(KeyScheduler(this.r + 1), len))
- + "\t " + Bin2HexString(KeyScheduler(this.r + 1)));
- this.results.add("V" + this.r + ": \t\t " + insertSpaces(Bin2String(v, len))
- + "\t " + Bin2HexString(v));
- int u = Sbox(v); // inverse sbox
- this.results.add("U" + this.r + ": \t\t " + insertSpaces(Bin2String(u, len))
- + "\t " + Bin2HexString(u));
- this.results.add("K" + this.r + ": \t\t "
- + insertSpaces(Bin2String(KeyScheduler(this.r), len)) + "\t "
- + Bin2HexString(KeyScheduler(this.r)));
- int w = XOR(u, KeyScheduler(this.r));
- this.results.add("W" + (this.r-1) + ": \t\t " + insertSpaces(Bin2String(w, len))
- + "\t " + Bin2HexString(w));
- for (int i = r - 1; i >= 1; i--){
- int key = KeyScheduler(i);
- v = Permutation(w);
- this.results.add("V" + i + ": \t\t " + insertSpaces(Bin2String(v, len))
- + "\t " + Bin2HexString(v));
- u = Sbox(v);
- this.results.add("U" + i + ": \t\t " + insertSpaces(Bin2String(u, len))
- + "\t " + Bin2HexString(u));
- this.results.add("K" + i + ": \t\t " + insertSpaces(Bin2String(key, len))
- + "\t " + Bin2HexString(key));
- w = XOR(u, KeyScheduler(i));
- if(i != 1) {
- this.results.add("W" + (i-1) + ": \t\t " + insertSpaces(Bin2String(w, len))
- + "\t " + Bin2HexString(w));
- }
- }
- int x = w;
- this.results.add("X: \t\t " + insertSpaces(Bin2String(x, len)) + "\t "
- + Bin2HexString(x));
- }
- // Display only plaintext and resulting ciphertext
- public void displaySimpleResults() {
- System.out.println("Plaintext: " + Bin2String(x, this.l*this.m)
- + "\nCiphertext: " + Bin2String(y, this.l*this.m));
- }
- // Display detailed internal results of SPN
- public void displayDetailedResults() {
- for(int i = 0; i < this.results.size(); i++) {
- System.out.println(this.results.get(i));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement