diff --git a/build.xml b/build.xml new file mode 100644 index 0000000..ce52359 --- /dev/null +++ b/build.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + Builds, tests, and runs the project EncryptDecrypt. + + + diff --git a/build/built-jar.properties b/build/built-jar.properties new file mode 100644 index 0000000..9684c27 --- /dev/null +++ b/build/built-jar.properties @@ -0,0 +1,4 @@ +#Wed, 18 Jul 2018 02:04:10 +0600 + + +D\:\\Java\ Codes\\EncryptDecrypt= diff --git a/build/classes/encryptdecrypt/ArtCover.jpg b/build/classes/encryptdecrypt/ArtCover.jpg new file mode 100644 index 0000000..dcef43f Binary files /dev/null and b/build/classes/encryptdecrypt/ArtCover.jpg differ diff --git a/build/classes/encryptdecrypt/DES.class b/build/classes/encryptdecrypt/DES.class new file mode 100644 index 0000000..47fd6a8 Binary files /dev/null and b/build/classes/encryptdecrypt/DES.class differ diff --git a/build/classes/encryptdecrypt/DES.rs b/build/classes/encryptdecrypt/DES.rs new file mode 100644 index 0000000..1429b8a --- /dev/null +++ b/build/classes/encryptdecrypt/DES.rs @@ -0,0 +1 @@ +encryptdecrypt.DES diff --git a/build/classes/encryptdecrypt/EncryptDecrypt$1.class b/build/classes/encryptdecrypt/EncryptDecrypt$1.class new file mode 100644 index 0000000..5a4663c Binary files /dev/null and b/build/classes/encryptdecrypt/EncryptDecrypt$1.class differ diff --git a/build/classes/encryptdecrypt/EncryptDecrypt.class b/build/classes/encryptdecrypt/EncryptDecrypt.class new file mode 100644 index 0000000..279e865 Binary files /dev/null and b/build/classes/encryptdecrypt/EncryptDecrypt.class differ diff --git a/build/classes/encryptdecrypt/EncryptDecrypt.rs b/build/classes/encryptdecrypt/EncryptDecrypt.rs new file mode 100644 index 0000000..dca08ff --- /dev/null +++ b/build/classes/encryptdecrypt/EncryptDecrypt.rs @@ -0,0 +1,2 @@ +encryptdecrypt.EncryptDecrypt$1 +encryptdecrypt.EncryptDecrypt diff --git a/build/classes/encryptdecrypt/Project_image.jpg b/build/classes/encryptdecrypt/Project_image.jpg new file mode 100644 index 0000000..dcef43f Binary files /dev/null and b/build/classes/encryptdecrypt/Project_image.jpg differ diff --git a/build/classes/encryptdecrypt/RC4.class b/build/classes/encryptdecrypt/RC4.class new file mode 100644 index 0000000..91c94e5 Binary files /dev/null and b/build/classes/encryptdecrypt/RC4.class differ diff --git a/build/classes/encryptdecrypt/RC4.rs b/build/classes/encryptdecrypt/RC4.rs new file mode 100644 index 0000000..1fad6e3 --- /dev/null +++ b/build/classes/encryptdecrypt/RC4.rs @@ -0,0 +1 @@ +encryptdecrypt.RC4 diff --git a/build/classes/encryptdecrypt/TripleDES.class b/build/classes/encryptdecrypt/TripleDES.class new file mode 100644 index 0000000..f72c8de Binary files /dev/null and b/build/classes/encryptdecrypt/TripleDES.class differ diff --git a/build/classes/encryptdecrypt/TripleDES.rs b/build/classes/encryptdecrypt/TripleDES.rs new file mode 100644 index 0000000..f3181b9 --- /dev/null +++ b/build/classes/encryptdecrypt/TripleDES.rs @@ -0,0 +1 @@ +encryptdecrypt.TripleDES diff --git a/build/classes/encryptdecrypt/UtilFile.class b/build/classes/encryptdecrypt/UtilFile.class new file mode 100644 index 0000000..7d3e4dc Binary files /dev/null and b/build/classes/encryptdecrypt/UtilFile.class differ diff --git a/build/classes/encryptdecrypt/UtilFile.rs b/build/classes/encryptdecrypt/UtilFile.rs new file mode 100644 index 0000000..5650d9c --- /dev/null +++ b/build/classes/encryptdecrypt/UtilFile.rs @@ -0,0 +1 @@ +encryptdecrypt.UtilFile diff --git a/dist/EncryptDecrypt.jar b/dist/EncryptDecrypt.jar new file mode 100644 index 0000000..09a014f Binary files /dev/null and b/dist/EncryptDecrypt.jar differ diff --git a/dist/How To Use.txt b/dist/How To Use.txt new file mode 100644 index 0000000..445f8a8 Binary files /dev/null and b/dist/How To Use.txt differ diff --git a/dist/README.TXT b/dist/README.TXT new file mode 100644 index 0000000..b48b109 --- /dev/null +++ b/dist/README.TXT @@ -0,0 +1,32 @@ +======================== +BUILD OUTPUT DESCRIPTION +======================== + +When you build an Java application project that has a main class, the IDE +automatically copies all of the JAR +files on the projects classpath to your projects dist/lib folder. The IDE +also adds each of the JAR files to the Class-Path element in the application +JAR files manifest file (MANIFEST.MF). + +To run the project from the command line, go to the dist folder and +type the following: + +java -jar "EncryptDecrypt.jar" + +To distribute this project, zip up the dist folder (including the lib folder) +and distribute the ZIP file. + +Notes: + +* If two JAR files on the project classpath have the same name, only the first +JAR file is copied to the lib folder. +* Only JAR files are copied to the lib folder. +If the classpath contains other types of files or folders, these files (folders) +are not copied. +* If a library on the projects classpath also has a Class-Path element +specified in the manifest,the content of the Class-Path element has to be on +the projects runtime path. +* To set a main class in a standard Java project, right-click the project node +in the Projects window and choose Properties. Then click Run and enter the +class name in the Main Class field. Alternatively, you can manually type the +class name in the manifest Main-Class element. diff --git a/manifest.mf b/manifest.mf new file mode 100644 index 0000000..1574df4 --- /dev/null +++ b/manifest.mf @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +X-COMMENT: Main-Class will be added automatically by build + diff --git a/nbproject/build-impl.xml b/nbproject/build-impl.xml new file mode 100644 index 0000000..08b6cc3 --- /dev/null +++ b/nbproject/build-impl.xml @@ -0,0 +1,1420 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No tests executed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set JVM to use for profiling in profiler.info.jvm + Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + Must select one file in the IDE or set profile.class + This target only works when run from inside the NetBeans IDE. + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + + + Must select some files in the IDE or set test.includes + + + + + Must select one file in the IDE or set run.class + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + Must select some files in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + Must select one file in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/nbproject/genfiles.properties b/nbproject/genfiles.properties new file mode 100644 index 0000000..c4d6d32 --- /dev/null +++ b/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=a64f7383 +build.xml.script.CRC32=351b0b0d +build.xml.stylesheet.CRC32=8064a381@1.80.1.48 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=a64f7383 +nbproject/build-impl.xml.script.CRC32=6b2d5a37 +nbproject/build-impl.xml.stylesheet.CRC32=830a3534@1.80.1.48 diff --git a/nbproject/private/private.properties b/nbproject/private/private.properties new file mode 100644 index 0000000..e5930ea --- /dev/null +++ b/nbproject/private/private.properties @@ -0,0 +1,2 @@ +compile.on.save=true +user.properties.file=C:\\Users\\Nirzak\\AppData\\Roaming\\NetBeans\\8.2\\build.properties diff --git a/nbproject/private/private.xml b/nbproject/private/private.xml new file mode 100644 index 0000000..fbb017b --- /dev/null +++ b/nbproject/private/private.xml @@ -0,0 +1,13 @@ + + + + + + file:/D:/Java%20Codes/EncryptDecrypt/src/EncryptDecrypt/EncryptDecrypt.java + file:/D:/Java%20Codes/EncryptDecrypt/src/EncryptDecrypt/TripleDES.java + file:/D:/Java%20Codes/EncryptDecrypt/src/EncryptDecrypt/UtilFile.java + file:/D:/Java%20Codes/EncryptDecrypt/src/EncryptDecrypt/DES.java + file:/D:/Java%20Codes/EncryptDecrypt/src/EncryptDecrypt/RC4.java + + + diff --git a/nbproject/project.properties b/nbproject/project.properties new file mode 100644 index 0000000..a4d80c6 --- /dev/null +++ b/nbproject/project.properties @@ -0,0 +1,74 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=false +annotation.processing.processor.options= +annotation.processing.processors.list= +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.test.classpath=\ + ${run.test.classpath} +# Files in build.classes.dir which should be excluded from distribution jar +dist.archive.excludes= +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/EncryptDecrypt.jar +dist.javadoc.dir=${dist.dir}/javadoc +excludes= +includes=** +jar.compress=false +javac.classpath= +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.external.vm=true +javac.processorpath=\ + ${javac.classpath} +javac.source=1.8 +javac.target=1.8 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +main.class=encryptdecrypt.EncryptDecrypt +manifest.file=manifest.mf +meta.inf.dir=${src.dir}/META-INF +mkdist.disabled=false +platform.active=default_platform +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +# Space-separated list of JVM arguments used when running the project. +# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. +# To set system properties for unit tests define test-sys-prop.name=value: +run.jvmargs= +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +source.encoding=UTF-8 +src.dir=src +test.src.dir=test diff --git a/nbproject/project.xml b/nbproject/project.xml new file mode 100644 index 0000000..a4fb7e9 --- /dev/null +++ b/nbproject/project.xml @@ -0,0 +1,15 @@ + + + org.netbeans.modules.java.j2seproject + + + EncryptDecrypt + + + + + + + + + diff --git a/src/EncryptDecrypt/ArtCover.jpg b/src/EncryptDecrypt/ArtCover.jpg new file mode 100644 index 0000000..dcef43f Binary files /dev/null and b/src/EncryptDecrypt/ArtCover.jpg differ diff --git a/src/EncryptDecrypt/DES.java b/src/EncryptDecrypt/DES.java new file mode 100644 index 0000000..71c8f0d --- /dev/null +++ b/src/EncryptDecrypt/DES.java @@ -0,0 +1,358 @@ +package encryptdecrypt; + +import javax.swing.JOptionPane; + +public class DES{ + private static int[] Table_IP = { 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, + 36,28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, + 16, 8, 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 61, + 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7 + }; + private static int[] Table_FP = { 40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, + 15, 55, 23, 63, 31, 38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, + 61, 29, 36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51,19, 59, 27, 34, + 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25 + }; + private static int[] Table_P = { 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, + 26, 5, 18, 31, 10, 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, + 25 + }; + private static int[] Table_Expand = { 32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, + 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17, 16, 17, 18, 19, 20, 21, 20, + 21, 22, 23, 24, 25, 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1 }; + + //const for sboxes + private static int[][][] Table_SBox = { + { { 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7 }, + { 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8 }, + { 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0 }, + { 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 } + }, + { { 15, 1, 8, 14, 6, 11, 3, 2, 9, 7, 2, 13, 12, 0, 5, 10 }, + { 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5 }, + { 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15 }, + { 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 } + }, + { { 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8 }, + { 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1 }, + { 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7 }, + { 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 } + }, + { { 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15 }, + { 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9 }, + { 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4 }, + { 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 } + }, + { { 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9 }, + { 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6 }, + { 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14 }, + { 11, 8, 12, 7, 1, 14, 2, 12, 6, 15, 0, 9, 10, 4, 5, 3 } + }, + { { 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11 }, + { 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8 }, + { 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6 }, + { 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 } + + }, + { { 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1 }, + { 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6 }, + { 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2 }, + { 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 } + + }, + { { 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7 }, + { 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2 }, + { 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8 }, + { 2, 1, 14, 7, 4, 10, 18, 13, 15, 12, 9, 0, 3, 5, 6, 11 } + + } }; + + //const for key preparation + //permutation 64 -> 56 bit + private static int[] Table_P1 = { + 15, 7, 57, 49, 41, 33, 25, 17,26, 18, 10, 2, 59, 51, 43, 35, 27, 19, 9, + 1, 58, 50, 42, 34, 11, 3, 60,52, 44, 36, 63, 45, 37, 29, 21, 13, 5, 28, + 20, 12, 4,62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 55, 47, 39, 31, 23 + }; + //permutation 56 -> 48 bit + private static int[] Table_P2 = { 23, 19, 12, 4, 26, 8, 16, 14, 17, 3, 28, + 15, 6, 21, 10, 39, 56, 34, 53, 46, 42, 50, 36, 40, 51, 45, 33, 20, 13, 2, + 41, 52, 30, 48, 44, 49, 29, 32, 1, 5, 7, 27, 31, 37, 11, 24, 47, 55 + }; + //shift value for each position + private static int[] Table_Rotation = { 1, 2, 2, 1, 2, 1, 2, 2, 1, 2, 2, 1, + 1, 1, 2, 2 + }; + + + static byte[][] desKey = new byte[16][]; + + public void Encode(String key, String path){ + System.out.println("Encode DES algorithm"); + + prepareKey(key.getBytes());//calling method for preparing key + byte[] result = makeOutput(new UtilFile().read(path), true); + + new UtilFile().write(result,path);//File Creation + JOptionPane.showMessageDialog(null, "Your file is encoded successfully", "Encode Status", JOptionPane.INFORMATION_MESSAGE); + } + + public void Decode(String key, String path){ + System.out.println("Decode DES algorithm"); + + prepareKey(key.getBytes());//calling method for preparing key + byte[] result = makeOutput(new UtilFile().read(path), false); + + new UtilFile().write(result,path);//File Creation + JOptionPane.showMessageDialog(null, "Your file is decoded successfully", "Decode Status", JOptionPane.INFORMATION_MESSAGE); + } + + public byte[] Encode_onfly(String key, byte[] data){ + System.out.println("Encode ON FLY DES algorithm"); + + prepareKey(key.getBytes()); + return makeOutput(data, true); + } + + public byte[] Decode_onfly(String key, byte[] data){ + System.out.println("Decode ON FLY DES algorithm"); + + prepareKey(key.getBytes()); + return makeOutput(data, false); + } + + + private void prepareKey(byte[] byteKey){ + // PC1 permutation -> 56 + byte[] tmpK = P_Func(byteKey, Table_P1); + + // first 28 bits + byte[] F = Get_Bits(tmpK, 0, 28); + // second 28 bits + byte[] S = Get_Bits(tmpK, 28, 28); + + // generate 16 subkeys + for (int i = 0; i < 16; i++) { + + //left rotation bit + F = LR_Bits(F, 28, Table_Rotation[i]); + S = LR_Bits(S, 28, Table_Rotation[i]); + + //bond both 28 bits array + byte[] sub56key = Bond_Bits(F, 28, S, 28); + + //PC2 permutation -> 48 + desKey[i] = P_Func(sub56key, Table_P2); + } + } + + private byte[] makeOutput(final byte[] dataFile, boolean encode){ + + byte[] B_64bits = new byte[8]; + + if(encode){ + //add padding if file is not 64 bit % + int i, lenght=0; + lenght = 8 - dataFile.length % 8; + + byte[] padding = new byte[1]; + padding = new byte[lenght]; + padding[0] = (byte) 0x80; + for (i = 1; i < lenght; i++) + padding[i] =(byte) 0x0f; + + byte[] cipherFile = new byte[dataFile.length + lenght]; + + int count = 0; + for (i = 0; i < dataFile.length + lenght; i++) { + //encrypt bloc if have 64 bits + if (i > 0 && i % 8 == 0) { + B_64bits = elaborate64bits(B_64bits, true); + System.arraycopy(B_64bits, 0, cipherFile, i - 8, B_64bits.length); + } + //fill the bloc + if (i < dataFile.length) + B_64bits[i % 8] = dataFile[i]; + else{ + B_64bits[i % 8] = padding[count % 8]; + count++; + } + } + //encrypt last bloc + if(B_64bits.length == 8){ + B_64bits = elaborate64bits(B_64bits, true); + System.arraycopy(B_64bits, 0, cipherFile, i - 8, B_64bits.length); + } + + return cipherFile; + } else { + int i; + byte[] plainFile = new byte[dataFile.length]; + + for (i = 0; i < dataFile.length; i++) { + if (i > 0 && i % 8 == 0) { + B_64bits = elaborate64bits(B_64bits, false); + System.arraycopy(B_64bits, 0, plainFile, i - 8, B_64bits.length); + } + if (i < dataFile.length) + B_64bits[i % 8] = dataFile[i]; + } + B_64bits = elaborate64bits(B_64bits, false); + System.arraycopy(B_64bits, 0, plainFile, i - 8, B_64bits.length); + + //remove padding + int count = 0; + i = plainFile.length - 1; + while (plainFile[i] ==(byte) 0x0f) { + count++; + i--; + } + + byte[] tmp = new byte[plainFile.length - count - 1]; + System.arraycopy(plainFile, 0, tmp, 0, tmp.length); + + return tmp; + } + } + + private static byte[] elaborate64bits(byte[] array, boolean encode) { + byte[] tmp = new byte[array.length]; + byte[] first32bits = new byte[array.length / 2]; + byte[] second32bits = new byte[array.length / 2]; + + tmp = P_Func(array, Table_IP); + + second32bits = Get_Bits(tmp, 0, Table_IP.length/2); + first32bits = Get_Bits(tmp, Table_IP.length/2, Table_IP.length/2); + + for (int i = 0; i < 16; i++) { + byte[] tmpR = first32bits; + if(encode) + first32bits = F_Func(first32bits, desKey[15-i]); + else + first32bits = F_Func(first32bits, desKey[i]); + + first32bits = XOR_Func(second32bits, first32bits); + second32bits = tmpR; + } + + tmp = Bond_Bits(first32bits, Table_IP.length/2, second32bits, Table_IP.length/2); + + tmp = P_Func(tmp, Table_FP); + return tmp; + } + + private static byte[] F_Func(byte[] a, byte[] b) { + byte[] tmp; + tmp = P_Func(a, Table_Expand); + tmp = XOR_Func(tmp, b); + tmp = S_Func(tmp); + tmp = P_Func(tmp, Table_P); + return tmp; + } + + private static byte[] S_Func(byte[] array) { + array = Sunder_Bytes(array, 6); + byte[] out = new byte[array.length / 2]; + int halfByte = 0; + for (int b = 0; b < array.length; b++) { + byte valByte = array[b]; + int r = 2 * (valByte >> 7 & 0x0001) + (valByte >> 2 & 0x0001); + int c = valByte >> 3 & 0x000F; + int val = Table_SBox[b][r][c]; + if (b % 2 == 0) + halfByte = val; + else + out[b / 2] = (byte) (16 * halfByte + val); + } + return out; + } + + + + // util bit + /////////// + private static byte[] P_Func(byte[] input, int[] table) { + int nrBytes = (table.length - 1) / 8 + 1; + byte[] out = new byte[nrBytes]; + for (int i = 0; i < table.length; i++) { + int val = Get_Bit(input, table[i] - 1); + Set_Bit(out, i, val); + } + return out; + } + + private static byte[] Get_Bits(byte[] input, int pos, int n) { + int num = (n - 1) / 8 + 1; + byte[] out = new byte[num]; + for (int i = 0; i < n; i++) { + int val = Get_Bit(input, pos + i); + Set_Bit(out, i, val); + } + return out; + } + + private static void Set_Bit(byte[] data, int pos, int val) { + int posB = pos / 8; + int posb = pos % 8; + byte tmpB = data[posB]; + tmpB = (byte) (((0xFF7F >> posb) & tmpB) & 0x00FF); + byte newByte = (byte) ((val << (8 - (posb + 1))) | tmpB); + data[posB] = newByte; + } + + private static int Get_Bit(byte[] data, int pos) { + int posB = pos / 8; + int posb = pos % 8; + byte tmpB = data[posB]; + int bit = tmpB >> (8 - (posb + 1)) & 0x0001; + return bit; + } + + private static byte[] LR_Bits(byte[] input, int len, int pas) { + int num = (len - 1) / 8 + 1; + byte[] out = new byte[num]; + for (int i = 0; i < len; i++) { + int val = Get_Bit(input, (i + pas) % len); + Set_Bit(out, i, val); + } + return out; + } + + private static byte[] Bond_Bits(byte[] a, int aLen, byte[] b, int bLen) { + int num = (aLen + bLen - 1) / 8 + 1; + byte[] out = new byte[num]; + int j = 0; + for (int i = 0; i < aLen; i++) { + int val = Get_Bit(a, i); + Set_Bit(out, j, val); + j++; + } + for (int i = 0; i < bLen; i++) { + int val = Get_Bit(b, i); + Set_Bit(out, j, val); + j++; + } + return out; + } + + private static byte[] Sunder_Bytes(byte[] array, int len) { + int num = (8 * array.length - 1) / len + 1; + byte[] out = new byte[num]; + for (int i = 0; i < num; i++) { + for (int j = 0; j < len; j++) { + int val = Get_Bit(array, len * i + j); + Set_Bit(out, 8 * i + j, val); + } + } + return out; + } + + private static byte[] XOR_Func(byte[] a, byte[] b) { + byte[] out = new byte[a.length]; + for (int i = 0; i < a.length; i++) { + out[i] = (byte) (a[i] ^ b[i]); + } + return out; + } +} diff --git a/src/EncryptDecrypt/EncryptDecrypt.java b/src/EncryptDecrypt/EncryptDecrypt.java new file mode 100644 index 0000000..9c9ad81 --- /dev/null +++ b/src/EncryptDecrypt/EncryptDecrypt.java @@ -0,0 +1,258 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package encryptdecrypt; + +/** + * + * @author Nirzak + */ +import java.awt.*; +import java.awt.event.*; +import java.io.*; + +import javax.swing.*; + +public class EncryptDecrypt extends JPanel implements ActionListener { + + //Labels to identify the fields + private JLabel KeyLabel; + private JLabel TypeLabel; + private JLabel FileLabel; + private JLabel Imagelabel; + + //Strings for the labels + private static String KeyString = "Encode/Decode Key : "; + private static String TypeString = "Encryption Type : "; + private static String FileString = "Path to File : "; + + //Fields for data entry + private JTextField KeyField; + private JTextField FileField; + private JComboBox TypeList; + + //Button for access + private JButton btnEncode; + private JButton btnDecode; + private JButton btnFile; + + // Font for writting + private Font labelFont; + private Font textFont; + private Font buttonFont; + private Font ComboFont; + + // For using hand cursor + private Cursor cursor; + + //For showing header image + private ImageIcon image; + + + public EncryptDecrypt() { + // TypeArray for Combo Box + String[] TypeArr = {"RC4", "DES", "3DES"}; + + // Block for controling fonts + labelFont = new Font("TIMES NEW ROMAN", Font.BOLD, 18); + textFont = new Font("TIMES NEW ROMAN", Font.BOLD, 20); + buttonFont = new Font("TIMES NEW ROMAN", Font.BOLD, 22); + + // Creating hand cursor + cursor= new Cursor(Cursor.HAND_CURSOR); + + //Creating image & imagelabel + image= new ImageIcon(getClass().getResource("Project_image.jpg")); + + //Creating the labels & set their properties + Imagelabel= new JLabel(image); + Imagelabel.setBounds(0,0, 900, 200); + + + KeyLabel = new JLabel(KeyString); + KeyLabel.setBounds(10, 220, 175, 40); + KeyLabel.setBackground(Color.BLACK); + KeyLabel.setOpaque(true); + KeyLabel.setForeground(Color.orange); + KeyLabel.setFont(labelFont); + + TypeLabel = new JLabel(TypeString); + TypeLabel.setBounds(10, 270, 175, 40); + TypeLabel.setBackground(Color.black); + TypeLabel.setOpaque(true); + TypeLabel.setForeground(Color.orange); + TypeLabel.setFont(labelFont); + + FileLabel = new JLabel(FileString); + FileLabel.setBounds(10, 320, 175, 40); + FileLabel.setBackground(Color.black); + FileLabel.setOpaque(true); + FileLabel.setForeground(Color.orange); + FileLabel.setFont(labelFont); + + //Creating the text fields and set their properties. + KeyField = new JTextField(""); + KeyField.setBounds(220, 220, 500, 40); + KeyField.setBackground(Color.black); + KeyField.setForeground(Color.green); + KeyField.setFont(textFont); + KeyField.setToolTipText("Please input key more than 8 characters"); + + TypeList = new JComboBox(TypeArr); + TypeList.setBounds(220, 270, 500, 40); + TypeList.setBackground(Color.black); + TypeList.setForeground(Color.green); + TypeList.setToolTipText("Please use this Combo Box to use other method"); + TypeList.setFont(new Font("TIMES NEW ROMAN", Font.BOLD, 24)); + + TypeList.setSelectedIndex(0); + + FileField = new JTextField(""); + FileField.setBounds(220, 320, 500, 40); + FileField.setBackground(Color.black); + FileField.setForeground(Color.green); + FileField.setCursor(cursor); + FileField.setFont(textFont); + + //Creating buttons & set their properties. + btnEncode = new JButton("Encode File"); + btnEncode.setBounds(20, 370, 160, 50); + btnEncode.setBackground(Color.black); + btnEncode.setForeground(Color.yellow); + btnEncode.setFont(buttonFont); + btnEncode.setCursor(cursor); + btnEncode.setToolTipText("Click here to encode"); + btnEncode.setVerticalTextPosition(AbstractButton.CENTER); + btnEncode.setHorizontalTextPosition(AbstractButton.CENTER); + btnEncode.addActionListener(this); + + btnDecode = new JButton("Decode File"); + btnDecode.setBounds(220, 370, 500, 50); + btnDecode.setBackground(Color.black); + btnDecode.setForeground(Color.yellow); + btnDecode.setFont(buttonFont); + btnDecode.setCursor(cursor); + btnDecode.setToolTipText("Click here to decode"); + btnDecode.setVerticalTextPosition(AbstractButton.CENTER); + btnDecode.setHorizontalTextPosition(AbstractButton.CENTER); + btnDecode.addActionListener(this); + + btnFile = new JButton("Select File"); + btnFile.setBounds(720, 320, 150, 50); + btnFile.setBackground(Color.black); + btnFile.setForeground(Color.yellow); + btnFile.setFont(buttonFont); + btnFile.setCursor(cursor); + btnFile.setToolTipText("Click here to select the file"); + btnFile.setVerticalTextPosition(AbstractButton.CENTER); + btnFile.setHorizontalTextPosition(AbstractButton.CENTER); + btnFile.addActionListener(this); + + + // Setting panel components to panel + this.add(Imagelabel); + this.add(KeyLabel); + this.add(TypeLabel); + this.add(FileLabel); + + this.add(KeyField); + this.add(TypeList); + this.add(FileField); + + this.add(btnFile); + this.add(btnEncode); + this.add(btnDecode); + + this.setBounds(0, 0, 900, 500); + this.setLayout(null); + this.setBackground(Color.darkGray); + } + //Action Event Block + @Override + public void actionPerformed(ActionEvent e) { + //action event for encoding + if (e.getSource() == btnEncode) { + String key = KeyField.getText(); + String filePath = FileField.getText(); + int typeKey = TypeList.getSelectedIndex(); + + if (key.length() < 8 && filePath.length() < 1) { + JOptionPane.showMessageDialog(this, "Error.The key must be 8 char min and specific a path to file.", + "Error", JOptionPane.ERROR_MESSAGE); + } else { + switch (typeKey) { + case 0: + //encode RC4 + new RC4().Encode(key, filePath); + break; + case 1: + //encode DES + new DES().Encode(key, filePath); + break; + case 2: + //encode 3DES + new TripleDES().Encode(key, filePath); + break; + } + } + } //action event for decoding + else if (e.getSource() == btnDecode) { + String key = KeyField.getText(); + String filePath = FileField.getText(); + int typeKey = TypeList.getSelectedIndex(); + + if (key.length() < 8 && filePath.length() < 1) { + JOptionPane.showMessageDialog(this, "Error. The key must be 8 char min and specific a path to file.", + "Error", JOptionPane.ERROR_MESSAGE); + } else { + switch (typeKey) { + case 0: + //decode RC4 + new RC4().Decode(key, filePath); + break; + case 1: + //decode DES + new DES().Decode(key, filePath); + break; + case 2: + //decode 3DES + new TripleDES().Decode(key, filePath); + break; + + } + } + } //action event for choosing file to encrypt or decrypt + else if (e.getSource() == btnFile) { + JFileChooser fc = new JFileChooser(); + int returnVal = fc.showOpenDialog(EncryptDecrypt.this); + + if (returnVal == JFileChooser.APPROVE_OPTION) { + File file = fc.getSelectedFile(); + FileField.setText(file.getPath()); + } + } + } + //Creating and showing frame + private static void createAndShowFrame() { + JFrame frame = new JFrame("EncryptDecrypt"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setBounds(250, 250, 900, 500); + frame.setResizable(false); + frame.add(new EncryptDecrypt()); + frame.setLayout(null); + frame.setVisible(true); + } + + public static void main(String[] args) { + + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + UIManager.put("swing.boldMetal", Boolean.FALSE); + createAndShowFrame(); + } + }); + } +} diff --git a/src/EncryptDecrypt/Project_image.jpg b/src/EncryptDecrypt/Project_image.jpg new file mode 100644 index 0000000..dcef43f Binary files /dev/null and b/src/EncryptDecrypt/Project_image.jpg differ diff --git a/src/EncryptDecrypt/RC4.java b/src/EncryptDecrypt/RC4.java new file mode 100644 index 0000000..5666e94 --- /dev/null +++ b/src/EncryptDecrypt/RC4.java @@ -0,0 +1,59 @@ +package encryptdecrypt; + +import javax.swing.JOptionPane; + +public class RC4{ + + private final byte[] S = new byte[256]; + private final byte[] T = new byte[256]; + private int keylen; + + public void Encode(String key, String path){ + System.out.println("Encode RC4 algorithm"); + //calling method for preparing key by Key Scheduling algorithm + prepareKey(key.getBytes()); + //creating output of the file + makeOutput(new UtilFile().read(path), path); + JOptionPane.showMessageDialog(null, "Your file is successfully encoded", "Encode Status", JOptionPane.INFORMATION_MESSAGE); + } + + public void Decode(String key, String path){ + System.out.println("Decode RC4 algorithm"); + + prepareKey(key.getBytes()); + makeOutput(new UtilFile().read(path), path); + JOptionPane.showMessageDialog(null, "Your file is successfully decoded", "Decode Status", JOptionPane.INFORMATION_MESSAGE); + } + //*preparing key block where we will apply key scheduling algorithm to the kry and then we will generate the key stream by appying PRGA algorithm*/ + private void prepareKey(byte[] byteKey){ + keylen = byteKey.length; + for (int i = 0; i < 256; i++) { + S[i] = (byte) i; + T[i] = byteKey[i % keylen]; + } + int j = 0; + for (int i = 0; i < 256; i++) { + j = (j + S[i] + T[i]) & 0xFF; + S[i] ^= S[j]; + S[j] ^= S[i]; + S[i] ^= S[j]; + } + } + //*encrypting or decrypting block where we will xor the key stream with file bytes so that together they can create the desired cipherfile*/ + private void makeOutput(final byte[] plainFile, String pathFile){ + final byte[] cipherFile = new byte[plainFile.length]; + int i = 0, j = 0, k, t; + for (int counter = 0; counter < plainFile.length; counter++) { + i = (i + 1) & 0xFF; + j = (j + S[i]) & 0xFF; + S[i] ^= S[j]; + S[j] ^= S[i]; + S[i] ^= S[j]; + t = (S[i] + S[j]) & 0xFF; + k = S[t]; + cipherFile[counter] = (byte) (plainFile[counter] ^ k); + } + + new UtilFile().write(cipherFile,pathFile);//creating file output + } +} diff --git a/src/EncryptDecrypt/TripleDES.java b/src/EncryptDecrypt/TripleDES.java new file mode 100644 index 0000000..a7d4ce4 --- /dev/null +++ b/src/EncryptDecrypt/TripleDES.java @@ -0,0 +1,385 @@ +package encryptdecrypt; + +import javax.swing.JOptionPane; + +public class TripleDES{ + + + private static int[] Table_IP = { 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, + 36,28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, + 16, 8, 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 61, + 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7 + }; + private static int[] Table_FP = { 40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, + 15, 55, 23, 63, 31, 38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, + 61, 29, 36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51,19, 59, 27, 34, + 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25 + }; + private static int[] Table_P = { 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, + 26, 5, 18, 31, 10, 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, + 25 + }; + private static int[] Table_Expand = { 32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, + 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17, 16, 17, 18, 19, 20, 21, 20, + 21, 22, 23, 24, 25, 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1 }; + + private static int[][][] Table_SBox = { + { { 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7 }, + { 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8 }, + { 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0 }, + { 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 } + }, + { { 15, 1, 8, 14, 6, 11, 3, 2, 9, 7, 2, 13, 12, 0, 5, 10 }, + { 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5 }, + { 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15 }, + { 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 } + }, + { { 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8 }, + { 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1 }, + { 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7 }, + { 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 } + }, + { { 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15 }, + { 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9 }, + { 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4 }, + { 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 } + }, + { { 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9 }, + { 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6 }, + { 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14 }, + { 11, 8, 12, 7, 1, 14, 2, 12, 6, 15, 0, 9, 10, 4, 5, 3 } + }, + { { 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11 }, + { 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8 }, + { 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6 }, + { 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 } + + }, + { { 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1 }, + { 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6 }, + { 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2 }, + { 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 } + + }, + { { 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7 }, + { 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2 }, + { 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8 }, + { 2, 1, 14, 7, 4, 10, 18, 13, 15, 12, 9, 0, 3, 5, 6, 11 } + + } }; + + //const for key preparation + //permutation 64 -> 56 bit + private static int[] Table_P1 = { + 15, 7, 57, 49, 41, 33, 25, 17,26, 18, 10, 2, 59, 51, 43, 35, 27, 19, 9, + 1, 58, 50, 42, 34, 11, 3, 60,52, 44, 36, 63, 45, 37, 29, 21, 13, 5, 28, + 20, 12, 4,62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 55, 47, 39, 31, 23 + }; + //permutation 56 -> 48 bit + private static int[] Table_P2 = { 23, 19, 12, 4, 26, 8, 16, 14, 17, 3, 28, + 15, 6, 21, 10, 39, 56, 34, 53, 46, 42, 50, 36, 40, 51, 45, 33, 20, 13, 2, + 41, 52, 30, 48, 44, 49, 29, 32, 1, 5, 7, 27, 31, 37, 11, 24, 47, 55 + }; + //shift value for each position + private static int[] Table_Rotation = { 1, 2, 2, 1, 2, 1, 2, 2, 1, 2, 2, 1, + 1, 1, 2, 2 + }; + + //string key + static String[] TripleDESKey=new String[3]; + //byte key + private static byte[][] desKey0; + private static byte[][] desKey1; + private static byte[][] desKey2; + + + public void Encode(String key, String path){ + System.out.println("Encode 3DES algorithm"); + + //generate key0, key1, key2 + TripleDESKey[0]=new String(key); + desKey0 = prepareKey(key.getBytes()); + + String keyReverse = new StringBuffer(key).reverse().toString(); + TripleDESKey[1]=new String(keyReverse); + desKey1 = prepareKey(keyReverse.getBytes()); + + String keySwap = key.substring(key.length()/2,key.length())+ + key.substring(0,key.length()/2); + TripleDESKey[2]=new String(keySwap); + desKey2 = prepareKey(keySwap.getBytes()); + + makeOutput(new UtilFile().read(path),path,true); + JOptionPane.showMessageDialog(null, "Your file is encoded successfully", "Encode Status", JOptionPane.INFORMATION_MESSAGE); + } + + public void Decode(String key, String path){ + System.out.println("Decode 3DES algorithm"); + + //generate key0, key1, key2 + TripleDESKey[0]=new String(key); + desKey0 = prepareKey(key.getBytes()); + + String keyReverse = new StringBuffer(key).reverse().toString(); + TripleDESKey[1]=new String(keyReverse); + desKey1 = prepareKey(keyReverse.getBytes()); + + String keySwap = key.substring(key.length()/2,key.length())+ + key.substring(0,key.length()/2); + TripleDESKey[2]=new String(keySwap); + desKey2 = prepareKey(keySwap.getBytes()); + + makeOutput(new UtilFile().read(path),path,false); + JOptionPane.showMessageDialog(null, "Your file is decoded successfully", "Decode Status", JOptionPane.INFORMATION_MESSAGE); + } + + private byte[][] prepareKey(byte[] byteKey){ + byte[][] desTmp = new byte[16][]; + // PC1 permutation -> 56 + byte[] tmpK = P_Func(byteKey, Table_P1); + + // first 28 bits + byte[] F = Get_Bits(tmpK, 0, 28); + // second 28 bits + byte[] S = Get_Bits(tmpK, 28, 28); + + // generate 16 subkeys + for (int i = 0; i < 16; i++) { + + //left rotation bit + F = LR_Bits(F, 28, Table_Rotation[i]); + S = LR_Bits(S, 28, Table_Rotation[i]); + + //bond both 28 bits array + byte[] sub56key = Bond_Bits(F, 28, S, 28); + + //PC2 permutation -> 48 + desTmp[i] = P_Func(sub56key, Table_P2); + } + + return desTmp; + } + + private void makeOutput(final byte[] dataFile, String pathFile,boolean encode){ + + byte[] B_64bits = new byte[8]; + + if(encode){ + //add padding if file is not 64 bit % + int i, lenght=0; + lenght = 8 - dataFile.length % 8; + + byte[] padding = new byte[1]; + padding = new byte[lenght]; + padding[0] = (byte) 0x80; + for (i = 1; i < lenght; i++) + padding[i] =(byte) 0x0f; + + byte[] cipherFile = new byte[dataFile.length + lenght]; + + int count = 0; + for (i = 0; i < dataFile.length + lenght; i++) { + //encrypt bloc if have 64 bits + if (i > 0 && i % 8 == 0) { + B_64bits = elaborate64bits(B_64bits,desKey0, true); + B_64bits = elaborate64bits(B_64bits,desKey1, false); + B_64bits = elaborate64bits(B_64bits,desKey2, true); + + System.arraycopy(B_64bits, 0, cipherFile, i - 8, B_64bits.length); + } + //filling the block + if (i < dataFile.length) + B_64bits[i % 8] = dataFile[i]; + else{ + B_64bits[i % 8] = padding[count % 8]; + count++; + } + } + //encrypt last block + if(B_64bits.length == 8){ + B_64bits = elaborate64bits(B_64bits,desKey0, true); + B_64bits = elaborate64bits(B_64bits,desKey1, false); + B_64bits = elaborate64bits(B_64bits,desKey2, true); + + System.arraycopy(B_64bits, 0, cipherFile, i - 8, B_64bits.length); + } + + new UtilFile().write(cipherFile,pathFile); + } else { + int i; + byte[] plainFile = new byte[dataFile.length]; + + for (i = 0; i < dataFile.length; i++) { + if (i > 0 && i % 8 == 0) { + B_64bits = elaborate64bits(B_64bits,desKey2, false); + B_64bits = elaborate64bits(B_64bits,desKey1, true); + B_64bits = elaborate64bits(B_64bits,desKey0, false); + + System.arraycopy(B_64bits, 0, plainFile, i - 8, B_64bits.length); + } + if (i < dataFile.length) + B_64bits[i % 8] = dataFile[i]; + } + B_64bits = elaborate64bits(B_64bits,desKey2, false); + B_64bits = elaborate64bits(B_64bits,desKey1, true); + B_64bits = elaborate64bits(B_64bits,desKey0, false); + + System.arraycopy(B_64bits, 0, plainFile, i - 8, B_64bits.length); + + //remove padding + int count = 0; + i = plainFile.length - 1; + while (plainFile[i] ==(byte) 0x0f) { + count++; + i--; + } + + byte[] tmp = new byte[plainFile.length - count - 1]; + System.arraycopy(plainFile, 0, tmp, 0, tmp.length); + + + new UtilFile().write(tmp,pathFile); + } + } + + private static byte[] elaborate64bits(byte[] array,byte[][] key, boolean encode) { + byte[] tmp = new byte[array.length]; + byte[] first32bits = new byte[array.length / 2]; + byte[] second32bits = new byte[array.length / 2]; + + tmp = P_Func(array, Table_IP); + + second32bits = Get_Bits(tmp, 0, Table_IP.length/2); + first32bits = Get_Bits(tmp, Table_IP.length/2, Table_IP.length/2); + + for (int i = 0; i < 16; i++) { + byte[] tmpR = first32bits; + if(encode) + first32bits = F_Func(first32bits, key[15-i]); + else + first32bits = F_Func(first32bits, key[i]); + + first32bits = XOR_Func(second32bits, first32bits); + second32bits = tmpR; + } + + tmp = Bond_Bits(first32bits, Table_IP.length/2, second32bits, Table_IP.length/2); + + tmp = P_Func(tmp, Table_FP); + return tmp; + } + + private static byte[] F_Func(byte[] a, byte[] b) { + byte[] tmp; + tmp = P_Func(a, Table_Expand); + tmp = XOR_Func(tmp, b); + tmp = S_Func(tmp); + tmp = P_Func(tmp, Table_P); + return tmp; + } + + private static byte[] S_Func(byte[] array) { + array = Sunder_Bytes(array, 6); + byte[] out = new byte[array.length / 2]; + int halfByte = 0; + for (int b = 0; b < array.length; b++) { + byte valByte = array[b]; + int r = 2 * (valByte >> 7 & 0x0001) + (valByte >> 2 & 0x0001); + int c = valByte >> 3 & 0x000F; + int val = Table_SBox[b][r][c]; + if (b % 2 == 0) + halfByte = val; + else + out[b / 2] = (byte) (16 * halfByte + val); + } + return out; + } + + + + // util bit + /////////// + private static byte[] P_Func(byte[] input, int[] table) { + int nrBytes = (table.length - 1) / 8 + 1; + byte[] out = new byte[nrBytes]; + for (int i = 0; i < table.length; i++) { + int val = Get_Bit(input, table[i] - 1); + Set_Bit(out, i, val); + } + return out; + } + + private static byte[] Get_Bits(byte[] input, int pos, int n) { + int num = (n - 1) / 8 + 1; + byte[] out = new byte[num]; + for (int i = 0; i < n; i++) { + int val = Get_Bit(input, pos + i); + Set_Bit(out, i, val); + } + return out; + } + + private static void Set_Bit(byte[] data, int pos, int val) { + int posB = pos / 8; + int posb = pos % 8; + byte tmpB = data[posB]; + tmpB = (byte) (((0xFF7F >> posb) & tmpB) & 0x00FF); + byte newByte = (byte) ((val << (8 - (posb + 1))) | tmpB); + data[posB] = newByte; + } + + private static int Get_Bit(byte[] data, int pos) { + int posB = pos / 8; + int posb = pos % 8; + byte tmpB = data[posB]; + int bit = tmpB >> (8 - (posb + 1)) & 0x0001; + return bit; + } + + private static byte[] LR_Bits(byte[] input, int len, int pas) { + int num = (len - 1) / 8 + 1; + byte[] out = new byte[num]; + for (int i = 0; i < len; i++) { + int val = Get_Bit(input, (i + pas) % len); + Set_Bit(out, i, val); + } + return out; + } + + private static byte[] Bond_Bits(byte[] a, int aLen, byte[] b, int bLen) { + int num = (aLen + bLen - 1) / 8 + 1; + byte[] out = new byte[num]; + int j = 0; + for (int i = 0; i < aLen; i++) { + int val = Get_Bit(a, i); + Set_Bit(out, j, val); + j++; + } + for (int i = 0; i < bLen; i++) { + int val = Get_Bit(b, i); + Set_Bit(out, j, val); + j++; + } + return out; + } + + private static byte[] Sunder_Bytes(byte[] array, int len) { + int num = (8 * array.length - 1) / len + 1; + byte[] out = new byte[num]; + for (int i = 0; i < num; i++) { + for (int j = 0; j < len; j++) { + int val = Get_Bit(array, len * i + j); + Set_Bit(out, 8 * i + j, val); + } + } + return out; + } + + private static byte[] XOR_Func(byte[] a, byte[] b) { + byte[] out = new byte[a.length]; + for (int i = 0; i < a.length; i++) { + out[i] = (byte) (a[i] ^ b[i]); + } + return out; + } +} diff --git a/src/EncryptDecrypt/UtilFile.java b/src/EncryptDecrypt/UtilFile.java new file mode 100644 index 0000000..9beb19d --- /dev/null +++ b/src/EncryptDecrypt/UtilFile.java @@ -0,0 +1,53 @@ +package encryptdecrypt; + +import java.io.*; +import java.net.*; + +public class UtilFile{ + + public byte[] read(String aInputFileName){ + File file = new File(aInputFileName); + byte[] result = new byte[(int)file.length()]; + try { + InputStream input = null; + try { + int totalBytesRead = 0; + input = new BufferedInputStream(new FileInputStream(file)); + while(totalBytesRead < result.length){ + int bytesRemaining = result.length - totalBytesRead; + //input.read() returns -1, 0, or more : + int bytesRead = input.read(result, totalBytesRead, bytesRemaining); + if (bytesRead > 0){ + totalBytesRead = totalBytesRead + bytesRead; + } + } + } + finally { + input.close(); + } + } + catch (FileNotFoundException ex) { + } + catch (IOException ex) { + } + return result; + } + + + public void write(byte[] aInput, String aOutputFileName){ + try { + OutputStream output = null; + try { + output = new BufferedOutputStream(new FileOutputStream(aOutputFileName)); + output.write(aInput); + } + finally { + output.close(); + } + } + catch(FileNotFoundException ex){ + } + catch(IOException ex){ + } + } +}