Topic: aes algorithm regarding

Hi

I was planning to convert Java code to ruby script

JAVACODE

private static final String PK = "037629a6";
    private static final String EK = "Td-b6vsXyJoeDzmCXe40GrT_B4fszAAA";

    private static String encrypt(String secret){
       String base64_encrypted_secret = null;
        System.out.println("Secret with key="+base64_encrypted_secret);

        String secret_with_key = String.format("%s:%s", secret, PK);
       System.out.println("Secret with key="+secret_with_key);

        String secret_with_padding = pad(secret_with_key);

        System.out.println("Secret with padding="+pad(secret_with_padding));

//        // Gen a 16 byte IV
        try {
            byte[] iv = new byte[]{
                        0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07
                        //0,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6
            };
       
            System.out.println("iv="+iv);

            byte[] ek = EK.getBytes();
           
           
            AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
           
            SecretKeySpec keyspec = new SecretKeySpec(Base64.decodeBase64(EK.getBytes()), "AES");   
             
            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            cipher.init(Cipher.ENCRYPT_MODE, keyspec, paramSpec);
            System.out.println("keyspec="+cipher);
           
            logln(String.format("secret_with_padding = %s", secret_with_padding));
             byte[] encrypted_value = cipher.doFinal(secret_with_padding.getBytes());
             logln(String.format("%s", encrypted_value));
             String encrypted_secret = String.format("%s%s", new String(iv), new String(encrypted_value));
             System.out.println("Encrypted Secret="+encrypted_secret);
             System.out.println("Decrypt"+decrypt(base64_encrypted_secret));
             logln(encrypted_secret);
             base64_encrypted_secret = Base64.encodeBase64String(encrypted_secret.getBytes());
        } catch(Exception e){
            e.printStackTrace();           
        }
//               
//        //
        return base64_encrypted_secret;
    }
       
    private static String pad(String data){
        int paddingBytesNeeded = (16 - (data.length() % 16));
        logln(String.format("paddingBytesNeeded = %d", paddingBytesNeeded));
       
        if(paddingBytesNeeded == 0) paddingBytesNeeded = 16;
        for (int i = 0; i < paddingBytesNeeded; i++) {
            data = String.format("%s%c", data, (char)paddingBytesNeeded);
            logln(data);
            System.out.println("Encrypted  data="+data);
        }
       
        //
        System.out.println("Encrypted padding="+data);
        return data;
    }

    RUBY CODE


require "openssl"
require "digest"
require 'base64'

module FuzeEncryption
  def FuzeEncryption.encrypt(secret)
    base64_encrypted_secret = nil
    puts base64_encrypted_secret

    #secret with key
    secret_with_key = "xxxxxxxxx:037629a6"
    puts secret_with_key

    #secret with padding
    secret_with_padding = FuzeEncryption.pad(secret_with_key)
    puts secret_with_padding

    #iv
    iv = [ 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07]
    puts iv.to_s

    #ek
    ek = "Td-b6vsXyJoeDzmCXe40GrT_B4fszAAA"
    keyspec = Base64.decode64(ek)
    puts  keyspec

    #cipher
    cipher = OpenSSL::Cipher::Cipher.new("aes-128-cbc")
    cipher.encrypt
    cipher.key = keyspec
    cipher.iv = iv.to_s
    puts cipher

    #encrypted value
    encrypted_value = ""
    encrypted_value =cipher.update(secret_with_padding)
    encrypted_value = cipher.final
    puts encrypted_value

    #encrypted secret
    encrypted_secret = "#{iv.to_s}#{encrypted_value.to_s}"
    base64_encrypted_secret = Base64.encode64(encrypted_secret)
    puts base64_encrypted_secret
  end
  def FuzeEncryption.pad(data)

    paddingBytesNeeded = (16 - (data.length % 16))
    unless paddingBytesNeeded == 0
      paddingBytesNeeded = 16
    end
    for i in 0..paddingBytesNeeded
      data = "#{data}#{paddingBytesNeeded}"
    end
    return data

  end
end


Can any body tell me what Am I doing wrong.

arti

Last edited by artificialman@yahoo.com (2012-12-10 15:29:59)