Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace RandomShift {
  12. public partial class Form1 : Form {
  13. private int przesuniecie = 2;
  14.  
  15. public Form1() {
  16. InitializeComponent();
  17. }
  18.  
  19. //szyfruj
  20. private void button1_Click(object sender, EventArgs e) {
  21. TextBox tekstJawnyTB = (TextBox) tbTekstJawny;
  22. string tekst_jawny = tekstJawnyTB.Text;
  23. TextBox szyfrogramTB = (TextBox) tbSzyfrogram;
  24. string szyfrogram = "";
  25.  
  26. TextBox kluczTB = (TextBox) tbKlucz;
  27. int klucz = Int32.Parse(kluczTB.Text);
  28.  
  29. foreach (char c in tekst_jawny) {
  30. if (Char.IsLetter(c)) {
  31. int code = (int) c;
  32. int newCode = code - 65 + klucz;
  33. newCode = newCode % 26;
  34. newCode += 65;
  35.  
  36. if (newCode < 65) {
  37. newCode += 26;
  38. } else if (newCode > 90) {
  39. newCode -= 26;
  40. }
  41.  
  42. char newLetter = (char) newCode;
  43. szyfrogram += newLetter.ToString();
  44.  
  45. klucz += przesuniecie;
  46. }
  47. }
  48.  
  49. szyfrogramTB.Text = szyfrogram;
  50. }
  51.  
  52. //odszyfruj
  53. private void button2_Click(object sender, EventArgs e) {
  54. TextBox szyfrogramTB = (TextBox) tbSzyfrogram;
  55. string szyfrogram = szyfrogramTB.Text;
  56. TextBox odszyfrowanyTB = (TextBox) tbOdszyfrowany;
  57. string odszyfrowany = "";
  58.  
  59. TextBox kluczTB = (TextBox) tbKlucz;
  60. int klucz = Int32.Parse(kluczTB.Text);
  61.  
  62. foreach (char c in szyfrogram) {
  63. if (Char.IsLetter(c)) {
  64. int code = (int)c;
  65. int newCode = 0;
  66.  
  67. newCode = code - 65 - klucz;
  68. newCode = newCode % 26;
  69. newCode += 65;
  70.  
  71. if (newCode < 65) {
  72. newCode += 26;
  73. } else if (newCode > 90) {
  74. newCode -= 26;
  75. }
  76.  
  77. char newLetter = (char) newCode;
  78. odszyfrowany += newLetter.ToString();
  79.  
  80. klucz += przesuniecie;
  81. }
  82. }
  83.  
  84. odszyfrowanyTB.Text = odszyfrowany;
  85. }
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement