VegenerCipher.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. Name: Ajinkya Arun Gurav
  3. Div : H Roll No:21
  4. GR No : 121237
  5. Subject : Network Security Lab
  6. Lab 2: Implement encryption and decryption using Vegener Cipher
  7. */
  8. import java.io.BufferedReader;
  9. import java.io.IOException;
  10. import java.io.InputStreamReader;
  11. public class VegenerCipher {
  12. static String encrypt(String PlainText,int Key[])
  13. {
  14. String cipherText="";
  15. int index=0,offset=0,charAscii=0,CharVal=0,KeyIndex=0;
  16. System.out.println("PlainText Length: "+PlainText.length());
  17. for(index=0;index<PlainText.length();index++)
  18. {
  19. if(PlainText.charAt(index)==' '){
  20. cipherText+=' ';
  21. continue;
  22. }
  23. charAscii=PlainText.charAt(index)+Key[KeyIndex];
  24. CharVal=PlainText.charAt(index);
  25. /*if((charAscii-48)>=10 && CharVal>=48 && CharVal<=57)
  26. {
  27. offset=(charAscii-48)%10;
  28. cipherText+=(char)(offset+48);
  29. }*/
  30. if((charAscii-97) >= 26 && CharVal>=97 && CharVal<=122)
  31. {
  32. offset=(charAscii-97)%26;
  33. cipherText+=(char)(offset+97);
  34. }
  35. else if((charAscii-65) >= 26 && CharVal>=65 && CharVal<=90)
  36. {
  37. offset=(charAscii-65)%26;
  38. cipherText+=(char)(offset+65);
  39. }
  40. else
  41. {
  42. cipherText+=((char) (PlainText.charAt(index)+Key[KeyIndex]));
  43. }
  44. }
  45. System.out.println("CipherText is: "+cipherText);
  46. return cipherText;
  47. }
  48. static String decrypt(String cipherText,int Key[])
  49. {
  50. String PlainText="";
  51. int index=0,offset=0,charAscii=0,CharVal=0,KeyIndex=0;
  52. System.out.println("CipherText Length: "+cipherText.length());
  53. for(index=0;index<cipherText.length();index++)
  54. {
  55. if(cipherText.charAt(index)==' '){
  56. PlainText+=' ';
  57. continue;
  58. }
  59. charAscii=cipherText.charAt(index)-Key[KeyIndex];
  60. CharVal=cipherText.charAt(index);
  61. /*if((charAscii)<48 && CharVal>=48 && CharVal<=57)
  62. {
  63. offset=(charAscii-48)%10;
  64. if(offset<0)
  65. offset=-(offset);
  66. if(offset==0)
  67. PlainText+='0';
  68. else
  69. PlainText+=(char)(57-offset+1);
  70. }*/
  71. if(charAscii<65 && CharVal>=65 && CharVal<=90)
  72. {
  73. offset=(charAscii-65)%26;
  74. if(offset<0)
  75. offset=-(offset);
  76. if(offset==0)
  77. PlainText+='A';
  78. else
  79. PlainText+=(char)(90-offset+1);
  80. }
  81. else if((charAscii)<97 && CharVal>=97 && CharVal<=122)
  82. {
  83. offset=(charAscii-97)%26;
  84. if(offset<0)
  85. offset=-(offset);
  86. if(offset==0)
  87. PlainText+='a';
  88. else
  89. PlainText+=(char)(122-offset+1);
  90. }
  91. else
  92. {
  93. PlainText+=((char) (cipherText.charAt(index)-Key[KeyIndex]));
  94. }
  95. }
  96. System.out.println("PlainText is: "+PlainText);
  97. return cipherText;
  98. }
  99. public static void main(String args[])
  100. {
  101. String ptext=null,cipherText=null;
  102. int keyLength=0;
  103. int Keys[]=null;
  104. BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  105. try {
  106. System.out.println("Enter the PlainText");
  107. ptext=br.readLine();
  108. System.out.println("Enter the Key length");
  109. keyLength=Integer.parseInt(br.readLine());
  110. Keys=new int[keyLength];
  111. for(int i=0;i<keyLength;i++)
  112. {
  113. Keys[i]=Integer.parseInt(br.readLine());
  114. }
  115. } catch (IOException e) {
  116. // TODO Auto-generated catch block
  117. System.out.println("Exception:"+e.getCause());
  118. }
  119. cipherText=encrypt(ptext,Keys);
  120. decrypt(cipherText,Keys);
  121. }
  122. }
  123. /*
  124. Output:
  125. Enter the PlainText
  126. Ajinkya Gurav
  127. Enter the Key length
  128. 3
  129. 10
  130. 15
  131. 13
  132. PlainText Length: 13
  133. CipherText is: Ktsxuik Qebkf
  134. CipherText Length: 13
  135. PlainText is: Ajinkya Gurav
  136. */