当前位置: 首页>>代码示例>>Scala>>正文


Scala Cipher类代码示例

本文整理汇总了Scala中javax.crypto.Cipher的典型用法代码示例。如果您正苦于以下问题:Scala Cipher类的具体用法?Scala Cipher怎么用?Scala Cipher使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Cipher类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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")
  }
} 
开发者ID:knoldus,项目名称:knolx-portal,代码行数:28,代码来源:EncryptionUtility.scala

示例2: RSAUtils

//设置package包名称以及导入依赖的类
package com.wix.pay.twocheckout.tokenization

import java.security.KeyFactory
import java.security.spec.RSAPublicKeySpec
import javax.crypto.Cipher

import org.apache.commons.codec.binary.Base64

object RSAUtils {

  def rsaEncrypt(key: RSAPublicKey, text: String) = {
    val modulus = BigInt(1, Base64.decodeBase64(key.base64Modulus))
    val exp = BigInt(1, Base64.decodeBase64(key.base64Exp))

    val publicKeySpec = new RSAPublicKeySpec(modulus.bigInteger, exp.bigInteger)
    val publicKey = KeyFactory.getInstance("RSA").generatePublic(publicKeySpec)

    val cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING")
    cipher.init(Cipher.ENCRYPT_MODE, publicKey)
    val res = cipher.doFinal(text.getBytes("UTF-8"))
    Base64.encodeBase64String(res)
  }
}

case class RSAPublicKey(base64Modulus: String, base64Exp: String) 
开发者ID:wix,项目名称:libpay-2checkout,代码行数:26,代码来源:RSAUtils.scala

示例3: RSAUtilsTest

//设置package包名称以及导入依赖的类
package com.wix.pay.twocheckout.tokenization

import java.security.spec.RSAPublicKeySpec
import java.security.{KeyFactory, KeyPairGenerator}
import javax.crypto.Cipher

import org.apache.commons.codec.binary.Base64
import org.specs2.mutable.SpecWithJUnit
import org.specs2.specification.Scope

class RSAUtilsTest extends SpecWithJUnit {

  "RSAUtils" should {

    "encrypt data with provided key in base64 format" in new Ctx {
      val encrypted = RSAUtils.rsaEncrypt(publicKey, someMessage)
      decryptBase64(encrypted) mustEqual someMessage
    }

    "encrypt empty string with provided key in base64 format" in new Ctx {
      val encrypted = RSAUtils.rsaEncrypt(publicKey, emptyMessage)
      decryptBase64(encrypted) mustEqual emptyMessage
    }
  }

  trait Ctx extends Scope {
    val factory = KeyPairGenerator.getInstance("RSA")
    factory.initialize(2048)

    val keys = factory.genKeyPair()
    val publicKeySpec = KeyFactory.getInstance("RSA").getKeySpec(keys.getPublic, classOf[RSAPublicKeySpec])
    val publicKey = RSAPublicKey(
      Base64.encodeBase64String(publicKeySpec.getModulus.toByteArray),
      Base64.encodeBase64String(publicKeySpec.getPublicExponent.toByteArray)
    )
    println(publicKey)
    val someMessage = "some message hello"
    val emptyMessage = ""

    def decryptBase64(encrypted: String): String = {
      val bytes = Base64.decodeBase64(encrypted)
      val cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING")
      cipher.init(Cipher.DECRYPT_MODE, keys.getPrivate)
      new String(cipher.doFinal(bytes), "utf-8")
    }
  }

} 
开发者ID:wix,项目名称:libpay-2checkout,代码行数:49,代码来源:RSAUtilsTest.scala

示例4: encrypt

//设置package包名称以及导入依赖的类
package models.user

import javax.crypto.Cipher
import javax.crypto.spec.SecretKeySpec
import org.apache.commons.codec.binary.Base64
import java.security.MessageDigest
import java.util.Arrays
import controllers.HasConfig

trait HasEncryption { self: HasConfig =>
  
  private val CIPHER = "AES/ECB/PKCS5Padding"
  
  private lazy val keySpec = self.config.getString("recogito.email.key").flatMap { key =>
    if (key.isEmpty) {
      None
    } else {
      val md = MessageDigest.getInstance("SHA-1")
      val keyDigest = md.digest(key.getBytes)
      Some(new SecretKeySpec(Arrays.copyOf(keyDigest, 16), "AES"))
    }
  }
  
