Question: Need help fixing the error of Exception in thread main java.lang.StringIndexOutOfBoundsException: String index out of range: 6 in the code below. Everything was working fine
Need help fixing the error of "Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6" in the code below. Everything was working fine until it reaches the point of converting the result to ASCII in the cribDrag() function.
public static void main(String[] arguments)throws UnsupportedEncodingException { String cipherText6 = "9a6972380c5a91e5f80537193ca133c7e75faea9924bb191953e8f22d7c5adf067264b1d96f853c41892ad4480bd73f54902b1af35ef1860ac02972d05013543d93d306603fb4932be8b3d8f4825613183571320c170d9cde638f41e80e173e3dc5caefb574d6fce688cc49113f515a6cf7f2c066e4c3385230885884ddcf3f898029fe1e8dd3fcc9f76f3a77d35fa2d7" + "5b281b7e56b7f45bf3225e3ff003501d78d146e018e484ee18383d187ca0d6025d5f348ed9b61ae7f33acbfddf36b774d60a86ca016d1f4c83b8dbdaf59795e624dbdbbf310a5b271813fa31400835f4eec250d" + "dd43de10a25e790c9f28e7ebd0c74fa11181fcf79cf68d5bd75b662bbacf38d31b39cbcdc71d213d611812188ebe8855dcf857337ac75e2b36cceb0c4a729e1b7c72e78e0962c112351e62aa0f41456ebd34667929cdb06c2ffd627d7b11da0b6dd57900d0cafd9651a32e8d4247a7ab3cce24d08150ceee321b38dae50481acf00896ce1f8c7b7499dcf79008c9b5aee24b8de4ff1737116c"; String cipherText3 = "8f6f3779154f99e8e014375c34a267c1e745bbad895fe391c526873383cfa1e86632575481e803c41c93f94d97a738f54d02a9ec66bb1360b44ed3210206254fcc627b240bfb1d38b899788a093d2f2a9b4b1327c17edf99b224fe1e9ee66effc649a3b5430c7c9a2a91d7c51bab14e9d57d39566a5634c4340c858c5a93fff89e0394f3f49369ce9c6ee6f26b29a82e" + "29fb9ca0a8657c48ad2c60eaf70a6b44918a40630392171ca29b87cd9e9e037c6ad2bc4be08c61ba6223a8bac0a024741b71b270a0579ab1d0308ce9b54b391a6a49ebacfc5eb5b57dd56caf0b1694544cfa6e"; //System.out.println(xorHex(cipherText6, cipherText3)); String result = xorHex(cipherText6, cipherText3); cribDrag(result); } public static String xorHex(String s, String t) { //Convert String into BigInt BigInteger a = new BigInteger(s, 16); BigInteger b = new BigInteger(t, 16); //XOR the 2 BigInt's BigInteger xorResult = a.xor(b); //Convert BigInt to String of Hex String result = xorResult.toString(16); return result; } public static String hexToAscii(String s) { StringBuilder builder = new StringBuilder();
for (int i = 0; i < s.length(); i = i + 2) { //Split the hex string into two character group String ch = s.substring(i, i + 2); //Convert the each character group into integer using valueOf method int n = Integer.valueOf(ch, 16); //Cast the integer value to char builder.append((char)n); } return builder.toString(); } public static String asciiToHex(String s) { //Convert ASCII string to char array char[] ch = s.toCharArray(); StringBuilder builder = new StringBuilder(); //Iterate over char array and cast each element to Integer. for (char c : ch) { int i = (int) c; //Convert integer value to hex using toHexString() method. builder.append(Integer.toHexString(i).toUpperCase()); } return builder.toString(); }
public static Boolean cribCheck(String crib) { String cribResult =""; if(crib.matches("^[a-zA-Z0-9 ,?()-]*$")) { return true; } return false; } public static void cribDrag(String xor) throws StringIndexOutOfBoundsException { String crib0ne = "the"; //Convert crib to hex String cribWork = asciiToHex(crib0ne); System.out.print(" " + cribWork + " "); for(int i = 0; i < (xor.length() - cribWork.length()); i++) { String xorTemp = xor.substring(i, i + cribWork.length()); //XOR cribwork and substring(part of xor) String result = xorHex(xorTemp, cribWork); //Convert hex to ASCII result = hexToAscii(result); if(cribCheck(result)) { System.out.print(" ---------> " + result + " "); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
