All,

I am testing the 7.1 ENCRYPT function (Unidata). Previous to 7.1 you could
not encrypt in a language such as C# and have the Unibasic ENCRYPT function
decrypt a string. I believe that IBM has fixed this in 7.16 therefore the
test.

My problem is that (sad to say unlike C# but now I must use Java) it is hard
to find code in Java to produce the base64 encrypted string IF YOU HAVE TO
USE THE FOLLOWING PARAMETERS.

KEY        = "1234ABCDEF&&ABCDEFGH1234"  - Length = 24
KEY.SALT   = "01234567"                  - Length = 8
IV         = "1ABCDEF1"                  - Length = 8

What I am trying to do is produce an encrypted string in Java using the
above paramerters and give it to Unidata decrypt it (that program is written
and working).

I also have to base64 encode the encrypted string.

The following code is what I have so far - can someone help or point me to
another newsgroup. Notice that I have not found out how to pass in the salt
value.


    String instr = "This is the string to be Encrypted";
    String b64es;
    String decstr;
    byte[] encrypted;
    byte[] toEncrypt;
    byte[] fromEncrypt;
    byte[] es;
    
    byte[] myKey = "1234ABCDEF&&ABCDEFGH1234".getBytes();
    byte[] iv    = "1ABCDEF1".getBytes();
   
    try
    {
      System.out.println("Start of Encryption Test");
      
      // Prepare for Encryption
      IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
      
      
      DESedeKeySpec keySpec = new DESedeKeySpec(myKey);
      SecretKeyFactory secretKeyFactory =
SecretKeyFactory.getInstance("DESede");
      SecretKey secretKey = secretKeyFactory.generateSecret(keySpec); 
      
      Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
      cipher.init(Cipher.ENCRYPT_MODE,secretKey);
      // cipher.init(arg0, secretKey, ivParameterSpec)
                        
      // Get encrypted array of bytes.
      toEncrypt = instr.getBytes();
                        
     // Encrypts byte data
      encrypted = cipher.doFinal(toEncrypt);

     int keySizeInBits = keySpec.DES_EDE_KEY_LEN;
     System.out.println("Effective key size is " + keySizeInBits + "
bits.\n");
                        
     // get the cipher algorithm
     String alg = cipher.getAlgorithm();
     System.out.println("Algorithm " + alg);
//                      
//      // get the Key
//      System.out.println("Input Key : " + new String(key.getEncoded()));
//                      
//      // get the IV
//      System.out.println("Input IV : " + new String(cipher.getIV()) +
"\n");
//                      
      // Base 64 Encode the encrypted string
      b64es = new BASE64Encoder().encodeBuffer(encrypted);
      System.out.println("Base64 encrypted output : " + b64es);
                        
      // decrypt
      // Convert base64 encrypted string to encrypted byte array 
      es = new BASE64Decoder().decodeBuffer(b64es);

      // Initializes thE cipher
      cipher.init(Cipher.DECRYPT_MODE,secretKey);    
                        
      // decrypt byte array
      fromEncrypt = cipher.doFinal(es);
                        
      // convert byte array into string
      decstr = new String(fromEncrypt);
      System.out.println("Input string : " + instr);
      System.out.println("Output string : " + decstr);
      
    }
    catch (Exception e)
    {
      System.out.println("Error : " + e);
    }
  }
}



Thanks
george
-------
u2-users mailing list
[email protected]
To unsubscribe please visit http://listserver.u2ug.org/

Reply via email to