  def encrypt(plaintext: String) = keySpec match {
    case Some(spec) => {
      val cipher = Cipher.getInstance(CIPHER)
      cipher.init(Cipher.ENCRYPT_MODE, spec)
      Base64.encodeBase64String(cipher.doFinal(plaintext.getBytes("UTF-8")))
    }
    
    case None => plaintext
  }
  
  def decrypt(encrypted: String) = keySpec match {
    case Some(spec) => {
      val cipher = Cipher.getInstance(CIPHER)
      cipher.init(Cipher.DECRYPT_MODE, spec)
      new String(cipher.doFinal(Base64.decodeBase64(encrypted)))      
    }
    
    case None => encrypted
  }
  
} 
开发者ID:pelagios,项目名称:recogito2,代码行数:45,代码来源:HasEncryption.scala

示例5: update

//设置package包名称以及导入依赖的类
package com.spooky.cipher

import javax.crypto.spec.SecretKeySpec
import javax.crypto.Cipher
import java.nio.ByteBuffer
import spooky.util.ByteString
import akka.util.FakeBStrings

sealed trait WriteCipher {
  def update(bs: Array[Byte]): Array[Byte]
  def update(bb: ByteBuffer): ByteString
  def update(bb: ByteString): ByteString
  def updateToBytes(bb: ByteBuffer): Array[Byte]
}

class RC4WriteCipher(writeKey: SecretKeySpec) extends RC4Cipher(writeKey, true) with WriteCipher

object WritePlain extends WriteCipher {
  def update(bb: ByteBuffer): ByteString = FakeBStrings(bb.duplicate)

  def update(bs: Array[Byte]): Array[Byte] = bs

  def update(bb: ByteString): ByteString = bb

  def updateToBytes(bb: ByteBuffer): Array[Byte] = bb.array
} 
开发者ID:zpooky,项目名称:bittorrent,代码行数:27,代码来源:WriteCipher.scala

示例6: EncryptionContext

//设置package包名称以及导入依赖的类
package client

import java.security.{Key, PublicKey}
import javax.crypto.{KeyGenerator, Cipher}

import com.cryptoutility.protocol.crypto.{Base64Encode, Encrypt}
import play.api.Configuration

class EncryptionContext (publicKey: PublicKey, config: Configuration) {
  
  val symmetricAlgorithm = config.getString("cipher.symmetric.algorithm").get
  val asymmetricAlgorithm = config.getString("cipher.asymmetric.algorithm").get
  val secretKeyAlgorithm = config.getString("cipher.symmetric.key.algorithm").get

  def encrypt = Encrypt(symmetricAlgorithm, identity)(_, _)

  def cipher = {
    val key = secret
    val c = Cipher.getInstance(symmetricAlgorithm)
    c.init(Cipher.ENCRYPT_MODE, key)
    (key, c)
  }

  def secret = KeyGenerator.getInstance(secretKeyAlgorithm).generateKey()

  def encryptEncoded = Encrypt(symmetricAlgorithm, Base64Encode(_))(_, _)
  def wrap = Encrypt.wrap(asymmetricAlgorithm,  publicKey, Base64Encode(_))(_)
} 
开发者ID:ejosiah,项目名称:crypto-utility,代码行数:29,代码来源:EncryptionContext.scala

示例7: Encryptor

//设置package包名称以及导入依赖的类
package streams

import java.security.Key
import java.security.spec.AlgorithmParameterSpec
import javax.crypto.Cipher

import akka.stream.{Outlet, Inlet, Attributes, FlowShape}
import akka.stream.stage.{OutHandler, InHandler, GraphStageLogic, GraphStage}
import akka.util.ByteString


