-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFortan.f90
More file actions
69 lines (56 loc) · 1.67 KB
/
Fortan.f90
File metadata and controls
69 lines (56 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
! Brendon Kupsch
! Fortran Ceaser Cipher
program CodeCipher
implicit none
integer :: shift_val = 10
character(3) :: input_str = 'HAL'
character(3) :: original = ''
character(3) :: ciphered = ''
original = input_str
write(*, '(2a)') 'Original string = ', trim(original)
call encrypt(input_str, shift_val)
write(*, '(2a)') 'Encrypt: ', trim(original)//' => '//trim(input_str)
ciphered = input_str
call decrypt(input_str, shift_val)
write(*, '(2a)') 'Decrypted: ', trim(ciphered)//' => '//trim(input_str)
call solve(input_str, original)
contains
subroutine encrypt(msg, shift)
character(*), intent(inout) :: msg
integer :: shift
integer :: i
do i = 1, len(msg)
select case(msg(i:i))
case ('A':'Z')
msg(i:i) = achar(modulo(iachar(msg(i:i)) - 65 + shift, 26) + 65)
case ('a':'z')
msg(i:i) = achar(modulo(iachar(msg(i:i)) - 97 + shift, 26) + 97)
end select
end do
end subroutine
subroutine decrypt(msg, shift)
character(*), intent(inout) :: msg
integer :: shift
integer :: i
do i = 1, len(msg)
select case(msg(i:i))
case ('A':'Z')
msg(i:i) = achar(modulo(iachar(msg(i:i)) - 65 - shift, 26) + 65)
case ('a':'z')
msg(i:i) = achar(modulo(iachar(msg(i:i)) - 97 - shift, 26) + 97)
end select
end do
end subroutine
subroutine solve(msg, original)
character(*), intent(inout) :: msg
character(*), intent(inout) :: original
character(4) :: shift_key
integer :: i
do i = 0, 26
msg = original
call encrypt(msg, i)
write(shift_key, '(I2)') i
write(*, '(2a)') 'Key:', trim(shift_key)//' => '//trim(msg)
end do
end subroutine
end program CodeCipher