Scala-为什么我不能使用MD5加密文件?
Scala-Why can't I use MD5 to encrypt a file?
我正在尝试使用 MD5.Listing 加密文件,下面是我的代码。
import java.security.MessageDigest
import scala.io.Source
import java.io.PrintWriter
import java.io.File
object MD5 {
def md5(file: String)= {
val text=Source.fromFile(file)
val s=text.mkString
val hash = MessageDigest.getInstance("MD5").digest(s.getBytes)
hash.map("%02x".format(_)).mkString
}
def main(args:Array[String])={
val cipher=md5("InputFile")
val pw = new PrintWriter(new File("OutputFile"))
pw.write(cipher)
pw.close
}}
无论我的InputFile有多大,OutputFile似乎总是一样大小(大约10个字节)。问题出在哪里?有人可以帮我弄清楚吗?
"ecnryption" 人们通常指的是这样一种数据转换,即具有适当 "secret data" 的人可以逆转该转换。
"Hash function" on the other hand is by definition non-uniquely reverisble transformation that transforms arbitratry data to a fixed-sized "hash". MD5 is an old cryptographic hash function 这意味着这是一个 "hash function" 具有额外的加密相关属性(例如很难找到碰撞)。旁注:MD5 已经足够老了,不再被认为是加密强的。 MD5 目前的典型用途是验证数据完整性。
所以是的,MD5 用于加密是错误的。如果你想加密,看看AES ciphers
我正在尝试使用 MD5.Listing 加密文件,下面是我的代码。
import java.security.MessageDigest
import scala.io.Source
import java.io.PrintWriter
import java.io.File
object MD5 {
def md5(file: String)= {
val text=Source.fromFile(file)
val s=text.mkString
val hash = MessageDigest.getInstance("MD5").digest(s.getBytes)
hash.map("%02x".format(_)).mkString
}
def main(args:Array[String])={
val cipher=md5("InputFile")
val pw = new PrintWriter(new File("OutputFile"))
pw.write(cipher)
pw.close
}}
无论我的InputFile有多大,OutputFile似乎总是一样大小(大约10个字节)。问题出在哪里?有人可以帮我弄清楚吗?
"ecnryption" 人们通常指的是这样一种数据转换,即具有适当 "secret data" 的人可以逆转该转换。
"Hash function" on the other hand is by definition non-uniquely reverisble transformation that transforms arbitratry data to a fixed-sized "hash". MD5 is an old cryptographic hash function 这意味着这是一个 "hash function" 具有额外的加密相关属性(例如很难找到碰撞)。旁注:MD5 已经足够老了,不再被认为是加密强的。 MD5 目前的典型用途是验证数据完整性。
所以是的,MD5 用于加密是错误的。如果你想加密,看看AES ciphers