为什么我用RSA使用不同的纯文本获得相同的密码?

时间:2017-03-09 08:56:58

标签: scala encryption rsa

下面列出的是我的整个代码,

object RSA {
  def decodePublicKey(encodedKey: String):Option[PublicKey] = { 
    this.decodePublicKey(
      (new Base64()).decode(encodedKey)
    )   
  }
  def decodePublicKey(encodedKey: Array[Byte]): Option[PublicKey]= { 
    scala.util.control.Exception.allCatch.opt {
      val spec = new X509EncodedKeySpec(encodedKey)
      val factory = KeyFactory.getInstance("RSA")
      factory.generatePublic(spec)
    }   
  }
  def encrypt(data: String,key:PublicKey): Array[Byte] = { 
    val cipher = Cipher.getInstance("RSA")
    cipher.init(Cipher.ENCRYPT_MODE, key)
    val file=Source.fromFile(data)
    val stringWords=file.toArray
    val result=new String(stringWords)
    val words=result.getBytes()
    cipher.doFinal(words)
    }
  def main(args:Array[String]):Unit={
    println("The keys of RSA " + decodePublicKey("")
    val publicKey=decodePublicKey("")
    val cipher = encrypt("D:\\text.txt",publicKey.get)
    println("cipher is"+ cipher)
  }
}

我想要做的是用RSA加密明文。我的代码中没有语法错误。问题是当我运行它时,我在更改明文时得到了相同的密码(D:\ text.txt)。有谁知道原因?

0 个答案:

没有答案