Link to home
Start Free TrialLog in
Avatar of marshia
marshia

asked on

Java Data Encryption Standard (DES) Algorithm

I want to use the methods of DesCipher class.
specifically the encrypt.  It has this definition :

public void encrypt(byte clearText[],
                      int clearOff,
                      byte cipherText[],
                      int cipherOff)
Could anybody know what the parameters mean?
Avatar of mbormann
mbormann

these r available from
http://www.acme.com/java/software/
downloaded that and keeping it for refernce.

Thanx.
i dunno anything abt that

Hi mbormann,

I got
thttpd-2_05_tar.tar. Then what next?
i also dunno ,will do it when i have free time sometime.
pun intended.
:-)
Avatar of marshia

ASKER

Thanks mbormann but I already visited the site
 [ www.acme.com/java/software]
before but only got the functions without further explanations on the needed parameters.
From what I can see, I believe the meanings are:

clearText[] - byte array of un encrypted data

int clearOff - offset into clear text at which to start encoding

byte cipherText[] - encrypted data

int cipherOff - offset into encrypted data at which encryption starts

Encrypt probably works something like this:

    /// Encrypt some bytes.
    // The default implementation just calls encrypt(byte) repeatedly.
    // This can be overridden for speed.
    public void encrypt( byte[] clearText, int clearOff, byte[] cipherText, int cipherOff, int len )
      {
      for ( int i = 0; i < len; ++i )
          cipherText[cipherOff + i] = encrypt( clearText[clearOff + i] );
      }

so some data at the start of your block of bytes can be left unencrypted, for whatever reason you choose.

does this make sense?
Avatar of marshia

ASKER

Thanks Jod but I want to use the Java class , DesCipher and the method - encrypt which has
4 parameters.
 
With your example,  with the way I understand it,
I still need to create my own encrypt function.
Nope - the example I gave above is just from the same family of encryption tools to show you what it is doing.

Just use DesCipher and call it like this:

encrypt(
  dataArrayToEncrypt, //as a byte array

  clearoff, //offset into dataArrayToEncrypt to
            // start encrypting data at

  encryptedData, //where encrypted data is stored

  cipheroff // where to start storing encrypted data at in encryptedData
)

ASKER CERTIFIED SOLUTION
Avatar of mbormann
mbormann

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of marshia

ASKER

thanks mbormann,
    I can go on with my program now.

Avatar of marshia

ASKER

mbormann,
   congratulations :-)  you've got the points.
hey Jod should have got those man,i didn't pay much attention here.
:-)
Don't worry about it mb - just enjoy and know that as it's you I won't be getting the Men In Black to pay you a visit.

This one was a bit of a bummer though...

https://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=10233432 
Your example was clearer than mine anyway...
i think he still hasn't solved it,marshia?

and Jod ,i don't have points to see the question but i saw it thru my colleague's acct ,OK what is the bummer aside from the fact that "expertmb" is a jumping jiminy sonofagun ?

Yeah i agree that " returning an int as the file length is not a good idea"

also in the same vein  "returning an
int as the String length is not a good idea".

a pet question which i ask many people is ,Is it possible in C++ to create arbitray Strings of 'infinite' length limited only by physical Memory?i think it's limited to (on Intel machines)to 64KB data?

Please I want a answer as i am highly curious.

It is theoratically possible in Java ,ain't it so?but limited by physical Memory
>> jumping jiminy sonofagun

Strong words, softly spoken...;-)


http://www.codeguru.com/cpp/tic/tic0178.shtml

The exact implementation of memory layout for the string class is not defined by the C++ Standard. This architecture is intended to be flexible enough to allow differing implementations by compiler vendors, yet guarantee predictable behavior for users. In particular, the exact conditions under which storage is allocated to hold data for a string object are not defined. String allocation rules were formulated to allow but not require a reference-counted implementation, but whether or not the implementation uses reference counting, the semantics must be the same. To put this a bit differently, in C, every char array occupies a unique physical region of memory. In C++, individual string objects may or may not occupy unique physical regions of memory, but if reference counting is used to avoid storing duplicate copies of data, the individual objects must look and act as though they do exclusively own unique regions of storage. For example:

//: C17:StringStorage.cpp
#include <string>
#include <iostream>
using namespace std;

int main() {
  string s1("12345");
  // Set the iterator indicate the first element
  string::iterator it = s1.begin();
  // This may copy the first to the second or
  // use reference counting to simulate a copy
  string s2 = s1;
  // Either way, this statement may ONLY modify first
  *it = '0';
  cout << "s1 = " << s1 << endl;
  cout << "s2 = " << s2 << endl;
} ///:~


