
        character code(20000)*1,text(20000)*1,dig(700)*2,key(700)*2
        character alph(26)*1,key1(5,5)*1
        integer count(26,26)

        open(8,file="Playfair.data")
        n=0
  1     read(8,1000,end=2)(code(60*n+i),i=1,60)
1000    format(60a1)
        n=n+1
        go to 1
  2     close(8)
        write(*,*)n+1,' lines of code read.'
        do 3 i=60,1,-1
          if(code(i).eq." ")j=i-1
  3     continue
        nc = 55*n+j
        write(*,*)'The code has ',nc,' characters.'
        write(*,1000)(code(i),i=1,nc)

        do 4 i=1,nc,2
          j1 = ichar(code(i))-64
          j2 = ichar(code(i+1))-64
          count(j1,j2)=count(j1,j2)+1
  4     continue

        do 5 i=1,26
          alph(i)=char(64+i)
  5     continue

        write(*,1002)(alph(ic),ic=1,9),(alph(ic),ic=11,26)
1002    format(1x,25(2x,1a))
        do 20 ir=1,9
        write(*,1001)alph(ir),(count(ir,ic),ic=1,9)
     &            ,(count(ir,ic),ic=11,26)
 20     continue
        do 21 ir=11,26
        write(*,1001)alph(ir),(count(ir,ic),ic=1,9)
     &            ,(count(ir,ic),ic=11,26)
 21     continue
1001    format(1a,25(1x,i2))

  8     write(*,*)'Hit return to read key square guess.'
        read(*,*)

        write(*,*)'The key is'
        open(8,file='Playfair.key',form='formatted')
        do 45 i=1,5
          read(8,2000)(key1(i,j),j=1,5)
2000     format(5a1)
          write(*,2000)(key1(i,j),j=1,5)
 45     continue

        close(8)

        nk=0
        do 70 i=1,5
          do 70 j=1,5
            do 71 i1=1,4
              i2 = i+i1
              if(i2.gt.5)i2=i2-5
              do 71 j1=1,4
              j2=j+j1
              if(j2.gt.5)j2=j2-5
              if(key1(i,j).ne.'.'.and.key1(i2,j2).ne.'.'
     &          .and.(key1(i2,j).ne.'.'.or.key1(i,j2).ne.'.'))then
                 nk=nk+1
                 dig(nk)=key1(i,j)//key1(i2,j2)
                 key(nk)=char(ichar(key1(i,j2))+32)
     &                   //char(ichar(key1(i2,j))+32)
                 if(key(nk)(1:1).eq.'N')key(nk)='.'//key(nk)(2:2)
                 if(key(nk)(2:2).eq.'N')key(nk)=key(nk)(1:1)//'.'
              end if
 71         continue
            do 72 j1=1,4
              j2=j+j1
              if(j2.gt.5)j2=j2-5
              if(key1(i,j).ne.'.'.and.key1(i,j2).ne.'.')then
                j3=j-1
                if(j3.eq.0)j3=5
                j4=j2-1
                if(j4.eq.0)j4=5
                if(key1(i,j3).ne.'.'.or.key1(i,j4).ne.'.')then
                  nk=nk+1
                  dig(nk)=key1(i,j)//key1(i,j2)
                  key(nk)=char(ichar(key1(i,j3))+32)
     &                   //char(ichar(key1(i,j4))+32)
                 if(key(nk)(1:1).eq.'N')key(nk)='.'//key(nk)(2:2)
                 if(key(nk)(2:2).eq.'N')key(nk)=key(nk)(1:1)//'.'
                end if
              end if
 72        continue

            do 73 i1=1,4
              i2=i+i1
              if(i2.gt.5)i2=i2-5
              if(key1(i,j).ne.'.'.and.key1(i2,j).ne.'.')then
                i3=i-1
                if(i3.eq.0)i3=5
                i4=i2-1
                if(i4.eq.0)i4=5
                if(key1(i3,j).ne.'.'.or.key1(i4,j).ne.'.')then
                  nk=nk+1
                  dig(nk)=key1(i,j)//key1(i2,j)
                  key(nk)=char(ichar(key1(i3,j))+32)
     &                   //char(ichar(key1(i4,j))+32)
                 if(key(nk)(1:1).eq.'N')key(nk)='.'//key(nk)(2:2)
                 if(key(nk)(2:2).eq.'N')key(nk)=key(nk)(1:1)//'.'
                end if
              end if
 73        continue

 70    continue

c             do 74 i=1,nk
c             write(*,*)dig(i),'  ',key(i)
c74        continue             

              
        do 10 i=1,nc,2
          text(i)=code(i)
          text(i+1)=code(i+1)
          do 11 j=1,nk
            if(code(i).eq.dig(j)(1:1).and.
     &         code(i+1).eq.dig(j)(2:2))then
              text(i)=key(j)(1:1)
              text(i+1)=key(j)(2:2)
            end if
  11      continue
 10     continue

        write(*,*)'Latest guess is:'
        write(*,1000)(text(i),i=1,nc)


        go to 8
        end
