aes解密

b1zrtrql  于 2021-07-12  发布在  Spark
关注(0)|答案(1)|浏览(517)

我有一个用aes128对称算法加密的文件。如何在spark中配置解码文件?
我试着像下面那样加载文件,然后调用自定义函数来进行解密。但解密总是失败。看起来加密的二进制内容在通过spark加载后发生了变化。如何避免?

val dec = spark.sparkContext.textFile("inputFile")
dec.coalesce(1, shuffle=true).saveAsTExtFile("path")

解密保存文件时出现异常

Input length must be multiple of 16 when decrypting with padded cipher
java.lang.SecurityException: Input length must be multiple of 16 when decrypting with padded cipher
tquggr8v

tquggr8v1#

据我所知,spark本身没有解密api,因此您必须逐行(或者在dataframes的情况下是逐单元)进行解密。
代码方面你可以做如下的事情,
假设示例中的文本文件,

val sparkSession = ???
import sparkSession.implicits._

// Read text file as data set
val df = sparkSession.read.text("/path/to/files/").as[String]

// Create UDF for decryption
import org.apache.spark.sql.functions.udf
import org.apache.spark.sql.expressions.UserDefinedFunction

def decrypt_text: UserDefinedFunction = {
udf((str: String) => {
 // Decryption logic
})
}

// Use UDF to decrypt
val decDF = df.withColumn("decrpytedText", decrypt_text(col("value")))

// View results
decDF.show(false)

相关问题