• 0

need VB codes for aes or des to encryption


Question

6 answers to this question

Recommended Posts

  • 0

Well, I had to do des for homework,and I did it in C#. It's still a .net language and you shouldn't have to much trouble converting it to vb .net. Or at least it will help you understand the principle behind it. It's not the best written code since I rushed it a bit too much. And the functions only work with arrays of 8 bytes. You can get the bytes from any type with a built-in method or you can write your own method. And you can pack more things in 8 bytes and you should be fine as long as you keep them in the right order. Hope I didn't make any mistakes copying it. Good luck :).

using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

public static class myDES
{
private static int[, ,] M = {
			   {{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, 4, 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},
				{ 3, 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, 4}},

			   {{ 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,13, 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, 2, 5, 8},
				{ 2, 1,14, 7, 4,10, 8,13,15,12, 9, 0, 3, 5, 6,11}}
			   };

		private static int[,] PC0 = {
								{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[,] PC0I = {
								 {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[,] PC1 = {
					 {57,49,41,33,25,17, 9},
							 { 1,58,50,42,34,26,18},
					 {10, 2,59,51,43,35,27},
					 {19,11, 3,60,52,44,36},
					 {63,55,47,39,31,23,15},
					 { 7,62,54,46,38,30,22},
					 {14, 6,61,53,45,37,29},
					 {21,13, 5,28,20,12, 4}};

		private static int[,] PC2 ={
					{57,49,41,33,25,17, 9},
							{ 1,58,50,42,34,26,18},
					{10, 2,59,51,43,35,27},
					{19,11, 3,60,52,44,36},
					{63,55,47,39,31,23,15},
					{ 7,62,54,46,38,30,22},
					{14, 6,61,53,45,37,29},
					{21,13, 5,28,20,12, 4}};

		private static int[,] E ={
					  {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[,] 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}
						  };
public static byte[] encrypt(byte[] sequence, long key)
		{
			List<long> round_keys = new List<long>();
			round_keys = calculate_Round_Keys(key);
			long compress_64 = 0L;
			long permuted_64 = 0L;
			byte[] round_call = new byte[8];

			for (int j = 0; j < 8; j++)
			{
				compress_64 = compress_64 << 8;
				compress_64 = sequence[j] | compress_64;
			}
			for (int j = 0; j < 64; j++)
			{
				permuted_64 = permuted_64 << 1;
				permuted_64 = permuted_64 | ((compress_64 >> (64 - PC0[j / 8, j % 8])) & 1L);
			}
			for (int j = 7; j >= 0; j--)
			{
				round_call[j] = (byte)(permuted_64 & 255);
				permuted_64 = permuted_64 >> 8;
			}
			for (int j = 0; j < 16; j++)
			{
				round_call = this.encrypt_round(round_call, round_keys, j);
			}
			byte aux;
			aux = round_call[7];
			round_call[7] = round_call[3];
			round_call[3] = aux;
			aux = round_call[6];
			round_call[6] = round_call[2];
			round_call[2] = aux;
			aux = round_call[5];
			round_call[5] = round_call[1];
			round_call[1] = aux;
			aux = round_call[4];
			round_call[4] = round_call[0];
			round_call[0] = aux;
			compress_64 = 0L;
			for (int j = 0; j < 8; j++)
			{
				compress_64 = compress_64 << 8;
				compress_64 = round_call[j] | compress_64;
			}
			permuted_64 = 0L;
			for (int j = 0; j < 64; j++)
			{
				permuted_64 = permuted_64 << 1;
				permuted_64 = permuted_64 | ((compress_64 >> (64 - PC0I[j / 8, j % 8])) & 1L);
			}
			for (int j = 7; j >= 0; j--)
			{
				round_call[j] = (byte)(permuted_64 & 255);
				permuted_64 = permuted_64 >> 8;
			}
			return round_call;
		}

		public static byte[] decrypt(byte[] sequence, long key)
		{
			List<long> round_keys = new List<long>();
			round_keys = calculate_Round_Keys(key);
			long compress_64 = 0L;
			long permuted_64 = 0L;
			byte[] round_call = new byte[8];

			for (int j = 0; j < 8; j++)
			{
				compress_64 = compress_64 << 8;
				compress_64 = sequence[j] | compress_64;
			}
			for (int j = 0; j < 64; j++)
			{
				permuted_64 = permuted_64 << 1;
				permuted_64 = permuted_64 | ((compress_64 >> (64 - PC0[j / 8, j % 8])) & 1L);
			}
			for (int j = 7; j >= 0; j--)
			{
				round_call[j] = (byte)(permuted_64 & 255);
				permuted_64 = permuted_64 >> 8;
			}
			for (int j = 15; j >= 0; j--)
			{
				round_call = this.encrypt_round(round_call, round_keys, j);
			}
			byte aux;
			aux = round_call[7];
			round_call[7] = round_call[3];
			round_call[3] = aux;
			aux = round_call[6];
			round_call[6] = round_call[2];
			round_call[2] = aux;
			aux = round_call[5];
			round_call[5] = round_call[1];
			round_call[1] = aux;
			aux = round_call[4];
			round_call[4] = round_call[0];
			round_call[0] = aux;
			compress_64 = 0L;
			for (int j = 0; j < 8; j++)
			{
				compress_64 = compress_64 << 8;
				compress_64 = round_call[j] | compress_64;
			}
			permuted_64 = 0L;
			for (int j = 0; j < 64; j++)
			{
				permuted_64 = permuted_64 << 1;
				permuted_64 = permuted_64 | ((compress_64 >> (64 - PC0I[j / 8, j % 8])) & 1L);
			}
			for (int j = 7; j >= 0; j--)
			{
				round_call[j] = (byte)(permuted_64 & 255);
				permuted_64 = permuted_64 >> 8;
			}
			return round_call;
		}

		private static List<long> calculate_Round_Keys(long key)
		{
			List<long> ret = new List<long>();
			long key_64 = key;
			long key_56 = 0L;
			long key_48 = 0L;
			long key_first_28;
			long key_last_28;
			long key_56_aux;
			long mask_28 = 268435455; //2^28 - 1
			long mask_28_last = 72057593769492480;//2^56 - 1 - 2^28

			for (int i = 0; i < 56; i++)
			{
				key_56 = key_56 << 1;
				key_56 = key_56 | ((key_64 >> (64 - this.PC1[i / 8, i % 7])) & 1);
			}
			for (int i = 0; i < 16; i++)
			{
				key_last_28 = key_56 & mask_28;
				key_first_28 = (key_56 & mask_28_last) >> 28;

				for (int j = 0; j <= i; j++)
				{
					if (j == 0 || j == 1 || j == 8 || j == 15)
					{
						key_first_28 = key_first_28 << 1;
						key_first_28 = (key_first_28 & mask_28) | ((key_first_28 & ~mask_28) >> 28);
						key_last_28 = key_last_28 << 1;
						key_last_28 = (key_last_28 & mask_28) | ((key_last_28 & ~mask_28) >> 28);
					}
					else
					{
						key_first_28 = key_first_28 << 2;
						key_first_28 = (key_first_28 & mask_28) | ((key_first_28 & ~mask_28) >> 28);
						key_last_28 = key_last_28 << 2;
						key_last_28 = (key_last_28 & mask_28) | ((key_last_28 & ~mask_28) >> 28);   
					}
				}
				key_48 = 0L;
				key_56_aux = key_last_28 | (key_first_28 << 28);
				for (int j = 0; j < 48; j++)
				{
					key_48 = key_48 << 1;
					key_48 = key_48 | ((key_56_aux >> (56 - PC2[j / 8, j % 6])) & 1);
				}
				ret.Add(key_48);
			}
			return ret;
		}

		private byte[] encrypt_round(byte[] sequence, List<long> round_keys, int round)
		{
			byte[] ret = new byte[8];
			byte group_6;
			long expanded_48 = 0L;
			long substituted_32 = 0L;
			long permuted_32 = 0L;
			long aux_32 = 0L;
			long work_32 = 0L;
			long colums_4;
			long rows_2;

			ret[7] = sequence[3];
			ret[6] = sequence[2];
			ret[5] = sequence[1];
			ret[4] = sequence[0];

			work_32 = sequence[0] | work_32;
			work_32 = work_32 << 8;
			work_32 = sequence[1] | work_32;
			work_32 = work_32 << 8;
			work_32 = sequence[2] | work_32;
			work_32 = work_32 << 8;
			work_32 = sequence[3] | work_32;

			for (int i = 0; i < 48; i++)
			{
				expanded_48 = expanded_48 << 1;
				expanded_48 = expanded_48 | ((work_32 >> (32 - E[i / 8, i % 6])) & 1L);
			}
			expanded_48 = expanded_48 ^ round_keys[round];
			for (int i = 0; i < 8; i++)
			{
				substituted_32 = substituted_32 << 4;
				group_6 = (byte)((expanded_48 >> (48 - i * 6 - 6)) & 63);
				rows_2 = ((32 & group_6) >> 4) | (1 & group_6);
				colums_4 = (30 & group_6) >> 1;
				substituted_32 = substituted_32 | M[i, rows_2, colums_4];
			}

			aux_32 = 0L;
			aux_32 = aux_32 | sequence[4];
			aux_32 = aux_32 << 8;
			aux_32 = aux_32 | sequence[5];
			aux_32 = aux_32 << 8;
			aux_32 = aux_32 | sequence[6];
			aux_32 = aux_32 << 8;
			aux_32 = aux_32 | sequence[7];

			for (int i = 0; i < 32; i++)
			{
				permuted_32 = permuted_32 << 1;
				permuted_32 = permuted_32 | ((substituted_32 >> P[i / 8, i % 4]) & 1L);
			}

			substituted_32 = permuted_32 ^ aux_32;

			ret[3] = (byte)(substituted_32);
			substituted_32 = substituted_32 >> 8;
			ret[2] = (byte)(substituted_32);
			substituted_32 = substituted_32 >> 8;
			ret[1] = (byte)(substituted_32);
			substituted_32 = substituted_32 >> 8;
			ret[0] = (byte)(substituted_32);
			return ret;
		}
}

Edited by Cold Blood
Link to comment
Share on other sites

  • 0

I want to do it with VB.NET to DES or AES or RSA.

like this:

Encrypts the content of a text file and stores the cipher in another text file.

Decrypts a cipher stored in a text file and stores the plain text in a text file.

Your program should read from the user the path of the cipher and the plain text along with the key and either he/she wishes to encrypt or decrypt.

You may use one of the following languages to develop your program: C++, JAVA or VB.NET.

If you got the help of any packages or ready code, pleas mention it and provide all the resource you've used.

You can chose on or more of these algorithms to encrypt or decrypt.

o DES.

o AES.

o RSA.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.