class Encryptor(cipher: Cipher)
  extends GraphStage[FlowShape[ByteString, ByteString]]{

  val in = Inlet[ByteString]("Encryptor.in")
  val out = Outlet[ByteString]("Encryptor.out")

  def shape: FlowShape[ByteString, ByteString] = FlowShape.of(in, out)

  def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) {
    setHandler(in, new InHandler {
      override def onPush(): Unit = {
        val chunk = grab(in).toArray
        val encrypted = cipher.update(chunk)
        emit(out, ByteString(encrypted))

      }

      override def onUpstreamFinish(): Unit = {
        val last = cipher.doFinal()
        emit(out, ByteString(last))
        completeStage()
      }
    })

    setHandler(out, new OutHandler {
      override def onPull(): Unit =  pull(in)
    })
  }
} 
开发者ID:ejosiah,项目名称:crypto-utility,代码行数:41,代码来源:Encryptor.scala

示例8: Crypto

//设置package包名称以及导入依赖的类
package com.softwaremill.session

import java.security.MessageDigest
import java.util
import javax.crypto.{Cipher, Mac}
import javax.crypto.spec.SecretKeySpec
import javax.xml.bind.DatatypeConverter

import com.softwaremill.session.SessionUtil._

object Crypto {
  def sign_HmacSHA1_hex(message: String, secret: String): String = {
    val key = secret.getBytes("UTF-8")
    val mac = Mac.getInstance("HmacSHA1")
    mac.init(new SecretKeySpec(key, "HmacSHA1"))
    toHexString(mac.doFinal(message.getBytes("utf-8")))
  }

  def sign_HmacSHA256_base64(message: String, secret: String): String = {
    val key = secret.getBytes("UTF-8")
    val mac = Mac.getInstance("HmacSHA256")
    mac.init(new SecretKeySpec(key, "HmacSHA256"))
    DatatypeConverter.printBase64Binary(mac.doFinal(message.getBytes("utf-8")))
  }

  def encrypt_AES(value: String, secret: String): String = {
    val raw = util.Arrays.copyOf(secret.getBytes("utf-8"), 16)
    val skeySpec = new SecretKeySpec(raw, "AES")
    val cipher = Cipher.getInstance("AES")
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec)
    toHexString(cipher.doFinal(value.getBytes("utf-8")))
  }

  def decrypt_AES(value: String, secret: String): String = {
    val raw = util.Arrays.copyOf(secret.getBytes("utf-8"), 16)
    val skeySpec = new SecretKeySpec(raw, "AES")
    val cipher = Cipher.getInstance("AES")
    cipher.init(Cipher.DECRYPT_MODE, skeySpec)
    new String(cipher.doFinal(hexStringToByte(value)))
  }

  def hash_SHA256(value: String): String = {
    val digest = MessageDigest.getInstance("SHA-256")
    toHexString(digest.digest(value.getBytes("UTF-8")))
  }
} 
开发者ID:adamw,项目名称:testpr,代码行数:47,代码来源:Crypto.scala

示例9: 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)
} 
开发者ID:clouderite,项目名称:scala-berries,代码行数:27,代码来源:CryptoOperations.scala

示例10: 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)
  }
} 
开发者ID:kdrakon,项目名称:scala-credstash,代码行数:37,代码来源:AESEncryption.scala

示例11: 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)
  }

} 
开发者ID:EVENFinancial,项目名称:sbt-secrets,代码行数:44,代码来源:AesUtil.scala

示例12: EncryptTool

//设置package包名称以及导入依赖的类
package com.github.cuzfrog.utils

import javax.crypto.spec.SecretKeySpec
import javax.crypto.Cipher
import java.util.Base64

private[cuzfrog] object EncryptTool {

  def decrypt(encrypted: Array[Byte], key: Array[Byte]): Array[Byte] = {
    // Create key and cipher
    val aesKey = new SecretKeySpec(key, "AES")
    val cipher = Cipher.getInstance("AES")

    // decrypt the text
    cipher.init(Cipher.DECRYPT_MODE, aesKey)
    cipher.doFinal(encrypted)
  }

  def encrypt(input: Array[Byte], key: Array[Byte]): Array[Byte] = {
    // Create key and cipher
    val aesKey = new SecretKeySpec(key, "AES")

    val cipher = Cipher.getInstance("AES")
    // encrypt the text
    cipher.init(Cipher.ENCRYPT_MODE, aesKey)
    cipher.doFinal(input)

  }

  def encryptToBase64(input: String, key: String, charset: String = "utf8"): String = {
    val in = input.getBytes(charset)
    val k = key.getBytes(charset)
    encryptToBase64(in, k)
  }

  def encryptToBase64(input: Array[Byte], key: Array[Byte]): String = {
    val encrypted = encrypt(input, key)
    val encoded = Base64.getEncoder.encodeToString(encrypted)
    encoded
  }

  def decryptFromBase64(input: String, key: String, charset: String = "utf8"): String = {
    val k = key.getBytes(charset)
    new String(decryptFromBase64(input, k), charset)
  }

  def decryptFromBase64(input: String, key: Array[Byte]): Array[Byte] = {
    val decoded = Base64.getDecoder.decode(input)
    val decrypted = decrypt(decoded, key)
    decrypted
  }
} 
开发者ID:cuzfrog,项目名称:excela,代码行数:53,代码来源:EncryptTool.scala

