int const N = 10
// 将方阵a[N][N]第row行循环左移m位
void RowLeftn(char a[][N],int n,int row,int m) {
int i,j,t
if(row < 0 || row > n - 1) return
for(i = 0 i < m ++i) {
t = a[row][0]
for(j = 0 j < n - 1 ++j)
a[row][j] = a[row][j + 1]
a[row][n - 1] = t
}
}
// 将方阵a[N][N]第col列循环上移m位
void ColUpn(char a[][N],int n,int col,int m) {
int i,j,t
if(col < 0 || col > n - 1) return
for(i = 0 i < m ++i) {
t = a[0][col]
for(j = 0 j < n - 1 ++j)
a[j][col] = a[j + 1][col]
a[n - 1][col] = t
}
}
int main() {
char txt[N][N]
int a[N],b[N],i,j,n
while(scanf("%d",&n) == 1 && n > 0) {
fflush(stdin)
for(i = 0 i < n ++i) {
for(j = 0 j < n ++j)
scanf("%c",&txt[i][j])
}
for(i = 0 i < n ++i) {
for(j = 0 j < n ++j)
printf("%c",txt[i][j])
printf("\n")
}
for(i = 0 i < n ++i) scanf("%d",&a[i])
for(i = 0 i < n ++i) scanf("%d",&b[i])
for(i = 0 i < n ++i) RowLeftn(txt,n,i,a[i])
for(i = 0 i < n ++i) ColUpn(txt,n,i,b[i])
for(i = 0 i < n ++i) {
for(j = 0 j < n ++j)
printf("%c",txt[i][j])
}
printf("\n")
}
return 0
}
看你催就仓促写了个,自我感觉写的不是很好,但是能用了。数据只能是大写字母组成的字符串。加密的时候,输入Y,然后输入要加密的文本(大写字母)
解密的时候,输入N,然后输入一个整数n表示密文的个数,然后n个整数表示加密时候得到的密文。
/*RSA algorithm */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MM 7081
#define KK 1789
#define PHIM 6912
#define PP 85
typedef char strtype[10000]
int len
long nume[10000]
int change[126]
char antichange[37]
void initialize()
{ int i
char c
for (i = 11, c = 'A'c <= 'Z'c ++, i ++)
{ change[c] = i
antichange[i] = c
}
}
void changetonum(strtype str)
{ int l = strlen(str), i
len = 0
memset(nume, 0, sizeof(nume))
for (i = 0i <li ++)
{ nume[len] = nume[len] * 100 + change[str[i]]
if (i % 2 == 1) len ++
}
if (i % 2 != 0) len ++
}
long binamod(long numb, long k)
{ if (k == 0) return 1
long curr = binamod (numb, k / 2)
if (k % 2 == 0)
return curr * curr % MM
else return (curr * curr) % MM * numb % MM
}
long encode(long numb)
{ return binamod(numb, KK)
}
long decode(long numb)
{ return binamod(numb, PP)
}
main()
{ strtype str
int i, a1, a2
long curr
initialize()
puts("Input 'Y' if encoding, otherwise input 'N':")
gets(str)
if (str[0] == 'Y')
{ gets(str)
changetonum(str)
printf("encoded: ")
for (i = 0i <leni ++)
{ if (i) putchar('-')
printf(" %ld ", encode(nume[i]))
}
putchar('\n')
}
else
{ scanf("%d", &len)
for (i = 0i <leni ++)
{ scanf("%ld", &curr)
curr = decode(curr)
a1 = curr / 100
a2 = curr % 100
printf("decoded: ")
if (a1 != 0) putchar(antichange[a1])
if (a2 != 0) putchar(antichange[a2])
}
putchar('\n')
}
putchar('\n')
system("PAUSE")
return 0
}
测试:
输入:
Y
FERMAT
输出:
encoded: 5192 - 2604 - 4222
输入
N
3 5192 2604 4222
输出
decoded: FERMAT
这里使用的是按位加密,按ASCII码进行加密的算法自己写个,很容易的。#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
void
dofile(char
*in_fname,char
*pwd,char
*out_fname)/*对文件进行加密的具体函数*/
void
usage(char
*name)
void
main(int
argc,char
*argv[])/*定义main()函数的命令行参数*/
{
char
in_fname[30]/*用户输入的要加密的文件名*/
char
out_fname[30]
char
pwd[10]/*用来保存密码*/
if(argc!=4)
{/*容错处理*/
usage(argv[0])
printf("\nIn-fname:\n")
gets(in_fname)/*得到要加密的文件名*/
while(*in_fname==NULL)
{
printf("\nIn-fname:\n")
gets(in_fname)
}
printf("Password
6-8:\n")
gets(pwd)/*得到密码*/
while(*pwd==NULL
||
strlen(pwd)>8
||
strlen(pwd)<6)
{
printf("Password
6-8:\n")
gets(pwd)
}
printf("Out-file:\n")
gets(out_fname)/*得到加密后你要的文件名*/
while(*in_fname==NULL)
{
printf("Out-file:\n")
gets(out_fname)
}
while(!strcmp(in_fname,out_fname))
{
printf("文件名不能和源文件相同\n")
printf("Out-file:\n")
gets(out_fname)
}
dofile(in_fname,pwd,out_fname)
printf("加密成功,解密请再次运行程序\n")
}
else
{/*如果命令行参数正确,便直接运行程序*/
strcpy(in_fname,argv[1])
strcpy(pwd,argv[2])
strcpy(out_fname,argv[3])
while(*pwd==NULL
||
strlen(pwd)>8
||
strlen(pwd)<6)
{
printf("Password
faied!\n")
printf("Password
6-8:\n")
gets(pwd)
}
while(!strcmp(in_fname,out_fname))
{
printf("文件名不能和源文件相同\n")
printf("Out-file:\n")
gets(out_fname)
while(*in_fname==NULL)
{
printf("Out-file:\n")
gets(out_fname)
}
}
dofile(in_fname,pwd,out_fname)
printf("加密成功,解密请再次运行程序\n")
}
}
/*加密子函数开始*/
void
dofile(char
*in_fname,char
*pwd,char
*out_file)
{
FILE
*fp1,*fp2
register
char
ch
int
j=0
int
j0=strlen(pwd)
fp1=fopen(in_fname,"rb")
if(fp1==NULL)
{
printf("cannot
open
in-file.\n")
exit(1)/*如果不能打开要加密的文件,便退出程序*/
}
fp2=fopen(out_file,"wb")
if(fp2==NULL)
{
printf("cannot
open
or
create
out-file.\n")
exit(1)/*如果不能建立加密后的文件,便退出*/
}
/*加密算法开始*/
while(j0>=0)
{
ch=fgetc(fp1)
while(!feof(fp1))
{
fputc(ch^pwd[j>=j0?j=0:j++],fp2)/*异或后写入fp2文件*/
ch=fgetc(fp1)
}
j0--
}
fclose(fp1)/*关闭源文件*/
fclose(fp2)/*关闭目标文件*/
}
void
usage(char
*name)
{
printf("\t=======================File
encryption======================\n")
printf("\tusage:
%s
In-fname
password
out_fname\n",name)
printf("\tExample:
%s
file1.txt
12345678
file2.txt\n",name)
}