从java到scala的代码:消息解密

时间:2017-07-26 12:11:45

标签: java scala

我在Java中有下面的代码,请你帮我转换成scala。源数据有三列(col1,col2,encryptedmessage)。下面的代码解码加密的消息并将三个输入列保存到输出列。但是在scala中,我会说谎有一个函数,它接收加密的消息并产生没有COL1和COL2的解码值。

import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;


private int cryptographyKeySize=0;
private String cryptographyMethod=null;
private     KeyGenerator kgen = null;
private int secretKeyFieldIndex=0;
private int myDecryptedSecretFieldIndex=0;

private int encryptedTextFieldIndex1=0;
private int encryptedTextFieldIndex2=0;


private int COL1Index1 =0;
private int COL2Index1 =0;
private int COL1Index2 =0;
private int COL2Index2 =0;
private String symmKey = "9d6ea4d3e6f8c4f8";
private String ivSpec = "1c5dd32d7ba54bdd";


public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException 
{
    Object[] r = getRow(); 
    byte[] decrypted = null;
    byte[] raw = null;
    SecretKeySpec skeySpec = null;
    Cipher cipher = null;
    String exMessage = null;
    boolean isException = false;

    if (r==null)
    {
        setOutputDone();
        return false;
    }


    if(!isException){
        Object[] outputRowData = createOutputRow(r, data.outputRowMeta.size());


        try {

            raw = symmKey.getBytes("UTF-8");        
            skeySpec = new SecretKeySpec(raw, "AES");   
            cipher = Cipher.getInstance("AES/CBC/ISO10126PADDING");
            IvParameterSpec ivParameterSpec = new IvParameterSpec(ivSpec.getBytes("UTF-8"));
            cipher.init(Cipher.DECRYPT_MODE,skeySpec,ivParameterSpec);  
            byte[] decordedValue = new BASE64Decoder().decodeBuffer((String)r[encryptedTextFieldIndex1]);
            decrypted = cipher.doFinal(decordedValue);                  
            outputRowData[myDecryptedSecretFieldIndex]=new String(decrypted);
            outputRowData[COL1Index2]=r[COL1Index1];
            outputRowData[COL2Index2]=r[COL2Index1];
            outputRowData[encryptedTextFieldIndex2]=r[encryptedTextFieldIndex1];

            putRow(data.outputRowMeta, outputRowData);
        } catch (Exception e) {
            exMessage = "failed during decryption::"+(String)r[encryptedTextFieldIndex1];
            isException = true;
        }

    }

    return true;
}

1 个答案:

答案 0 :(得分:0)

缺少相当多的上下文,代码似乎有点混乱,似乎也有很多副作用。这些不是代码scala的非常好的先决条件。但这将是您的代码的一个相当规范的翻译:

import java.security._
import javax.crypto._
import javax.crypto.spec._
import sun.misc.BASE64Decoder
import sun.misc.BASE64Encoder

object Main {
  private val cryptographyKeySize: Int = 0
  private val cryptographyMethod: String = null
  private val kgen: KeyGenerator = null
  private val secretKeyFieldIndex: Int = 0
  private val myDecryptedSecretFieldIndex: Int = 0

  private val encryptedTextFieldIndex1: Int = 0
  private val encryptedTextFieldIndex2: Int = 0


  private val COL1Index1: Int = 0
  private val COL2Index1: Int = 0
  private val COL1Index2: Int = 0
  private val COL2Index2: Int = 0
  private val symmKey: String = "9d6ea4d3e6f8c4f8"
  private val ivSpec: String = "1c5dd32d7ba54bdd"

  def processRow(smi: StepMetaInterface, sdi: StepDataInterface): Boolean = {
    val r: Array[Any] = getRow()

    if (r == null) {
      setOutputDone()
      false
    } else {
        val outputRowData: Array[Any] = createOutputRow(r, data.outputRowMeta.size())

        try {
          val raw: Array[Byte] = symmKey.getBytes("UTF-8")
          val skeySpec: SecretKeySpec = new SecretKeySpec(raw, "AES")
          val cipher: Cipher = Cipher.getInstance("AES/CBC/ISO10126PADDING")
          val ivParameterSpec: IvParameterSpec = new IvParameterSpec(ivSpec.getBytes("UTF-8"))
          cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParameterSpec)

          val ecordedValue: Array[Byte] = new BASE64Decoder().decodeBuffer(r[encryptedTextFieldIndex1].asInstanceOf[String])
          val decryped: Array[Byte] = cipher.doFinal(decordedValue)
          outputRowData[myDecryptedSecretFieldIndex] = new String(decrypted)
          outputRowData[COL1Index2] = r[COL1Index1]
          outputRowData[COL2Index2] = r[COL2Index1]
          outputRowData[encryptedTextFieldIndex2] = r[encryptedTextFieldIndex1]

          putRow(data.outputRowMeta, outputRowData)
        } catch {
          case t: Throwable => 
              val exMessage: String = "failed during decryption::" + r[encryptedTextFieldIndex1].asInstanceOf[String]
        }
      true
      }
    }
  }
}