Important alert: (current site time 7/15/2013 3:41:19 PM EDT)
 

VB icon

Byte Bump Encode

Email
Submitted on: 3/13/2002 3:42:39 PM
By: John McDonald  
Level: Beginner
User Rating: By 2 Users
Compatibility: Java (JDK 1.1), Java (JDK 1.2)
Views: 9465
author picture
(About the author)
 
     My code takes a string, turns it to binary (a series of 1's and 0's) then adds a few extra fake bits at the begining of the series and a couple at the end(to even it out). Then turns it back into a Readable string again. It also does the reverse process. Example: ByteBump.doByteBump("This is a sample to show people what on earth this does"); would return this: "?Q́́???Ѽ͡܁??ݡ?Ё??Ѡѡ́??"
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Byte Bump Encode
// Description:My code takes a string, turns it to binary (a series of 1's and 0's) then adds a few extra fake bits at the begining of the series and a couple at the end(to even it out). Then turns it back into a Readable string again. It also does the reverse process.
Example: 
ByteBump.doByteBump("This is a sample to show people what on earth this does");
would return this:
"?Q́́???Ѽ͡܁??ݡ?Ё?? ѡ́??"
// By: John McDonald
//
// Inputs:A String
//
// Returns:An encoded String
//
// Assumes:This only encodes a string if all letters are in the ascii character set. 0-255
//
// Side Effects:None known
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=2732&lngWId=2//for details.//**************************************

public class ByteBump { 
	
	public static void main(String args[]){
		// sample use
		String coded = ByteBump.doByteBump("This is a sample phrase to see what it would look like");
		System.out.println(coded);
		System.out.println(ByteBump.undoByteBump(coded));
	}
	
	public static String doByteBump(String text){
		// change the text to binary, bump the bits down 6 notches, then turn it back to text
		
		String bits = toBinary(text);
		String fakeBits = "";
		String rv = "";
		
		// make a fake byte
		for(int i = 0; i < 8; i++){
			fakeBits += String.valueOf((int)(Math.rint(Math.random())));
		}
		
		// put 6 fake bits from our fake byte on the front of the real bits
		// and the other 2 fake bits on the end
		bits = fakeBits.substring(0, 6) + bits + fakeBits.substring(6);
		
		rv = fromBinary(bits);
		
		return rv;
	}
	
	public static String undoByteBump(String text){
		// change the text to binary, bump the bits down 6 notches, then turn it back to text
		if(text.length() == 1){return text;}
		
		
		String bits = toBinary(text);
		String rv = "";
		
		// take off the fake bits that were added in the last process
		bits = bits.substring(6, bits.length() - 2);
		
		rv = fromBinary(bits);
		
		return rv;
	}
	
	public static String toBinary(String text){
		// this turns a string into a sequence of bits
		String rv = "";
		
		// go through every letter and turn it to binary
		for(int i = 0; i < text.length(); i++){
			int curByte = (int)text.charAt(i);
			
			if(curByte >= 128){
				rv += 1;
				curByte -= 128;
			}else{rv += 0;}
			
			if(curByte >= 64){
				rv += 1;
				curByte -= 64;
			}else{rv += 0;}
			
			if(curByte >= 32){
				rv += 1;
				curByte -= 32;
			}else{rv += 0;}
			
			if(curByte >= 16){
				rv += 1;
				curByte -= 16;
			}else{rv += 0;}
			
			if(curByte >= 8){
				rv += 1;
				curByte -= 8;
			}else{rv += 0;}
			
			if(curByte >= 4){
				rv += 1;
				curByte -= 4;
			}else{rv += 0;}
			
			if(curByte >= 2){
				rv += 1;
				curByte -= 2;
			}else{rv += 0;}
			
			if(curByte >= 1){
				rv += 1;
				curByte -= 1;
			}else{rv += 0;}
		}
		
		return rv;
	}
	
	public static String fromBinary(String bits){
		// this turns a sequence of bits into a String
		String rv = "";
		
		// go through every 8 bits and turn then into a character
		for(int i = 0; i < bits.length(); i++){
			int curByte = 0;
			
			if(bits.charAt(i) == '1'){curByte += 128;}
			
			i++;
			if(bits.charAt(i) == '1'){curByte += 64;}
			
			i++;
			if(bits.charAt(i) == '1'){curByte += 32;}
			
			i++;
			if(bits.charAt(i) == '1'){curByte += 16;}
			
			i++;
			if(bits.charAt(i) == '1'){curByte += 8;}
			
			i++;
			if(bits.charAt(i) == '1'){curByte += 4;}
			
			i++;
			if(bits.charAt(i) == '1'){curByte += 2;}
			
			i++;
			if(bits.charAt(i) == '1'){curByte += 1;}
			
			rv += (char)curByte;
		}
		
		return rv;
	}
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
3/13/2003 1:03:40 AM

Yes...it is fine with lengthy strings,but in case, take 4 digits(take as a string) it is not provide anyb security...it can crack in a seconds...is it possible to add some more fake bits so that it will difficult to crack...it is urgent to me...if u have a answer please mail to me to patnaik_rjpmc@rediffmail.com
Regards
(If this comment was disrespectful, please report it.)

 
3/22/2006 4:38:46 AMAmirizzuan

man, your code is awesome. i use it inside my program for the company i worked with. i use your convert from string to binary function inside my encryption function.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.