2017-07-26 37 views
0

我有在Java中的波纹管代码,你能帮我转换成scala。源数据有三列(col1,col2,encryptedmessage)。以下代码解码加密的消息并将三个输入列保存为输出列。但在斯卡拉,我会撒谎有一个函数,它采用加密的消息,并产生解码值出COL1和COL2。从java到scala的代码:消息解密

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; 
} 
+0

首先,这不是'Java'代码。你的方法和领域是在课外宣布的。 – talex

+0

此加密不安全。它使用固定的IV和低熵密钥。 – erickson

回答

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 
     } 
    } 
    } 
}