new files
This commit is contained in:
parent
3af7344581
commit
86c034bb8b
1364 changed files with 21352 additions and 0 deletions
59
Task/Caesar-cipher/Go/caesar-cipher-1.go
Normal file
59
Task/Caesar-cipher/Go/caesar-cipher-1.go
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ckey struct {
|
||||
enc, dec func(rune) rune
|
||||
}
|
||||
|
||||
func newCaesar(k int) (*ckey, bool) {
|
||||
if k < 1 || k > 25 {
|
||||
return nil, false
|
||||
}
|
||||
rk := rune(k)
|
||||
return &ckey{
|
||||
enc: func(c rune) rune {
|
||||
if c >= 'a' && c <= 'z'-rk || c >= 'A' && c <= 'Z'-rk {
|
||||
return c + rk
|
||||
} else if c > 'z'-rk && c <= 'z' || c > 'Z'-rk && c <= 'Z' {
|
||||
return c + rk - 26
|
||||
}
|
||||
return c
|
||||
},
|
||||
dec: func(c rune) rune {
|
||||
if c >= 'a'+rk && c <= 'z' || c >= 'A'+rk && c <= 'Z' {
|
||||
return c - rk
|
||||
} else if c >= 'a' && c < 'a'+rk || c >= 'A' && c < 'A'+rk {
|
||||
return c - rk + 26
|
||||
}
|
||||
return c
|
||||
},
|
||||
}, true
|
||||
}
|
||||
|
||||
func (ck ckey) encipher(pt string) string {
|
||||
return strings.Map(ck.enc, pt)
|
||||
}
|
||||
|
||||
func (ck ckey) decipher(ct string) string {
|
||||
return strings.Map(ck.dec, ct)
|
||||
}
|
||||
|
||||
func main() {
|
||||
pt := "The five boxing wizards jump quickly"
|
||||
fmt.Println("Plaintext:", pt)
|
||||
for _, key := range []int{0, 1, 7, 25, 26} {
|
||||
ck, ok := newCaesar(key)
|
||||
if !ok {
|
||||
fmt.Println("Key", key, "invalid")
|
||||
continue
|
||||
}
|
||||
ct := ck.encipher(pt)
|
||||
fmt.Println("Key", key)
|
||||
fmt.Println(" Enciphered:", ct)
|
||||
fmt.Println(" Deciphered:", ck.decipher(ct))
|
||||
}
|
||||
}
|
||||
57
Task/Caesar-cipher/Go/caesar-cipher-2.go
Normal file
57
Task/Caesar-cipher/Go/caesar-cipher-2.go
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
type ckey struct {
|
||||
enc, dec unicode.SpecialCase
|
||||
}
|
||||
|
||||
func newCaesar(k int) (*ckey, bool) {
|
||||
if k < 1 || k > 25 {
|
||||
return nil, false
|
||||
}
|
||||
i := uint32(k)
|
||||
r := rune(k)
|
||||
return &ckey{
|
||||
unicode.SpecialCase{
|
||||
{'A', 'Z' - i, [3]rune{r}},
|
||||
{'Z' - i + 1, 'Z', [3]rune{r - 26}},
|
||||
{'a', 'z' - i, [3]rune{r}},
|
||||
{'z' - i + 1, 'z', [3]rune{r - 26}},
|
||||
},
|
||||
unicode.SpecialCase{
|
||||
{'A', 'A' + i - 1, [3]rune{26 - r}},
|
||||
{'A' + i, 'Z', [3]rune{-r}},
|
||||
{'a', 'a' + i - 1, [3]rune{26 - r}},
|
||||
{'a' + i, 'z', [3]rune{-r}},
|
||||
},
|
||||
}, true
|
||||
}
|
||||
|
||||
func (ck ckey) encipher(pt string) string {
|
||||
return strings.ToUpperSpecial(ck.enc, pt)
|
||||
}
|
||||
|
||||
func (ck ckey) decipher(ct string) string {
|
||||
return strings.ToUpperSpecial(ck.dec, ct)
|
||||
}
|
||||
|
||||
func main() {
|
||||
pt := "The five boxing wizards jump quickly"
|
||||
fmt.Println("Plaintext:", pt)
|
||||
for _, key := range []int{0, 1, 7, 25, 26} {
|
||||
ck, ok := newCaesar(key)
|
||||
if !ok {
|
||||
fmt.Println("Key", key, "invalid")
|
||||
continue
|
||||
}
|
||||
ct := ck.encipher(pt)
|
||||
fmt.Println("Key", key)
|
||||
fmt.Println(" Enciphered:", ct)
|
||||
fmt.Println(" Deciphered:", ck.decipher(ct))
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue