

Task

Implement a Caesar cipher, both encoding and decoding. 

The key is an integer from 1 to 25.

This cipher rotates (either towards left or right) the letters of the alphabet (A to Z).

The encoding replaces each letter with the 1st to 25th next letter in the alphabet (wrapping Z to A).

So key 2 encrypts "HI" to "JK", but key 20 encrypts "HI" to "BC".

This simple "mono-alphabetic substitution cipher" provides almost no security, because an attacker who has the encoded message can either use frequency analysis to guess the key, or just try all 25 keys.

Caesar cipher is identical to Vigenère cipher with a key of length 1. 

Also, Rot-13 is identical to Caesar cipher with key 13.



Related tasks

 Rot-13
 Substitution Cipher
 Vigenère Cipher/Cryptanalysis


 
/* ARM assembly Raspberry PI  */
/*  program caresarcode.s   */
 
/* Constantes    */
.equ STDOUT, 1                           @ Linux output console
.equ EXIT,   1                           @ Linux syscall
.equ WRITE,  4                           @ Linux syscall
 
.equ STRINGSIZE,          500
 
/* Initialized data */
.data
szMessString:            .asciz "String :\n"
szMessEncrip:            .asciz "\nEncrypted :\n"
szMessDecrip:            .asciz "\nDecrypted :\n"
szString1:               .asciz "Why study medicine because there is internet ?"
 
szCarriageReturn:       .asciz "\n"
 
/* UnInitialized data */
.bss 
szString2:                .skip  STRINGSIZE
szString3:                .skip  STRINGSIZE
/*  code section */
.text
.global main 
main: 
 
    ldr r0,iAdrszMessString                     @ display message
    bl affichageMess
    ldr r0,iAdrszString1                        @ display string
    bl affichageMess
    ldr r0,iAdrszString1
    ldr r1,iAdrszString2
    mov r2,#20                                  @ key
    bl encrypt
    ldr r0,iAdrszMessEncrip
    bl affichageMess
    ldr r0,iAdrszString2                        @ display string
    bl affichageMess 
    ldr r0,iAdrszString2
    ldr r1,iAdrszString3
    mov r2,#20                                  @ key
    bl decrypt
    ldr r0,iAdrszMessDecrip
    bl affichageMess
    ldr r0,iAdrszString3                        @ display string
    bl affichageMess 
    ldr r0,iAdrszCarriageReturn
    bl affichageMess 
100:                                            @ standard end of the program
    mov r0, #0                                  @ return code
    mov r7, #EXIT                               @ request to exit program
    svc 0                                       @ perform system call
iAdrszMessString:         .int szMessString
iAdrszMessDecrip:         .int szMessDecrip
iAdrszMessEncrip:         .int szMessEncrip
iAdrszString1:            .int szString1
iAdrszString2:            .int szString2
iAdrszString3:            .int szString2
iAdrszCarriageReturn:     .int szCarriageReturn
/******************************************************************/
/*     encrypt strings                         */ 
/******************************************************************/
/* r0 contains the address of the string1 */
/* r1 contains the address of the encrypted string */
/* r2 contains the key (1-25)                     */
encrypt:
    push {r3,r4,lr}           @ save  registers 
    mov r3,#0                 @ counter byte string 1
1:
    ldrb r4,[r0,r3]           @ load byte string 1
    cmp r4,#0                 @ zero final ?
    streqb r4,[r1,r3]
    moveq r0,r3
    beq 100f
    cmp r4,#65                @ < A ?
    strltb r4,[r1,r3]
    addlt r3,#1
    blt 1b
    cmp r4,#90                @ > Z
    bgt 2f
    add r4,r2                 @ add key
    cmp r4,#90                @ > Z
    subgt r4,#26
    strb r4,[r1,r3]
    add r3,#1
    b 1b
2:
    cmp r4,#97                @ < a ?
    strltb r4,[r1,r3]
    addlt r3,#1
    blt 1b
    cmp r4,#122               @> z
    strgtb r4,[r1,r3]
    addgt r3,#1
    bgt 1b
    add r4,r2
    cmp r4,#122
    subgt r4,#26
    strb r4,[r1,r3]
    add r3,#1
    b 1b
 
100:
    pop {r3,r4,lr}            @ restaur registers
    bx lr                     @ return
/******************************************************************/
/*     decrypt strings                                           */ 
/******************************************************************/
/* r0 contains the address of the encrypted string1 */
/* r1 contains the address of the decrypted string */
/* r2 contains the key (1-25)                     */
decrypt:
    push {r3,r4,lr}          @ save  registers 
    mov r3,#0                @ counter byte string 1
1:
    ldrb r4,[r0,r3]          @ load byte string 1
    cmp r4,#0                @ zero final ?
    streqb r4,[r1,r3]
    moveq r0,r3
    beq 100f
    cmp r4,#65               @ < A ?
    strltb r4,[r1,r3]
    addlt r3,#1
    blt 1b
    cmp r4,#90               @ > Z
    bgt 2f
    sub r4,r2                @ substract key
    cmp r4,#65               @ < A
    addlt r4,#26
    strb r4,[r1,r3]
    add r3,#1
    b 1b
2:
    cmp r4,#97               @ < a ?
    strltb r4,[r1,r3]
    addlt r3,#1
    blt 1b
    cmp r4,#122              @ > z
    strgtb r4,[r1,r3]
    addgt r3,#1
    bgt 1b
    sub r4,r2                @ substract key
    cmp r4,#97               @ < a
    addlt r4,#26
    strb r4,[r1,r3]
    add r3,#1
    b 1b
 
100:
    pop {r3,r4,lr}           @ restaur registers
    bx lr                    @ return
/******************************************************************/
/*     display text with size calculation                         */ 
/******************************************************************/
/* r0 contains the address of the message */
affichageMess:
    push {r0,r1,r2,r7,lr}                       @ save  registers 
    mov r2,#0                                   @ counter length */
1:                                              @ loop length calculation
    ldrb r1,[r0,r2]                             @ read octet start position + index 
    cmp r1,#0                                   @ if 0 its over
    addne r2,r2,#1                              @ else add 1 in the length
    bne 1b                                      @ and loop 
                                                @ so here r2 contains the length of the message 
    mov r1,r0                                   @ address message in r1 
    mov r0,#STDOUT                              @ code to write to the standard output Linux
    mov r7, #WRITE                              @ code call system "write" 
    svc #0                                      @ call system
    pop {r0,r1,r2,r7,lr}                        @ restaur registers
    bx lr                                       @ return
 
 