Reference counting may serve to make an implementation more memory efficient, but it is transparent to users of the string class.

In other words, it depends on how it is implemented but possible...

Manipulating basic strings is probably easier in C++ than in Java, because of function overloading. Look here for some examples:

http://www.codeguru.com/cpp/tic/tic0179.shtml
Thanks Jod,

I done completely forgotten ALL my C++ skills whatever they were acquired during college,so had to ask somebody .I can't 'think' in C++ ,that's a big handicap,way too big but i have come to love Java.
marshia,
i done completed that program today.

import Acme.Crypto.*;

/* This program is dedicated to Ashish Kulkarni,my brother and fellow programmer
Well bro ,we did this ,so take over and do the next step by urself.
   
For this to work ,download the freely available Acme Crypto package at
http://www.acme.com/java/software/
if u want it please mail me at amitkulkarni@mailandnews.com,
and i will try to respond under 15 days.
*/

public class EncryptionTest
{
 private static final int ENC_SIZE = 8;
 private static byte zeroes[] = {0,0,0,0,0,0,0,0};
 private static byte in[] = new byte[ENC_SIZE];
 private static byte out[] = new byte[ENC_SIZE];
 private static byte tempArr[] = new byte[ENC_SIZE];
      
 static
 {
       zeroes = new byte[ENC_SIZE];
       for(int i=zeroes.length;--i >= 0;)
       {
             zeroes[i]=0;
       }
 }
      
 public static void main(String[] args) throws Exception
 {
       String unencryptedDataString = "This is unencrypted data String man,testing this out,savvy?";
     byte [] unencrypted_data = unencryptedDataString.getBytes();
       int unencryptLen = unencryptedDataString.length();

       //since 8 is the size of encrypt/decrypt methods
       //of Des3Cipher,we need to pad the original
       //unencrypted byte array

       unencryptLen += (ENC_SIZE-(unencryptLen%ENC_SIZE));
       byte [] encryptedData = new byte[unencryptLen];
       byte [] decryptedData = new byte[unencryptLen];

       //instantiate Des3Cipher with some fixed key
       Des3Cipher cipher= new Des3Cipher("0123456789abcdef");
       for(int eIndx=0;eIndx<unencrypted_data.length;eIndx+=ENC_SIZE )
       {
             in=smartCopy(unencrypted_data,eIndx,in,0,ENC_SIZE);
             cipher.encrypt(in , 0 , tempArr ,0);
             //flush
             smartCopy(tempArr,0,encryptedData,eIndx,ENC_SIZE);
             System.arraycopy(zeroes,0,in,0,ENC_SIZE);
             System.arraycopy(zeroes,0,tempArr,0,ENC_SIZE);
       }

       //This proves that at one side u encrypt with a special key
       //at other u use the same one to decrypt,
       //Uh-Oh ,u gotta keep da key in a Seeeecure place man.
       cipher=null;
       cipher= new Des3Cipher("0123456789abcdef");
            
       for(int dIndx=encryptedData.length;dIndx>=0;dIndx-=ENC_SIZE )
       {
             out=smartCopy(encryptedData,dIndx,out,0,ENC_SIZE);
             cipher.decrypt(out , 0 ,tempArr  ,0);
             smartCopy(tempArr,0,decryptedData,dIndx,ENC_SIZE);
             //flush
             System.arraycopy(zeroes,0,out,0,ENC_SIZE);
             System.arraycopy(zeroes,0,tempArr,0,ENC_SIZE);
       }
            
       //to do a trim is very IMPORTANT,since we havta remove padded spaces.
       System.out.println("\nFINALLY decryptedData='"+new String(decryptedData).trim()+"'");
 }
      
 private static final byte[] smartCopy(byte [] src,int srcPos,byte [] dst,int dstPos,int len)
 {
       int lenSrcArr=src.length;
       int lenDstArr=dst.length ;
       //encryption
       if(lenSrcArr > lenDstArr)
       {
             if((srcPos+len) > lenSrcArr)
                   System.arraycopy(src,srcPos,dst,0,lenSrcArr - srcPos);
             else
                   System.arraycopy(src,srcPos,dst,0,len);
       }
       else
       //decryption
       {
             if((dstPos+len) > lenDstArr)
                   System.arraycopy(src,0,dst,dstPos,lenDstArr - dstPos);
             else
                   System.arraycopy(src,0,dst,dstPos,len);
       }
       return dst;
 }
}
Avatar of marshia

ASKER

thanks a lot mbormann,
    you really deserve the points.

marshia :-)