[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 384
  • Last Modified:

BASE64Encoder



Hello All

I have this code

String s = null;
s = "somevalue";

BASE64Encoder encoder = new BASE64Encoder();
String encoded = encoder.encodeBuffer(s.getBytes());

return encoded;


The value returned has a carriage return, so when i insert this value into a db table and do a select where clause the value isn't picked up as the select statement looks like this

select * from table where encoded = 'fklgjfjglfjklgjf'

But if i do this

select * from table where encoded = 'fklgjfjglfjklgjf
'

It works

Can anyone tell me how i get rid of the carriage return ?

Many Thanks
0
code_123
Asked:
code_123
1 Solution
 
plork123Commented:


Try this

byte[] sUserIdBytes = s.getBytes();
            
BASE64Encoder encoder = new BASE64Encoder();
       
String encoded = encoder.encode(sUserIdBytes ) ;

return encoded ;
0
 
imladrisCommented:
From your select statements it looks like the offending character is at the end of the string. In that case the simple solution would be something like:

String s = null;
s = "somevalue";

BASE64Encoder encoder = new BASE64Encoder();
String encoded = encoder.encodeBuffer(s.getBytes());

return encoded.substring(0,encoded.length()-1);
0
 
CEHJCommented:
Let's look at this a different way - firstly, why are you saving something as Base64?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
code_123Author Commented:


what should i be saving it as then?
0
 
CEHJCommented:
There are other options, but without knowing why you're using this method, it's hard to comment
0
 
code_123Author Commented:


I just want ot encypt a string - that's all, so pass a string into the method and return an encrypted version of is

e.g. normal string = mystring
encrypted string = hjgjkf898TWQ890585jkjk954534323dwe ......
0
 
CEHJCommented:
Well Base64's purpose is not for encryption - it offers negligible protection. Do you want to encrypt it two-way or one-way?
0
 
code_123Author Commented:


one-way


But can you show me 2-way as well :)

0
 
CEHJCommented:
If it's one-way, then you would be best off using MessageDigest with SHA1. Practicably uncrackable.

Two-way - see the following (and related links):

http://javaalmanac.com/egs/javax.crypto/DesString.html
0
 
code_123Author Commented:


do you have an example of messagedigest with sha1 ?
0
 
CEHJCommented:
0
 
CEHJCommented:
You can use this to give you a hash you can read (but not decrypt) and save in your db:



      public static String getDigest(String s) {
            try {
                  MessageDigest md = MessageDigest.getInstance("SHA1");
                  byte[] bytes = md.digest(s.getBytes());
                  return new BigInteger(1, bytes).toString(16);
            } catch (NoSuchAlgorithmException e) {
                  e.printStackTrace();
            }
            return null;
      }
0
 
CEHJCommented:
Small correction:


      public static String getDigest(String s) {
            String result = null;
            try {
                  MessageDigest md = MessageDigest.getInstance("SHA1");
                  byte[] bytes = md.digest(s.getBytes());
                  result = new BigInteger(1, bytes).toString(16);
                  while (result.length() < 40) {
                        result = "0" + result;
                  }
            } catch (NoSuchAlgorithmException e) {
                  e.printStackTrace();
            }
            return result;
      }
      
0
 
code_123Author Commented:



Cheers

Works much better

0
 
CEHJCommented:
:-)
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now