本文整理汇总了Scala中javax.crypto.spec.IvParameterSpec类的典型用法代码示例。如果您正苦于以下问题:Scala IvParameterSpec类的具体用法?Scala IvParameterSpec怎么用?Scala IvParameterSpec使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了IvParameterSpec类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: EncryptionUtility
//设置package包名称以及导入依赖的类
package utilities
import java.util.Base64
import javax.crypto.Cipher
import javax.crypto.spec.{IvParameterSpec, SecretKeySpec}
object EncryptionUtility {
private val Algorithm = "AES/CBC/PKCS5Padding"
private val Key = new SecretKeySpec(Base64.getDecoder.decode("DxVnlUlQSu3E5acRu7HPwg=="), "AES")
private val IvSpec = new IvParameterSpec(new Array[Byte](16))
val AdminKey = "Dx$V!nl%Ul^QS&u3*[email protected]=="
def encrypt(text: String): String = {
val cipher = Cipher.getInstance(Algorithm)
cipher.init(Cipher.ENCRYPT_MODE, Key, IvSpec)
new String(Base64.getEncoder.encode(cipher.doFinal(text.getBytes("utf-8"))), "utf-8")
}
def decrypt(text: String): String = {
val cipher = Cipher.getInstance(Algorithm)
cipher.init(Cipher.DECRYPT_MODE, Key, IvSpec)
new String(cipher.doFinal(Base64.getDecoder.decode(text.getBytes("utf-8"))), "utf-8")
}
}
示例2: decrypt
//设置package包名称以及导入依赖的类
package io.policarp.scala.credstash
import javax.crypto.Cipher
import javax.crypto.spec.{ IvParameterSpec, SecretKeySpec }
trait AESEncryption {
def decrypt(key: Array[Byte], encryptedValue: Array[Byte]): Array[Byte]
}
object DefaultAESEncryption extends AESEncryption {
private def cipher(key: Array[Byte], encryptMode: Int) = {
val cipher = Cipher.getInstance("AES/CTR/NoPadding")
// based on Python's Crypto.Cipher.AES and Crypto.Util.Counter
val blockSize = cipher.getBlockSize
// ref: https://pythonhosted.org/pycrypto/Crypto.Util.Counter-module.html
// Python default is Big Endian
val counter = Array[Byte](1).padTo(blockSize, 0.toByte).reverse
val ivParameterSpec = new IvParameterSpec(counter)
cipher.init(encryptMode, keyToSpec(key), ivParameterSpec)
cipher
}
def keyToSpec(key: Array[Byte]): SecretKeySpec = new SecretKeySpec(key, "AES")
def encrypt(key: Array[Byte], value: Array[Byte]): Array[Byte] = {
cipher(key, Cipher.ENCRYPT_MODE).doFinal(value)
}
def decrypt(key: Array[Byte], encryptedValue: Array[Byte]): Array[Byte] = {
cipher(key, Cipher.DECRYPT_MODE).doFinal(encryptedValue)
}
}
示例3: CryptoOperations
//设置package包名称以及导入依赖的类
package io.clouderite.commons.scala.berries.crypto
import java.util.Base64
import javax.crypto.Cipher
import javax.crypto.spec.{IvParameterSpec, SecretKeySpec}
class CryptoOperations(text: String) {
private val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
def encrypt(key: String, iv: String): String = {
cipher.init(Cipher.ENCRYPT_MODE, keySpec(key), paramSpec(iv))
Base64.getEncoder.encodeToString(cipher.doFinal(text.getBytes()))
}
def decrypt(key: String, iv: String): String = {
cipher.init(Cipher.DECRYPT_MODE, keySpec(key), paramSpec(iv))
new String(cipher.doFinal(Base64.getDecoder.decode(text)))
}
def keySpec(key: String) = new SecretKeySpec(key.getBytes(), "AES")
def paramSpec(iv: String) = new IvParameterSpec(iv.getBytes())
}
object CryptoOperations {
implicit def toCryptoOperations(text: String): CryptoOperations = new CryptoOperations(text)
}
示例4: AesUtil
//设置package包名称以及导入依赖的类
package com.evenfinancial.sbt.secrets.util
import java.util.Base64
import java.security.MessageDigest
import javax.crypto.Cipher
import javax.crypto.spec.{IvParameterSpec, SecretKeySpec}
// Basically copied from the Play framework's Crypto library.
// @see https://github.com/playframework/playframework/blob/2.4.x/framework/src/play/src/main/scala/play/api/libs/Crypto.scala
object AesUtil {
def encrypt(data: String, dataKey: String): String = {
val secretKey = buildSecretKey(dataKey)
val cipher = buildCipher()
cipher.init(Cipher.ENCRYPT_MODE, secretKey)
val encrypted = cipher.doFinal(data.getBytes("UTF-8"))
val result = cipher.getIV() ++ encrypted
Base64.getEncoder.encodeToString(result)
}
def decrypt(data: String, dataKey: String): String = {
val secretKey = buildSecretKey(dataKey)
val bytes = Base64.getDecoder.decode(data)
val cipher = buildCipher()
val iv = bytes.slice(0, cipher.getBlockSize)
val payload = bytes.slice(cipher.getBlockSize, bytes.size)
cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv))
new String(cipher.doFinal(payload), "utf-8")
}
private def buildCipher() = Cipher.getInstance("AES/CTR/NoPadding")
private def buildSecretKey(dataKey: String) = {
val algorithm = "AES"
val messageDigest = MessageDigest.getInstance("SHA-256")
messageDigest.update(dataKey.getBytes("utf-8"))
// max allowed length in bits / (8 bits to a byte)
val maxAllowedKeyLength = Cipher.getMaxAllowedKeyLength(algorithm) / 8
val raw = messageDigest.digest().slice(0, maxAllowedKeyLength)
new SecretKeySpec(raw, algorithm)
}
}
示例5: CipherUtil
//设置package包名称以及导入依赖的类
package com.knockdata.spark.utils
import java.security.MessageDigest
import javax.crypto.Cipher
import javax.crypto.spec.{IvParameterSpec, SecretKeySpec}
import sun.misc.{BASE64Decoder, BASE64Encoder}
object CipherUtil {
val cipherName = "AES/CBC/PKCS5Padding"
val ivspec = new IvParameterSpec(Array[Byte](0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
def getKey(key: String): Array[Byte] = {
val raw = MessageDigest.getInstance("MD5").digest(key.getBytes)
raw
}
def encrypt(key: String, password: String): String = {
val spec = new SecretKeySpec(getKey(key), "AES")
val cipher = Cipher.getInstance(cipherName)
cipher.init(Cipher.ENCRYPT_MODE, spec, ivspec)
val encrypted = cipher.doFinal(password.getBytes("UTF8"))
new BASE64Encoder().encode(encrypted)
}
def decrypt(key: String, encryptedPassword: String): String = {
val spec = new SecretKeySpec(getKey(key), "AES")
val cipher = Cipher.getInstance(cipherName)
cipher.init(Cipher.DECRYPT_MODE, spec, ivspec)
val encrypted = new BASE64Decoder().decodeBuffer(encryptedPassword)
val decrypted = cipher.doFinal(encrypted)
new String(decrypted, "UTF8")
}
}