实现“广义凯撒密码”的C语言程序如下所示:
#include <stdio.h> 
#include <conio.h> 
#include <string.h> 
#include <malloc.h> 
#include <stdlib.h> 

int gcd(int a, int b) /*辗转相除法求a,b的最大公因数*/ 

int k = 0; 

do 

k = a%b; 
a = b; 
b = k; 
}while(k!=0); 
return a; 


int Ni(int a, int b) /*求a相对于b的逆*/ 

int i = 0; 
while(a*(++i)%b!=1); 
return i; 


void Jiami() /*仿射密码加密*/ 

char c[100]; 
int length, i=0, ka=0, kb=0; 
system("cls"); 

printf("********Jiami********\nPlease input primal sentence: "); 
gets(c); 
length = strlen(c); 
printf("Input the key(2 numbers): "); 
scanf("%d,%d", &ka, &kb); 
getchar(); 
if(gcd(ka,26)!=1) 

printf("The value of the key is error!\nPress any key to return..."); 
return; 


for(i=0; i<length; i++) 

if(c[i]>96&&c[i]<123) 
c[i] = (ka*(c[i]-97)+kb)%26+65; 
else if(c[i]>64&&c[i]<91) 
c[i] = (ka*(c[i]-65)+kb)%26+65; 

printf("Result is: %s\n", c); 
printf("Press any key to return..."); 
getch(); 


void Jiemi()
{
char c[100]; 
int length, i=0, ka=0, kb=0,tmp; 
system("cls"); 

printf("********Jiemi********\nPlease input ciphertext: "); 
gets(c); 
length = strlen(c); 
printf("Input the key(2 numbers): "); 
scanf("%d,%d", &ka, &kb); 
getchar(); 
if(gcd(ka,26)!=1) 

printf("The value of the key is error!\nPress any key to return..."); 
return; 


for(i=0; i<length; i++) 

if(c[i]>64&&c[i]<91) 

tmp = Ni(ka,26)*((c[i]-65)-kb); 
if(tmp<0) 
c[i] = tmp%26+26+97; 
else 
c[i] = tmp%26+97; 

}
printf("\nAfter translated the sentence,we can see the primal sentence as follow:\n%s\n", c); 
printf("Press any key to return..."); 
getch(); 


void main() 

char i = '0'; 
system("cls"); 

while(i!='3') 

system("cls"); 
printf("********Press 1~3 to choose:********\n"); 
printf("1.Jiami\n2.Jiemi\n3.Exit \n"); 
i = getch(); 
if(i=='1') 
Jiami(); 
else if(i=='2') 
Jiemi();
else if(i=='3')
break; 

}  
因为刚刚学习JAVA,不知道如何把该程序改写成JAVA程序?请各位老大赐教。