locked
Convert code C to C# RRS feed

  • Question

  • Help me convert this code is in C language to C #

    #include<stdio.h> 
    #include<conio.h> 
    #include<stdlib.h> 
    #include<math.h> 
    #include<string.h> 

    long int p,q,n,t,flag,e[100],d[100],temp[100],j,m[100],en[100],i; 
    char msg[100]; 
    int prime(long int); 
    void ce(); 
    long int cd(long int); 
    void encrypt(); 
    void decrypt(); 
    void main() 

    clrscr(); 
    printf("\nENTER FIRST PRIME NUMBER\n"); 
    scanf("%d",&p); 
    flag=prime(p); 
    if(flag==0) 

        printf("\nWRONG INPUT\n"); 
        getch(); 
        exit(1); 

    printf("\nENTER ANOTHER PRIME NUMBER\n"); 
    scanf("%d",&q); 
    flag=prime(q); 
    if(flag==0||p==q) 

        printf("\nWRONG INPUT\n"); 
        getch(); 
        exit(1); 

    printf("\nENTER MESSAGE\n"); 
    fflush(stdin); 
    scanf("%s",msg); 
    for(i=0;msg[i]!=NULL;i++) 
    m[i]=msg[i]; 
    n=p*q; 
    t=(p-1)*(q-1); 
    ce(); 
    printf("\nPOSSIBLE VALUES OF e AND d ARE\n"); 
    for(i=0;i<j-1;i++) 
    printf("\n%ld\t%ld",e[i],d[i]); 
    encrypt(); 
    decrypt(); 
    getch(); 

    int prime(long int pr) 

    int i; 
    j=sqrt(pr); 
    for(i=2;i<=j;i++) 

        if(pr%i==0) 
        return 0; 

    return 1; 

    void ce() 

    int k; 
    k=0; 
    for(i=2;i<t;i++) 

        if(t%i==0) 
        continue; 
        flag=prime(i); 
        if(flag==1&&i!=p&&i!=q) 
        { 
            e[k]=i; 
            flag=cd(e[k]); 
            if(flag>0) 
            { 
                d[k]=flag; 
                k++; 
            } 
            if(k==99) 
            break; 
        } 


    long int cd(long int x) 

    long int k=1; 
    while(1) 

        k=k+t; 
        if(k%x==0) 
        return(k/x); 


    void encrypt() 

    long int pt,ct,key=e[0],k,len; 
    i=0; 
    len=strlen(msg); 
    while(i!=len) 

        pt=m[i]; 
        pt=pt-96; 
        k=1; 
        for(j=0;j<key;j++) 
        { 
            k=k*pt; 
            k=k%n; 
        } 
        temp[i]=k; 
        ct=k+96; 
        en[i]=ct; 
        i++; 

    en[i]=-1; 
    printf("\nTHE ENCRYPTED MESSAGE IS\n"); 
    for(i=0;en[i]!=-1;i++) 
    printf("%c",en[i]); 

    void decrypt() 

    long int pt,ct,key=d[0],k; 
    i=0; 
    while(en[i]!=-1) 

        ct=temp[i]; 
        k=1; 
        for(j=0;j<key;j++) 
        { 
            k=k*ct; 
            k=k%n; 
        } 
        pt=k+96; 
        m[i]=pt; 
        i++; 

    m[i]=-1; 
    printf("\nTHE DECRYPTED MESSAGE IS\n"); 
    for(i=0;m[i]!=-1;i++) 
    printf("%c",m[i]); 
    }

    This code is an implementation of  RSA encryption.

    Thanks in advance

    Friday, October 7, 2016 10:32 PM

Answers

  • Help me convert this code is in C language to C #

    long int p,q,n,t,flag,e[100],d[100],temp[100],j,m[100],en[100],i;

    void main() 

    clrscr(); 

    ...


    int prime(long int pr) 

    int i; 
    j=sqrt(pr); 

    ...

    }

    This code is an implementation of  RSA encryption.


    Is there any good reason why you are trying to convert this C program?

    Note that it doesn't appear to have been written for Visual C/C++, as clrscr()
    has never been available in VC++. It is mostly found in the former
    Borland/Turbo C++ compilers. Also the sqrt() function in VC++ expects a float
    or double as an argument, not a long, and it returns a double not a long.

    For C# you should be looking at what is already available for doing RSA encryption.

    For example:

    RSA Class
    https://msdn.microsoft.com/en-us/library/system.security.cryptography.rsa(v=vs.110).aspx

    - Wayne

    Saturday, October 8, 2016 4:38 AM
  • Hi Vando1258,

    Thank you for posting here.

    For your question, you could use tools to convert C++ TO C#.

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.

    I hope this would be helpful to you.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 21, 2016 6:57 AM

All replies

  • Help me convert this code is in C language to C #

    long int p,q,n,t,flag,e[100],d[100],temp[100],j,m[100],en[100],i;

    void main() 

    clrscr(); 

    ...


    int prime(long int pr) 

    int i; 
    j=sqrt(pr); 

    ...

    }

    This code is an implementation of  RSA encryption.


    Is there any good reason why you are trying to convert this C program?

    Note that it doesn't appear to have been written for Visual C/C++, as clrscr()
    has never been available in VC++. It is mostly found in the former
    Borland/Turbo C++ compilers. Also the sqrt() function in VC++ expects a float
    or double as an argument, not a long, and it returns a double not a long.

    For C# you should be looking at what is already available for doing RSA encryption.

    For example:

    RSA Class
    https://msdn.microsoft.com/en-us/library/system.security.cryptography.rsa(v=vs.110).aspx

    - Wayne

    Saturday, October 8, 2016 4:38 AM
  • As has already been reported to clrscr, some statements have no equivalent in C#.
    You can also remove fflush (stdin);

    If you create a C# application in console mode:

    printf may be replaced by Console.WriteLine

    and scanf by Console.ReadLine

    long int by long

    len = strlen (msg); by len = msg.Length;

    etc.
    Saturday, October 8, 2016 9:36 AM
  • Hi Vando1258,

    Thank you for posting here.

    For your question, you could use tools to convert C++ TO C#.

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.

    I hope this would be helpful to you.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 21, 2016 6:57 AM