Java Encryption Code HELP. HELP HELP?
-
I HAVE NO idea what I am suppsoe to do for this code. All I know is, basically you have a phrase and teh computer encrypts the phrase by changuing the aphabet. ex. A=V B= Y....... and I am suppsoe to give a hint to the user like this is the letter that occurs the most in the phrase. help me
-
Answer:
OK, I have learned to use StringBuilder a lot. Deep inside the JVM, java uses StringBuilder whenever we concatenate Strings with the "+" operator. In fact, if we have 10,000 Strings, StringBuilder is x1500 percent faster than "+". Here is code that countes freqency, the highest number of like chars is the divider of a "normal" alphabet to make a new alphabet with the last chunk rotated to the front. Then, we take the original message and for each character, we find the index in the normal alphabet and use that index number to choose from the "rotated alphabet". Maybe you can do the same without StringBuilder. //>======================= import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.Set; public class AlphaShift { public static void main(String[] args) { String testData = "Resistance is futile, Captain Pichard"; String keyCrypt = countCharRepeats(testData); System.out.println("keyCrypt: " + keyCrypt); String secretMess = construeCrypt( keyCrypt, testData ); System.out.println( "secretMessage: " + secretMess); } private static String construeCrypt( String key, String mess ) { StringBuilder sb = new StringBuilder(); char alpha = ' '; // space char or 32 char omega = '~'; for (int i = (int)alpha; i <= (int)omega; i++) { sb.append( (char) i ); } System.out.println("sb: " + sb.toString()); String keyShift = key.substring(0,1); int shiftFactor = sb.indexOf(keyShift); String firstChunk = sb.substring(shiftFactor); System.out.println("firstChunk: " + firstChunk); StringBuilder sb2 = new StringBuilder( sb.substring( shiftFactor)); sb2.append( sb.substring( 0,shiftFactor)); System.out.println("sb2: " + sb2.toString()); String hint = "/" + key + "/"; // checkSum in zip files StringBuilder secretMessage = new StringBuilder(hint); for (int i = 0; i < mess.length(); i++) { String search = String.valueOf( mess.charAt(i) ); int charIndex = sb.indexOf(search); secretMessage.append( sb2.charAt(charIndex)); } return secretMessage.toString(); } static private String countCharRepeats(String msg) { String[] holder; // split das String into chars char[] chars = msg.toCharArray(); // kinda like an egg carton, only one can go into // one slot Set<Character> dupes = new HashSet<Character>(); for (char c : chars) { if (!dupes.add(c)) { continue; } } // fetch the contents of Set to a List, to sort int counter = 0; ArrayList<Character> list = new ArrayList<Character>(); for (Character character : dupes) { if( Character.isLetter(character) ) list.add(character); } // put the turkey into lexiconagle order Collections.sort(list); // now get ready to report results holder = new String[list.size()]; for (Character character : list) { int count = 0; holder[counter] = String.valueOf(character); for (int i = 0; i < chars.length; i++) { count += (character.compareTo(chars[i]) == 0) ? 1 : 0; } holder[counter] += "," + count; counter++; } int hi_ndex = 0; for (int i = 0; i < holder.length; i++) { String[] flds = holder[i].split(","); System.out.printf("%s\'s: %s%n", flds[0], flds[1]); int comp = Integer.parseInt(flds[1]); hi_ndex = (comp > hi_ndex) ? i : hi_ndex; } System.out.println("The dupes: " + list.toString()); System.out.println("The String: " + msg); return holder[ hi_ndex ]; } }
James Bond at Yahoo! Answers Visit the source
Related Q & A:
- how to close the applet in java code?Best solution by Stack Overflow
- Can't delete name off messenger address list, error code 40402,need help pls?Best solution by Yahoo! Answers
- I did something really bad and now i need help please help me.Best solution by Yahoo! Answers
- I need help with writing up the Java Code.Best solution by Yahoo! Answers
- Help help With my jobs?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.