1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "pio/inc/pio.h"
static void purge(FILE *fp)
{
int c;
while ((c = fgetc(fp)) != '\n' && c != EOF)
{
}
}
static void clean (char *s, FILE *fp)
{
/* search ... */
char *p = strchr (s, '\n'); /* <string.h> */
if (p != NULL)
{
/* ... and kill */
*p = 0;
}
else
{
purge (fp);
}
}
static int menu(void)
{
int choice;
printf("**********************\n");
printf("* cryptage menu *\n");
printf("**********************\n\n");
printf("1. enter clear message\n");
printf("2. read clear message \n");
printf("3. read encrypted message \n");
printf("9. quit\n");
printf("your choice : \n");
{
char s[4];
fgets(s, sizeof s, stdin);
clean(s, stdin);
choice = strtol(s, NULL, 10);
}
return choice;
}
/*
bit 3-0 : encryption-in
bit 7-4 : encryption-out
*/
unsigned encrypt_quartet(unsigned port, unsigned in)
{
unsigned char data;
/* send quartet to bits 3 to 0 */
pio_outb(port, in);
/* wait 1 ms */
Sleep (1);
/* receive encoded MSB from bits 7 to 4 */
pio_inb(port, &data);
/* store encoded MSB */
return (data >> 4) & 0x0F;
}
unsigned encrypt_byte(unsigned port, unsigned in)
{
unsigned out = 0;
/* encode MSB to bits 3 to 0 */
out |= (encrypt_quartet(port, (in >> 4) & 0xF) << 4);
/* send LSB to bits 3 to 0 */
out |= (encrypt_quartet(port, (in >> 0) & 0xF) << 0);
return out;
}
void encrypt_string (unsigned port, char const *s_in, char *s_out, size_t size)
{
size_t i;
for (i = 0; i < size - 1; i++)
{
s_out[i] = encrypt_byte(port, s_in[i]);
}
s_out[i] = 0;
}
int main()
{
/* addresse de base du port parallele */
unsigned port = 0x378;
int end = 0;
char msg[16 + 1] = "";
do
{
int choicemenu = menu();
switch (choicemenu)
{
case 1:
printf("enter message to crypt (max %u characters)\n", (unsigned) (sizeof msg - 1));
fgets(msg, sizeof msg, stdin);
clean(msg, stdin);
break;
case 2:
printf("message to encrypt '%s'\n", msg);
break;
case 3:
/* ici je dois envoyer msg vers le port parallele dans les
broches 2 à 5 et recuperer des données qui viennent du circuit
imprimé dans les broches 6 à 9*/
/* hardware encryption */
{
char cryptmsg[sizeof msg];
encrypt_string (port, msg, cryptmsg, sizeof cryptmsg);
printf ("encrypted msg: '%s'\n", cryptmsg);
}
break;
case 9:
end = 1;
break;
default:
printf("bad choice or illegal operation\n");
getchar();
}
}
while (!end)
;
return 0;
} |
Partager