示例13: EncryptTool

//设置package包名称以及导入依赖的类
package com.github.cuzfrog.utils

import java.util.Base64
import javax.crypto.spec.SecretKeySpec
import javax.crypto.Cipher

import jdk.internal.util.xml.impl.Input

private[cuzfrog] object EncryptTool {


  def decrypt(encrypted: Array[Byte], key: Array[Byte]): Array[Byte] = {
    // Create key and cipher
    val aesKey = new SecretKeySpec(key, "AES")
    val cipher = Cipher.getInstance("AES")

    // decrypt the text
    cipher.init(Cipher.DECRYPT_MODE, aesKey)
    cipher.doFinal(encrypted)
  }

  def encrypt(input: Array[Byte], key: Array[Byte]): Array[Byte] = {
    // Create key and cipher
    val aesKey = new SecretKeySpec(key, "AES")

    val cipher = Cipher.getInstance("AES")
    // encrypt the text
    cipher.init(Cipher.ENCRYPT_MODE, aesKey)
    cipher.doFinal(input)
  }

  def encryptToBase64(input: String, key: String, charset: String = "utf8"): String = {
    val in = input.getBytes(charset)
    val k = key.getBytes(charset)
    encryptToBase64(in, k)
  }

  def encryptToBase64(input: Array[Byte], key: Array[Byte]): String = {
    val encrypted = encrypt(input, key)
    val encoded = Base64.getEncoder.encodeToString(encrypted)
    encoded
  }

  def decryptFromBase64(input: String, key: String, charset: String = "utf8"): String = {
    val k = key.getBytes(charset)
    new String(decryptFromBase64(input, k), charset)
  }

  def decryptFromBase64(input: String, key: Array[Byte]): Array[Byte] = {
    val decoded = Base64.getDecoder.decode(input)
    val decrypted = decrypt(decoded, key)
    decrypted
  }

} 
开发者ID:cuzfrog,项目名称:maila,代码行数:56,代码来源:EncryptTool.scala

示例14: 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")
  }
} 
开发者ID:knockdata,项目名称:spark-highcharts,代码行数:40,代码来源:CipherUtil.scala

示例15: AES

//设置package包名称以及导入依赖的类
package myscala.util

import java.security.MessageDigest
import java.util
import java.util.Base64
import javax.crypto.Cipher
import javax.crypto.spec.SecretKeySpec

object AES {

    def encrypt(strToEncrypt: String, secret: String): String = {
        val secretKey = getSecretKeySpec(secret)
        val cipher = Cipher.getInstance("AES/ECB/PKCS5Padding")
        cipher.init(Cipher.ENCRYPT_MODE, secretKey)
        Base64.getEncoder.encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")))
    }

    def decrypt(strToDecrypt: String, secret: String): String = {
        val secretKey = getSecretKeySpec(secret)
        val cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING")
        cipher.init(Cipher.DECRYPT_MODE, secretKey)
        new String(cipher.doFinal(Base64.getDecoder.decode(strToDecrypt)))
    }

    private def getSecretKeySpec(myKey: String): SecretKeySpec = {
        var key = myKey.getBytes("UTF-8")
        val sha = MessageDigest.getInstance("SHA-1")
        key = sha.digest(key)
        key = util.Arrays.copyOf(key, 16)
        new SecretKeySpec(key, "AES")
    }

} 
开发者ID:yingzhuo,项目名称:myscala,代码行数:34,代码来源:AES.scala


注:本文中的javax.crypto.Cipher类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。