Hi everyone,I'm tryind to do something about sound on linux.I basically record some sound and try to encyrpt it using algorithm.However,I encountered some sort of pointer problem.Both RSA algorithm and recording&listenin sound works properly.
Here is my code:
/*
* rsa.c
*
* Created on: 24 May 2012
* Author: tugce
*/
#include <stdio.h>
#include <ncurses.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <math.h>
#include<fcntl.h>
#include<stdlib.h>
#include<stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <stdio.h>
#include <linux/soundcard.h>
#define LENGTH 3 /* how many seconds of speech to store */
#define RATE 8000 /* the sampling rate */
#define FILE_INPUT "/dev/dsp" /* Path to the sound card. */
#define FILE_OUTPUT "/dev/dsp"
//Her is gcd function
int gcd(int a,int b)
{
while(a!=b){
if(a>b)
a-=b;
else
b-=a;
}
return a;
}
//This is called Extended Euclid’s Algorithm to find d.
int findD(int e,int n)
{
int f=e;
int d=n;
int x1, x2, x3, y1, y2, y3;
x1 = 1; x2 = 0; x3 = f; //p
y1 = 0; y2 = 1; y3 = d; //d
int q = 0, i = 1;
int t1 = 0, t2 = 0, t3 = 0;
do
{
if (i == 1)
{
q = x3 / y3;
t1 = x1 - (q * y1);
t2 = x2 - (q * y2);
t3 = x3 - (q * y3);
}
else
{
x1 = y1; x2 = y2; x3 = y3;
y1 = t1; y2 = t2; y3 = t3;
q = x3 / y3;
t1 = x1 - (q * y1);
t2 = x2 - (q * y2);
t3 = x3 - (q * y3);
}
i++;
if (y3 == 0)
{
break;
}
} while (y3 != 1);
if (y3 == 0)
{
//printf("Sayinin tersi yoktur!!!!");
}
else
{
// printf("\nSayinin tersi : %d" , y2);
}
if(y2<=0)
{
y2=e+y2;
}
return y2;
}
//Instead of using pow function,I have choose to write square and multiply method which is faster and
//more suitable for big integers.Because we have no such a change to find 104^30 such like that
//Here computes pow(a,b)%n
int squareMul(int a,int b,int n)
{
int y = 1;
/* Compute pow(a, b) % n using the binary square and multiply method. */
while (b != 0)
{
/* For each 1 in b, accumulate y. */
if (b & 1)
{
y = (y * a) % n;
}
/* Square a for each bit in b. */
a = (a * a) % n;
/* Prepare for the next bit in b. */
b = b >> 1;
}
return y;
}
//Encyrption function
//Assume our plain-text is M
int *encyrpt(int text[],int e,int n)
{
int t=0;
int *s=(int *)malloc(24000);
for(t=0;t<sizeof(text);t++)
{
int gec=(int)text[t];
//Here computes E(M)=M^e mod n;
s[t]=squareMul(gec,e,n);
}
return s;
}
//Here is decyrption
//Assume our chipher-text is C
int *decyrpt(int enc[],int d,int e,int n)
{
int i=0;
int *s=(int *)malloc(24000);
for(i=0;i<sizeof(enc);i++)
{
int gelenEnc=(int)enc[i];
//Here computes D(C)=C^d mod n;
s[i]=squareMul(gelenEnc,d,n);
}
return s;
}
//Here is totient function to find prime to m.
int totient(int m)
{
int i;
int ph=1;
for(i=2;i<m;i++){
if(gcd(i,m)==1)
{
ph=i;
break;
}
}
return ph;
}
int main(){
int sound_device;
/* open sound device */
sound_device = open("/dev/dsp", O_RDWR);
if (sound_device < 0) {
perror("Open sound card failed");
exit(1);
}
//Here are some variables that I used for RSA ALGORİTHM
int* ascii;
int* enc;
int* dec;
int p,q;
int k=0;
int n;
int e;
int c;
int phi;
int d;
unsigned char buf[LENGTH*RATE];
//Here enter 2 relatively prime number
//I have chose the p=73 and q=151
p=73;
q=151;
printf("\n\ p :%d and q :%d \t \n",p,q);
//Here computes n
n = p*q;
//Here computes phi func simply
phi=(p-1)*(q-1);
printf("\n\ n :\t= %d \n",n);
printf("\n\ Phi :\t= %d \n",phi);
//Here Euilers Totient function.It finds a number 'e' which is relatively prime with phi.
e=totient(phi);
//Here computes d,which is multiplicative inverse of e modula phi.
d=findD(phi,e);
printf("\n\ e :\t= %d \n",e);
printf("\n\ d :\t= %d which is multiplicative inverse of e modula phi \n",d);
printf("Say something:\n");
read(sound_device, buf, sizeof(buf)); /* record some sound */
//Here is the ascii values for plainText.I have created new array in order to store plainText's ascii for simplicty
ascii=(int *)malloc(24000*sizeof(int));
enc=(int *)malloc(24000*sizeof(int));
dec=(int *)malloc(24000*sizeof(int));
//Here ascii stores plaintText's ascii number.
for(c=0;c<LENGTH*RATE;c++)
{
int k=(int)buf[c];
ascii[c]=k;
printf("\n\t Ascii's of %c \t= %d \n",buf[c],ascii[c]);
}
//Here is encyription
enc=encyrpt(ascii,e,n);
//Here is decyription
dec=decyrpt(enc,d,e,n);
write(sound_device, dec, LENGTH*RATE);
ioctl(sound_device, SOUND_PCM_SYNC, 0);
for(k=0;k<sizeof(dec);k++)
{
printf("%c",dec[k]);
}
}
Basically,Rsa works with ascii array which includes char's of my word and encyrpt/decyrpt asciis with rsa.Here is my RSA algorithm which works properly separate with sound.
/*
* rsa.c
*
* Created on: 24 May 2012
* Author: tugce
*/
#include <stdio.h>
#include <ncurses.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
//Her is gcd function
int gcd(int a,int b)
{
while(a!=b){
if(a>b)
a-=b;
else
b-=a;
}
return a;
}
//This is called Extended Euclid’s Algorithm to find d.
int findD(int e,int n)
{
int f=e;
int d=n;
int x1, x2, x3, y1, y2, y3;
x1 = 1; x2 = 0; x3 = f; //p
y1 = 0; y2 = 1; y3 = d; //d
int q = 0, i = 1;
int t1 = 0, t2 = 0, t3 = 0;
do
{
if (i == 1)
{
q = x3 / y3;
t1 = x1 - (q * y1);
t2 = x2 - (q * y2);
t3 = x3 - (q * y3);
}
else
{
x1 = y1; x2 = y2; x3 = y3;
y1 = t1; y2 = t2; y3 = t3;
q = x3 / y3;
t1 = x1 - (q * y1);
t2 = x2 - (q * y2);
t3 = x3 - (q * y3);
}
i++;
if (y3 == 0)
{
break;
}
} while (y3 != 1);
if (y3 == 0)
{
//printf("Sayinin tersi yoktur!!!!");
}
else
{
// printf("\nSayinin tersi : %d" , y2);
}
if(y2<=0)
{
y2=e+y2;
}
return y2;
}
//Instead of using pow function,I have choose to write square and multiply method which is faster and
//more suitable for big integers.Because we have no such a change to find 104^30 such like that
//Here computes pow(a,b)%n
int squareMul(int a,int b,int n)
{
int y = 1;
/* Compute pow(a, b) % n using the binary square and multiply method. */
while (b != 0)
{
/* For each 1 in b, accumulate y. */
if (b & 1)
{
y = (y * a) % n;
}
/* Square a for each bit in b. */
a = (a * a) % n;
/* Prepare for the next bit in b. */
b = b >> 1;
}
return y;
}
//Encyrption function
//Assume our plain-text is M
int *encyrpt(int text[],int e,int n)
{
int t=0;
int *s=(int *)malloc(100);
for(t=0;t<sizeof(text);t++)
{
int gec=(int)text[t];
//Here computes E(M)=M^e mod n;
s[t]=squareMul(gec,e,n);
}
return s;
}
//Here is decyrption
//Assume our chipher-text is C
int *decyrpt(int enc[],int d,int e,int n)
{
int i=0;
int *s=(int *)malloc(100);
for(i=0;i<sizeof(enc);i++)
{
int gelenEnc=(int)enc[i];
//Here computes D(C)=C^d mod n;
s[i]=squareMul(gelenEnc,d,n);
}
return s;
}
//Here is totient function to find prime to m.
int totient(int m)
{
int i;
int ph=1;
for(i=2;i<m;i++){
if(gcd(i,m)==1)
{
ph=i;
break;
}
}
return ph;
}
int main(){
//Here are some variables that I used for RSA ALGORİTHM
//str is our plain-text
char *plainText;
int *ascii;
int *enc;
int *dec;
int p,q;
int k=0;
int n;
int e;
int c;
int phi;
int d;
plainText="Merhaba";
//Here enter 2 relatively prime number
//I have chose the p=73 and q=151
p=73;
q=151;
printf("\n\ p :%d and q :%d \t \n",p,q);
//Here computes n
n = p*q;
//Here computes phi func simply
phi=(p-1)*(q-1);
printf("\n\ n :\t= %d \n",n);
printf("\n\ Phi :\t= %d \n",phi);
//Here Euilers Totient function.It finds a number 'e' which is relatively prime with phi.
e=totient(phi);
//Here computes d,which is multiplicative inverse of e modula phi.
d=findD(phi,e);
printf("\n\ e :\t= %d \n",e);
printf("\n\ d :\t= %d which is multiplicative inverse of e modula phi \n",d);
//Here is the ascii values for plainText.I have created new array in order to store plainText's ascii for simplicty
ascii=(int *)malloc(sizeof(int)*sizeof(plainText)/sizeof(char));
//Here ascii stores plaintText's ascii number.
for(c=0;c<7;c++)
{
int k=(int)plainText[c];
ascii[c]=k;
printf("\n\t Ascii's of %c \t= %d \n",plainText[c],ascii[c]);
}
//Here is encyription
enc=encyrpt(ascii,e,n);
/*for(k=0;k<sizeof(enc);k++)
{
printf(" Encyprted : %d\n ",*(enc+k));
}*/
//Here is decyription
dec=decyrpt(enc,d,e,n);
for(k=0;k<sizeof(dec);k++)
{
printf("%c",dec[k]);
}
}
Here is writing&listening from sound card.It'S also works properly.
#include <sys/ioctl.h>
#include <stdlib.h>
#include <stdio.h>
#include <linux/soundcard.h>
#define LENGTH 3 /* how many seconds of speech to store */
#define RATE 8000 /* the sampling rate */
/* this buffer holds the digitized audio */
unsigned char buf[LENGTH*RATE];
#define FILE_INPUT "/dev/dsp" /* Path to the sound card. */
#define FILE_OUTPUT "/dev/dsp"
int main() {
int sound_device;
/* open sound device */
sound_device = open("/dev/dsp", O_RDWR);
if (sound_device < 0) {
perror("Open sound card failed");
exit(1);
}
/*Read samples from the soundcard into buffer. (Blocking function call)*/
//read(sound_device, buffer, LENGTH);
while(1)
{ printf("Say something:\n");
read(sound_device, buf, sizeof(buf)); /* record some sound */
printf("You said:\n");
write(sound_device, buf, sizeof(buf));
ioctl(sound_device, SOUND_PCM_SYNC, 0);
}
return 1;
}
I'm new with C language,so I've little idea how to deal with arrays&sizes etc.Therefore I didnt combined both RSA and sound algortihms in 1 one code.Any help would be appriciated.
Thanks