ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

JAVA/RPG Encrypting/Encoding

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • JAVA/RPG Encrypting/Encoding

    I have a client that needs to send an encrypted/encoded string of data to vendor to gain access to an web application. I'm using RPG to accomplish this and everything is working fine but my encrypted/encoded string is not being accepted by my client's vendor. I'm using the QC3ENCDT encryption API and Scott's Klement's BASE64 encoding program and it's not giving me what the vendor's needs to see. I'm not real sure I have all the parameters set correctly on the QC3ENCDT to match the JAVA sample provided below. The vendor has supplied the JAVA code snippet to handle the encryption/encoding, but not being a JAVA programmer, I don't know if I create this code on the ISeries as I know nothing about JAVA. The vendor is having concerns if the AS400 can accomplish this and I know it can if the programmer was somewhat smarter. If anyone could help create this on the AS400 so that I call it from an RPG program or help in configuring the QC3ENCDT API, I would be willing to pay for your time. My clinet's OS version is V5R3.

    Below is the JAVA code snippet provided by the vendor...

    Code:
    public String encrypt(String cleartext, String key, String alg)    {
                String encrpytedString = null;
                byte[] ciphertext = null;
    
                try
                  {
                       byte[] raw = new BASE64Decoder().decodeBuffer(key);
                       SecretKeySpec skeySpec = new SecretKeySpec(raw, alg);
                       //use some padding parameters to avoid Bad Padding exception
                       Cipher desCipher = Cipher.getInstance(alg + "/ECB/PKCS5Padding");
                       
                      desCipher.init(Cipher.ENCRYPT_MODE, skeySpec);
                      ciphertext = desCipher.doFinal(cleartext.getBytes("UTF8"));
    
                      //do not convert byte[] to String directly. user BASE64 instead.
                      //A base64 encoded byte array only contains displayable characters.
                      //The returned byte array may contain control characters
                      //or other characters that should not be present in a string
                      encrpytedString = new sun.misc.BASE64Encoder().encode (ciphertext);
                  }
                  catch (Exception e) {
                      e.printStackTrace(System.out);
                  }
                  return encrpytedString;
            }
    Thanks!

  • #2
    Re: JAVA/RPG Encrypting/Encoding

    How to include source code in your posts.
    "Time passes, but sometimes it beats the <crap> out of you as it goes."

    Comment


    • #3
      Re: JAVA/RPG Encrypting/Encoding

      Code:
      	public String encrypt(String cleartext, String key, String alg) {
      		String encrpytedString = null;
      		byte[] ciphertext = null;
      
      		try {
      			byte[] raw = new BASE64Decoder().decodeBuffer(key);
      			SecretKeySpec skeySpec = new SecretKeySpec(raw, alg);
      			//use some padding parameters to avoid Bad Padding exception
      			Cipher desCipher = Cipher.getInstance(alg + "/ECB/PKCS5Padding");
      
      			desCipher.init(Cipher.ENCRYPT_MODE, skeySpec);
      			ciphertext = desCipher.doFinal(cleartext.getBytes("UTF8"));
      
      			//do not convert byte[] to String directly. user BASE64 instead.
      			//A base64 encoded byte array only contains displayable characters.
      			//The returned byte array may contain control characters
      			//or other characters that should not be present in a string
      			encrpytedString = new sun.misc.BASE64Encoder().encode (ciphertext);
      		}
      		catch (Exception e) {
      			e.printStackTrace(System.out);
      		}
      		return encrpytedString;
      	}
      "Time passes, but sometimes it beats the <crap> out of you as it goes."

      Comment


      • #4
        Re: JAVA/RPG Encrypting/Encoding

        Hello

        Here is RPG to do it:

        Comment


        • #5
          Re: JAVA/RPG Encrypting/Encoding

          I had posted this over on another board as well, and Scott Klement is the man! After a few messages back and forth, it works! I would like to thank everyone here who helped as well. The vendor, who is a one of the major wireless companies, can't believe I'm doing this using RPG and AS400/ISeries. I told them the AS400/ISeries community is tight knit group and loves to proved the so called experts wrong.

          Again, thanks and have a Merry Christmas!

          Comment


          • #6
            Hi Tom, i have the same project as you. The problem is that the encrypted password with base64 encoded is incorrect and return code 404 as not authorized. May I know the program that you said it’s already working. I can send you mine if you allow me. Hoping for your reply on this. Thank you.

            Comment

            Working...
            X