i信息学奥赛

加入QQ群:1025629106,或关注微信公众号:i信息学奥赛,获取更多学习资源。

导航

加密/解密程序

Posted on 2016-12-10 09:47  shnoip  阅读(378)  评论(0编辑  收藏  举报

感谢上外静中任淳同学提供

uses crt;
var t1,t2:text;
    a,i:integer;
    sr1,sr2,sn1,sn2:string;
    s:string;
    f:boolean;
    c:char;

procedure start;
begin
  clrscr;
  f:=true;
  gotoxy(35,8);
  textcolor(lightgreen);
  write('加密/解密 程序');
  delay(500);
  gotoxy(36,10);
  textcolor(blink);
  write('加密');
  textcolor(7);
  write('  ');
  write('解密');
  gotoxy(32,12);
  write('(a:← d:→ 回车:确定)');
  gotoxy(30,14);
  textcolor(lightred);
  write('只能加密或解密.txt文件!!!');
  while true do begin
    if keypressed then begin
      c:=readkey;
      if c=#13 then break;
      if c='a' then begin
        f:=true;
        gotoxy(36,10);
        textcolor(blink);
        write('加密');
        textcolor(7);
        write('  ');
        write('解密');
      end;
      if c='d' then begin
        f:=false;
        gotoxy(36,10);
        textcolor(7);
        write('加密');
        write('  ');
        textcolor(blink);
        write('解密');
      end;
    end;
  end;
end;

procedure enc;
begin
  write('文件路径:');
  readln(sr1);
  write('文件名:');
  readln(sn1);
  write('保存路径:');
  readln(sr2);
  write('保存文件名:');
  readln(sn2);
  assign(t1,sr1+'\'+sn1);
  assign(t2,sr2+'\'+sn2);
  reset(t1);
  rewrite(t2);

  randomize;
  a:=random(75)+20;
  writeln(t2,-a);

  while not eof(t1) do begin
    readln(t1,s);
    for i:=1 to length(s) do s[i]:=chr(ord(s[i])+a);
    writeln(t2,s);
  end;

  close(t1);
  close(t2);
  clrscr;
  cursoroff;
  sound(100);
  gotoxy(39,12);
  textcolor(lightred);
  writeln('加密成功!!!');
  delay(500);
  nosound;
  while not keypressed do cursoroff;
end;

procedure bre;
begin
  write('文件路径:');
  readln(sr1);
  write('文件名:');
  readln(sn1);
  write('保存路径:');
  readln(sr2);
  write('保存文件名:');
  readln(sn2);
  assign(t1,sr1+'\'+sn1);
  assign(t2,sr2+'\'+sn2);
  reset(t1);
  rewrite(t2);

  readln(t1,a);
  while not eof(t1) do begin
    readln(t1,s);
    for i:=1 to length(s) do s[i]:=chr(ord(s[i])+a);
    writeln(t2,s);
  end;

  close(t1);
  close(t2);
  clrscr;
  cursoroff;
  sound(100);
  gotoxy(39,12);
  textcolor(lightred);
  writeln('解密成功!!!');
  delay(500);
  nosound;
  while not keypressed do cursoroff;
end;

begin
  cursoroff;
  start;
  textcolor(7);
  textbackground(black);
  clrscr;
  cursoron;
  if f then enc else bre;
end.