0
 return Base64.encodeBase64String(cipherText);

im getting an error in this line.It suggest me to make a class in base64,but is there any standard class for Base64?Im using this to encrypt.

import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.Arrays;
import java.io.*;
import java.lang.*;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;


    public class TRIPPLE_DES {


        public static void main(String[] args) throws Exception {


    String text = "cardNumber=28293939493330&securityCode=123&cardExpiryMonth=07&cardExpiryYear=2013&cardHolderName=Test&transactionAmount=50.00&currencyCode=356&customerReferenceNo=9393938393938&cardProvider=VISA&name=Test&mobileNo=983345123412&email=test@test.com&contactNo=983345123412&password=wyzgames&amount=100&remoteIP=10.10.10.50&checkSum=92be84d25b60b3f9f233c074d12ade1ddef158cb369a0734afff3fb6adc9d7ddb4b26f7e6001563747a8d47457e713750e5802b4871cfbe70baca9304d4c385f";


         String codedtext = new TRIPPLE_DES().encrypt(text);
         String decodedtext = new TRIPPLE_DES().decrypt(codedtext);


         String encodedurl = URLEncoder.encode(codedtext,"UTF-8");
         System.out.println(encodedurl);
         System.out.println(decodedtext);
        }


        public String encrypt(String message) throws Exception {
         final MessageDigest md = MessageDigest.getInstance("md5");
         final byte[] digestOfPassword = md.digest("HG58YZ3CR9".getBytes("utf-8"));
         final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);


         for (int j = 0, k = 16; j < 8;) {
          keyBytes[k++] = keyBytes[j++];
         }


         final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
         final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
         final Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
         cipher.init(Cipher.ENCRYPT_MODE, key, iv);


         final byte[] plainTextBytes = message.getBytes("utf-8");
         final byte[] cipherText = cipher.doFinal(plainTextBytes);

         return Base64.encodeBase64String(cipherText);
        }


        public String decrypt(String message) throws Exception
        {
         final MessageDigest md = MessageDigest.getInstance("md5");
         final byte[] digestOfPassword = md.digest("HG58YZ3CR9".getBytes("utf-8"));
         final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
         for (int j = 0, k = 16; j < 8;) {
          keyBytes[k++] = keyBytes[j++];
         }


         final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
         final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
         final Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
         decipher.init(Cipher.DECRYPT_MODE, key, iv);



         byte[] byteMessage= Base64.decodeBase64(message);
         final byte[] plainText = decipher.doFinal(byteMessage);


         return new String(plainText, "UTF-8");
        }
    }
Ajeesh
  • 5,650
  • 8
  • 30
  • 52
  • 1
    What do you expect to happen from this command: `return Base64.encodeBase64String(cipherText);` ? You wrote this yourself, and `Base64` doesn't exist in your code. – Math Nov 19 '13 at 12:04
  • possibly duplicate of http://stackoverflow.com/questions/16169008/why-method-base64-encodebase64stringbyte-is-not-available – Shoaib Chikate Nov 19 '13 at 12:05
  • @Math not my code..Its a code provided to me – Ajeesh Nov 19 '13 at 12:07

2 Answers2

3

There isn't an official Base64 class in JDK.

I can suggest you to use this: Apache Commons Codec

Cirou
  • 1,420
  • 12
  • 18
  • Ok thanks.i have downloaded this commen codec.How can I use it with this program – Ajeesh Nov 19 '13 at 12:11
  • Add the library in your project's classpath and import the required class – Cirou Nov 19 '13 at 12:17
  • As of Java 6 there is built-in support for Base64 encoding. No need to use an external library. http://stackoverflow.com/questions/469695/decode-base64-data-in-java/2054226#2054226 – Marko Topolnik Nov 19 '13 at 12:17
  • @MarkoTopolnik yes i know there are 'workarounds' but i was talking about an official `Base64` class. Btw i prefer to use the Apache library – Cirou Nov 19 '13 at 12:19
  • @Cirou Why do you consider an official JDK class a 'workaround'? – Marko Topolnik Nov 19 '13 at 12:24
  • I state that i never used the `javax.xml.bind.DatatypeConverter` class, but after reading the comments at the answer you linked i'm still not persuaded in using it. I always used the Apache Commons Codec and never had problems – Cirou Nov 19 '13 at 12:30
0

you can use Base64Encoder and Base64Decoder for you purpose.

Change

Base64.encodeBase64String(cipherText)

to

new BASE64Encoder().encode(cipherText)

and

Base64.decodeBase64(message)

to

new BASE64Decoder().decodeBuffer(message)
Adi
  • 2,364
  • 1
  • 17
  • 23