-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
815 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package src.java; | ||
|
||
public interface BlockCipher | ||
{ | ||
|
||
/** | ||
* Returns this block cipher's block size in bytes. | ||
* | ||
* @return Block size. | ||
*/ | ||
public int blockSize(); | ||
|
||
/** | ||
* Returns this block cipher's key size in bytes. | ||
* | ||
* @return Key size. | ||
*/ | ||
public int keySize(); | ||
|
||
/** | ||
* Set the key for this block cipher. <TT>key</TT> must be an array of bytes | ||
* whose length is equal to <TT>keySize()</TT>. | ||
* | ||
* @param key Key. | ||
*/ | ||
public void setKey | ||
(byte[] key); | ||
|
||
/** | ||
* src.java.Encrypt the given plaintext. <TT>text</TT> must be an array of bytes | ||
* whose length is equal to <TT>blockSize()</TT>. On input, <TT>text</TT> | ||
* contains the plaintext block. The plaintext block is encrypted using the | ||
* key specified in the most recent call to <TT>setKey()</TT>. On output, | ||
* <TT>text</TT> contains the ciphertext block. | ||
* | ||
* @param text Plaintext (on input), ciphertext (on output). | ||
*/ | ||
public void encrypt | ||
(byte[] text); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
package src.java; /** | ||
* src.java.Decrypt.java | ||
* version : ak | ||
* Revision: $log ak$ | ||
*/ | ||
|
||
import src.java.utils.Utils; | ||
|
||
/** | ||
* This program is an implementation of block cipher Speck. | ||
* It decrypts the given ciphertext by XORing with key generated | ||
* from 22 round key scheduler | ||
* | ||
* @author Ajinkya Kale | ||
* | ||
*/ | ||
|
||
public class Decrypt { | ||
|
||
short [] k0= new short[22]; // stores subkeys | ||
short [] l0 =new short [22]; // stores L0 values | ||
short [] l1 = new short[22]; // stores L1 values | ||
short [] l2 = new short [22]; // stores L2 values | ||
byte[] key; // stoes key | ||
byte[] plaintext; // stores plaintext | ||
|
||
/** | ||
* Constructor initilizing key and plaintext | ||
*/ | ||
public Decrypt(byte [] key, byte []plaintext) { | ||
this.key= key; | ||
this.plaintext=plaintext; | ||
} | ||
|
||
|
||
/** | ||
* This method sets the initial values of the key k0,lo,l1,l2 | ||
*/ | ||
|
||
public void setKey(byte[] key) { | ||
long key_1= Utils.packLongBigEndian(key, 0); | ||
k0[0]= (short)(key_1 & 0x000000000000FFFFL); | ||
l0[0]= (short)((key_1 & 0x00000000FFFF0000L)>>16); | ||
l1[0]= (short)((key_1 & 0x0000FFFF00000000L)>>32); | ||
l2[0]= (short)((key_1 & 0xFFFF000000000000L)>>48); | ||
} | ||
|
||
/** | ||
* returns blocksize | ||
*/ | ||
|
||
public int blockSize() { | ||
return 32; | ||
} | ||
|
||
/** | ||
* returns keysize | ||
*/ | ||
|
||
public int keySize() { | ||
return 64; | ||
} | ||
|
||
/** | ||
* This method produceds 22 subkeys required to generate ciphertext | ||
* | ||
*/ | ||
public void key_schedule1(){ | ||
int count=1; | ||
int l=1,k=1,m=1,j=1; | ||
int first=0, second=0, third=0; | ||
for(int i=0; i<21;i++){ | ||
if( count==1){ | ||
l0[l]= (short)((k0[i] + l_right_rotate(l0[first]))^ (short)i); | ||
k0[j] = (short)( k_left_rotate(k0[i])^ l0[l]); | ||
l++; | ||
j++; | ||
first++; | ||
} | ||
|
||
if(count ==2){ | ||
l1[k]= (short) ((k0[i] + l_right_rotate(l1[second]))^ (short)i); | ||
k0[j] =(short) ( k_left_rotate(k0[i]) ^ l1[k]); | ||
k++; | ||
j++; | ||
second++; | ||
} | ||
if(count == 3){ | ||
l2[m]= (short) ((k0[i] +l_right_rotate(l2[third]))^(short)i); | ||
k0[j]= (short) ((k_left_rotate(k0[i])) ^ l2[m]); | ||
m++; | ||
j++; | ||
third++; | ||
} | ||
count++; | ||
if(count>3){ | ||
count=1; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* This method perfoms right rotation by 7 | ||
* @param s | ||
* @return temp | ||
*/ | ||
private short l_right_rotate(short s) { | ||
short x= (short) ((s& 0x0000FFFF)>>7); | ||
short y= (short) ((s& 0x0000FFFF)<<9); | ||
short temp = (short) (y|x); | ||
return temp; | ||
} | ||
|
||
/** | ||
* This method performs left rotation by 2 | ||
* @param s | ||
* @return temp | ||
*/ | ||
private short k_left_rotate(short s){ | ||
short y= (short)( (s& 0x0000FFFF)<<2); | ||
short x= (short)((s& 0x0000FFFF)>>14); | ||
short temp = (short) (y|x); | ||
return temp; | ||
} | ||
|
||
/** | ||
* This method performs right rotate vy 2 positions | ||
* @param s | ||
* @return temp | ||
*/ | ||
private short right_rotate_by_2(short s){ | ||
short y= (short)( (s& 0x0000FFFF)>>2); | ||
short x= (short)((s& 0x0000FFFF)<<14); | ||
short temp = (short) (y|x); | ||
return temp; | ||
} | ||
|
||
/** | ||
* This method perform left rotation by 7 | ||
* @param s | ||
* @return temp | ||
*/ | ||
private short left_rotate_by_7(short s){ | ||
short x= (short) ((s& 0x0000FFFF)<<7); | ||
short y= (short) ((s& 0x0000FFFF)>>9); | ||
short temp = (short) (y|x); | ||
return temp; | ||
} | ||
|
||
/** | ||
* This method decrypts the ciphertext using the subkey in reverse order. | ||
* Decryption consists of 22 rounds | ||
*/ | ||
|
||
public void decrypt(byte [] text){ | ||
int dec=0; | ||
int ciphertext = Utils.packIntBigEndian(text, 0); | ||
short x = (short) ((ciphertext & 0xFFFF0000)>>16); | ||
short y = (short) ((ciphertext & 0x0000FFFF)); | ||
|
||
for(int i=21; i>=0 ; i--){ | ||
short value1= (short)(x ^ y); | ||
y= right_rotate_by_2((short)(x ^ y)); | ||
short value2= (short) (x ^ k0[i]); | ||
short value3 = (short)((x ^ k0[i])- y); | ||
x= (short)(left_rotate_by_7((short)((x ^ k0[i])- y))); | ||
dec = x<<16 | ( y&0x0000FFFF); | ||
|
||
} | ||
byte[] temp2 = Utils.toByteArray(Utils.toString(dec)); | ||
text[0]= temp2[0]; | ||
text[1]= temp2[1]; | ||
text[2]= temp2[2]; | ||
text[3]= temp2[3]; | ||
|
||
} | ||
|
||
/** | ||
*This method is executed if args are not sufficient. | ||
* program exits here. | ||
*/ | ||
private static void usage() { | ||
System.err.println ("Usage: java src.java.EncryptFile <key> <ctfile>"); | ||
System.err.println ("<ctfile> = ciphertext file name"); | ||
System.err.println ("<key> = Key (64 hex digits)"); | ||
System.exit (1); | ||
} | ||
/** | ||
* This is main program | ||
* @param args commandline arguments | ||
*/ | ||
public static void main(String[] args) { | ||
if(args.length !=2){ | ||
usage(); | ||
} | ||
byte[] key =Utils.toByteArray(args[0]); | ||
byte[] plaintext = Utils.toByteArray(args[1]); | ||
Decrypt s= new Decrypt(key, plaintext); | ||
s.setKey(key); | ||
s.key_schedule1(); | ||
s.decrypt(plaintext); | ||
System.out.println(Utils.toString(plaintext)); // this prints the plaintext output | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package src.java;/* | ||
* src.java.Encrypt.java | ||
* Version : $ak$ | ||
* Revision: log $ak$ | ||
*/ | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.BufferedOutputStream; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import src.java.utils.Utils; | ||
|
||
/** | ||
* This program Decrypts the file, in ECB mode. | ||
* This is an implementation of block cipher SPECK | ||
* @author Ajinkya Kale | ||
* | ||
*/ | ||
|
||
public class DecryptFile { | ||
/** | ||
* This is main program | ||
* @param args commandline arguments | ||
*/ | ||
public static void main(String[] args) throws IOException{ | ||
if(args.length!=3){ | ||
usage(); | ||
} | ||
|
||
|
||
File plaintext= new File (args[1]); // plaintext file | ||
File ciphertext= new File( args[2]); // cipher text file | ||
byte [] key = Utils.toByteArray(args[0]); // key | ||
InputStream cipher_t= new BufferedInputStream | ||
(new FileInputStream (plaintext)); | ||
OutputStream plain_t = new BufferedOutputStream | ||
(new FileOutputStream (ciphertext)); | ||
|
||
Path path = Paths.get(plaintext.getAbsolutePath()); | ||
byte [] p = Files.readAllBytes(path); // reads all the bytes of file | ||
|
||
Decrypt e = new Decrypt(key, p); | ||
e.setKey(key); // sets the key | ||
e.key_schedule1(); // generates subkeys | ||
|
||
int iter=0; | ||
while( iter != p.length){ // decrypts the 4 bytes at a time | ||
int prev= iter; | ||
byte [] temp = {(byte) (p[iter]) ,(byte)(p[++iter]), (byte)(p[++iter]), (byte)(p[++iter])}; | ||
int temp2= Utils.packIntBigEndian(temp, 0); | ||
Utils.unpackIntBigEndian(temp2, temp, 0); | ||
e.decrypt(temp); | ||
p[prev]= temp[0]; | ||
p[++prev]= temp[1]; | ||
p[++prev]= temp[2]; | ||
p[++prev]= temp[3]; | ||
prev=0; | ||
iter++; | ||
} | ||
|
||
byte [] t = remove_padding(p); // orginaml plaintext without padding | ||
// writies plaintext without padding to the file | ||
int c=0; | ||
while(c!= t.length){ | ||
plain_t.write(t[c]); | ||
c++; | ||
} | ||
plain_t.close(); | ||
cipher_t.close(); | ||
|
||
} | ||
|
||
/** | ||
* This method removes padding from ciphertext | ||
* @param ciphertext byte array | ||
* @return temp | ||
*/ | ||
public static byte[] remove_padding(byte [] ciphertext_ ){ | ||
int i=ciphertext_.length-1; | ||
int counter=0; | ||
List<Byte> u = new ArrayList<Byte>(); | ||
byte[] temp; | ||
|
||
while(ciphertext_[i]== 0x0000){ | ||
counter++; | ||
i--; | ||
} | ||
|
||
|
||
for( int j=0; j<(ciphertext_.length-(counter+1)); j++){ | ||
u.add(ciphertext_[j]); | ||
} | ||
|
||
temp= new byte[ciphertext_.length-(counter+1)]; | ||
|
||
for(int k=0; k< temp.length; k++){ | ||
temp[k]= u.get(k); | ||
} | ||
return temp; | ||
|
||
} | ||
|
||
/** | ||
* Print a usage message and exit. | ||
*/ | ||
private static void usage() { | ||
System.err.println ("Usage: java src.java.DecryptFile <key> <ctfile> <plaintext>"); | ||
System.err.println ("<ptfile> = Plaintext file name"); | ||
System.err.println ("<ctfile> = Ciphertext file name"); | ||
System.err.println ("<key> = Key(64 hex digits)"); | ||
System.exit (1); | ||
} | ||
} |
Oops, something went wrong.