Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Simple java encryption

807601Mar 14 2008 — edited Mar 15 2008
Hi everyone,

I have a binary String representing a character - for example, character f = 01100110
I also have a cipher String represented as 0's and 1's that could be upto 128 in length = 01011101010111000101000010001001001111101111011 etc etc.

I want to use the cipher to encrypt the character and the reverse.

I have attempted to use BigInteger to XOR both and to decrypt do the reverse except occasionally I get a number format exception because the binary string is too large (I think).

Here is the code:
    public String encrypt(String plain, String secret) {
    
        BigInteger bi = new BigInteger(plain);
        BigInteger bii = new BigInteger(secret, 2);
        BigInteger res = bi.xor(bii);
        return res.toString();    
    }
   
    public String decrypt(String encrypted, String secret) {

        BigInteger bi = new BigInteger(encrypted);
        BigInteger bii = new BigInteger(secret, 2);
        BigInteger res = bii.xor(bi);
        String str = res.toString();
        long val = (long) Long.parseLong(str, 2);
        Character c = new Character((char) val);
        String out = (String) c.toString();
        return out;        
    }
Also the encrypt method is obvious as the bigInteger is the same value except for the last few digits.

Is there any way I can encrypt /decrypt so it is not so obvious and no number format errors?

Thanx in advance
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 12 2008
Added on Mar 14 2008
15 comments
465 views