本文整理汇总了Scala中java.security.KeyFactory类的典型用法代码示例。如果您正苦于以下问题:Scala KeyFactory类的具体用法?Scala KeyFactory怎么用?Scala KeyFactory使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了KeyFactory类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: 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)
示例2: 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")
}
}
}
示例3: Cipher
//设置package包名称以及导入依赖的类
package com.github.wildprairie.common.utils
import java.nio.file.Paths
import java.security.{PrivateKey, PublicKey}
object Cipher {
val RSA = new Cipher("RSA")
}
class Cipher(algorithm: String) {
import java.nio.file.Files
import java.security.KeyFactory
import java.security.spec.X509EncodedKeySpec
import java.security.spec.PKCS8EncodedKeySpec
def generatePrivate: PrivateKey = {
val keyBytes = Files.readAllBytes(Paths.get(getClass.getResource("/private_key.der").toURI))
val spec = new PKCS8EncodedKeySpec(keyBytes)
val kf = KeyFactory.getInstance(algorithm)
kf.generatePrivate(spec)
}
def generatePublic: PublicKey = {
val keyBytes = Files.readAllBytes(Paths.get(getClass.getResource("/public_key.der").toURI))
val spec = new X509EncodedKeySpec(keyBytes)
val kf = KeyFactory.getInstance(algorithm)
kf.generatePublic(spec)
}
def decrypt(privateKey: PrivateKey, input: Array[Byte]): Array[Byte] = {
val decrypt = javax.crypto.Cipher.getInstance(algorithm)
decrypt.init(javax.crypto.Cipher.DECRYPT_MODE, privateKey)
decrypt.doFinal(input)
}
}
示例4: Cert
//设置package包名称以及导入依赖的类
package run.cosy.crypto
import java.math.BigInteger
import java.security.KeyFactory
import java.security.interfaces.RSAPublicKey
import java.security.spec.RSAPublicKeySpec
import akka.http.scaladsl.model.Uri
import org.w3.banana.{PointedGraph, RDF, RDFOps, binder}
import org.w3.banana.binder.{PGBinder, RecordBinder, ToPG}
object Cert {
def binderWithName[Rdf<:RDF](u: Uri)(implicit ops: RDFOps[Rdf]): PGBinder[Rdf, RSAPublicKey] =
new Cert[Rdf].binderRootName(u.toString())
def binder[Rdf<:RDF](implicit ops: RDFOps[Rdf]): PGBinder[Rdf, RSAPublicKey] = new Cert[Rdf].binder
}
class Cert[Rdf<:RDF](implicit ops: RDFOps[Rdf]) {
import org.w3.banana.{CertPrefix, RDF, RDFOps, binder}
implicit val recordBinder = org.w3.banana.binder.RecordBinder[Rdf](ops)
val cert = CertPrefix[Rdf]
import org.w3.banana.binder._
import recordBinder._
implicit val rsaClassUri = recordBinder.classUrisFor[RSAPublicKey](cert.RSAPublicKey)
val factory = KeyFactory.getInstance("RSA")
val exponent = property[BigInteger](cert.exponent)
val modulus = property[Array[Byte]](cert.modulus)
val binder: PGBinder[Rdf, RSAPublicKey] =
pgb[RSAPublicKey](modulus, exponent)(
(m: Array[Byte], e: BigInteger) => factory.generatePublic(new RSAPublicKeySpec(new BigInteger(m), e)).asInstanceOf[RSAPublicKey],
(key: RSAPublicKey) => Some((key.getModulus.toByteArray, key.getPublicExponent))
).withClasses(rsaClassUri)
def binderRootName(uri: String) =
pgbWithConstId[RSAPublicKey](uri)(modulus, exponent)(
(m: Array[Byte], e: BigInteger) => factory.generatePublic(new RSAPublicKeySpec(new BigInteger(m), e)).asInstanceOf[RSAPublicKey],
(key: RSAPublicKey) => Some((key.getModulus.toByteArray, key.getPublicExponent))
).withClasses(rsaClassUri)
}
示例5: Keys
//设置package包名称以及导入依赖的类
package com.malliina.aws.cognito
import java.math.BigInteger
import java.security.KeyFactory
import java.security.interfaces.RSAPublicKey
import java.security.spec.RSAPublicKeySpec
import java.util.Base64
object Keys {
val keyFactory = KeyFactory.getInstance("RSA")
def publicKey(key: JWTKey): RSAPublicKey =
publicKey(toBigInt(key.n), toBigInt(key.e))
private def publicKey(modulus: BigInteger, exponent: BigInteger): RSAPublicKey = {
val keySpec = new RSAPublicKeySpec(modulus, exponent)
keyFactory.generatePublic(keySpec).asInstanceOf[RSAPublicKey]
}
private def toBigInt(enc: String): BigInteger =
new BigInteger(1, Base64.getUrlDecoder.decode(enc))
}
示例6: NkPrivateRsaCrtKey
//设置package包名称以及导入依赖的类
package com.nitrokey.nethsmtest
import java.security.interfaces.RSAPrivateCrtKey
import java.security.spec.RSAPrivateCrtKeySpec
import java.security.KeyFactory
import java.math.BigInteger
import NetHsmProtocol._
case class NkPrivateRsaCrtKey(
modulus: Seq[Byte],
publicExponent: Seq[Byte],
privateExponent: Seq[Byte],
primeP: Seq[Byte],
primeQ: Seq[Byte],
primeExponentP: Seq[Byte],
primeExponentQ: Seq[Byte],
crtCoefficient: Seq[Byte]) {
def javaPrivateKey: RSAPrivateCrtKey = {
val privateKeySpec: RSAPrivateCrtKeySpec = new RSAPrivateCrtKeySpec(
new BigInteger(modulus.toArray),
new BigInteger(publicExponent.toArray),
new BigInteger(privateExponent.toArray),
new BigInteger(primeP.toArray),
new BigInteger(primeQ.toArray),
new BigInteger(primeExponentP.toArray),
new BigInteger(primeExponentQ.toArray),
new BigInteger(crtCoefficient.toArray))
val kf = KeyFactory.getInstance("RSA")
kf.generatePrivate(privateKeySpec).asInstanceOf[RSAPrivateCrtKey]
}
def publicKey: NkPublicRsaKey = {
NkPublicRsaKey(modulus, publicExponent)
}
def privateKey: NkPrivateRsaKey = {
NkPrivateRsaKey(primeP, primeQ, publicExponent)
}
}
object NkPrivateRsaCrtKey {
def apply(priv: RSAPrivateCrtKey) = {
new NkPrivateRsaCrtKey(
priv.getModulus.toByteArray,
priv.getPublicExponent.toByteArray,
priv.getPrivateExponent.toByteArray,
priv.getPrimeP.toByteArray,
priv.getPrimeQ.toByteArray,
priv.getPrimeExponentP.toByteArray,
priv.getPrimeExponentQ.toByteArray,
priv.getCrtCoefficient.toByteArray
)
}
}
示例7: JWTPublicKeyProviderImpl
//设置package包名称以及导入依赖的类
package access
import java.security.spec.{ECPoint, ECPublicKeySpec}
import java.security.{KeyFactory, PublicKey, Security}
import com.google.inject.Inject
import org.bouncycastle.jce.ECNamedCurveTable
import org.bouncycastle.jce.spec.ECNamedCurveSpec
import play.api.Configuration
class JWTPublicKeyProviderImpl @Inject()(configuration: Configuration) extends JWTPublicKeyProvider {
override def publicKey: PublicKey = {
val xRaw: String = configuration.getString("accessService.X").getOrElse("")
val yRaw: String = configuration.getString("accessService.Y").getOrElse("")
val X = BigInt(xRaw, 16)
val Y = BigInt(yRaw, 16)
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider())
val curveParams = ECNamedCurveTable.getParameterSpec("P-521")
val curveSpec =
new ECNamedCurveSpec("P-521", curveParams.getCurve, curveParams.getG, curveParams.getN, curveParams.getH)
val publicSpec = new ECPublicKeySpec(new ECPoint(X.underlying(), Y.underlying()), curveSpec)
KeyFactory.getInstance("ECDSA", "BC").generatePublic(publicSpec)
}
}
示例8: JWTPrivateKeyProviderImpl
//设置package包名称以及导入依赖的类
package access
import java.security.spec.ECPrivateKeySpec
import java.security.{KeyFactory, PrivateKey, Security}
import com.google.inject.Inject
import org.bouncycastle.jce.ECNamedCurveTable
import org.bouncycastle.jce.spec.ECNamedCurveSpec
import play.api.Configuration
class JWTPrivateKeyProviderImpl @Inject()(configuration: Configuration) extends JWTPrivateKeyProvider {
override def privateKey: PrivateKey = {
val sRaw: String = configuration.getString("accessService.S").getOrElse("")
val S = BigInt(sRaw, 16)
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider())
val curveParams = ECNamedCurveTable.getParameterSpec("P-521")
val curveSpec =
new ECNamedCurveSpec("P-521", curveParams.getCurve, curveParams.getG, curveParams.getN, curveParams.getH)
val privateSpec = new ECPrivateKeySpec(S.underlying(), curveSpec)
KeyFactory.getInstance("ECDSA", "BC").generatePrivate(privateSpec)
}
}
示例9: loadPkcs8PrivateKey
//设置package包名称以及导入依赖的类
package mu.node.echod.util
import java.nio.file.{Files, Paths}
import java.security.{KeyFactory, PrivateKey, PublicKey}
import java.security.spec.{PKCS8EncodedKeySpec, X509EncodedKeySpec}
import pdi.jwt.JwtAlgorithm.RS256
trait KeyUtils {
val jwtDsa = RS256
def loadPkcs8PrivateKey(path: String): PrivateKey = {
val keyBytes = Files.readAllBytes(Paths.get(path))
val spec = new PKCS8EncodedKeySpec(keyBytes)
KeyFactory.getInstance("RSA").generatePrivate(spec)
}
def loadX509PublicKey(path: String): PublicKey = {
val keyBytes = Files.readAllBytes(Paths.get(path))
val spec = new X509EncodedKeySpec(keyBytes)
KeyFactory.getInstance("RSA").generatePublic(spec)
}
}
示例10: RSA
//设置package包名称以及导入依赖的类
package utils
import java.security.{KeyFactory, PrivateKey}
import javax.crypto.Cipher
import java.util.Base64
import java.security.spec.RSAPrivateCrtKeySpec
import sun.security.util.DerInputStream
object RSA {
val privateKeyStr = "-----BEGIN RSA PRIVATE KEY-----\nMIICXQIBAAKBgQDlOJu6TyygqxfWT7eLtGDwajtNFOb9I5XRb6khyfD1Yt3YiCgQ\nWMNW649887VGJiGr/L5i2osbl8C9+WJTeucF+S76xFxdU6jE0NQ+Z+zEdhUTooNR\naY5nZiu5PgDB0ED/ZKBUSLKL7eibMxZtMlUDHjm4gwQco1KRMDSmXSMkDwIDAQAB\nAoGAfY9LpnuWK5Bs50UVep5c93SJdUi82u7yMx4iHFMc/Z2hfenfYEzu+57fI4fv\nxTQ//5DbzRR/XKb8ulNv6+CHyPF31xk7YOBfkGI8qjLoq06V+FyBfDSwL8KbLyeH\nm7KUZnLNQbk8yGLzB3iYKkRHlmUanQGaNMIJziWOkN+N9dECQQD0ONYRNZeuM8zd\n8XJTSdcIX4a3gy3GGCJxOzv16XHxD03GW6UNLmfPwenKu+cdrQeaqEixrCejXdAF\nz/7+BSMpAkEA8EaSOeP5Xr3ZrbiKzi6TGMwHMvC7HdJxaBJbVRfApFrE0/mPwmP5\nrN7QwjrMY+0+AbXcm8mRQyQ1+IGEembsdwJBAN6az8Rv7QnD/YBvi52POIlRSSIM\nV7SwWvSK4WSMnGb1ZBbhgdg57DXaspcwHsFV7hByQ5BvMtIduHcT14ECfcECQATe\naTgjFnqE/lQ22Rk0eGaYO80cc643BXVGafNfd9fcvwBMnk0iGX0XRsOozVt5Azil\npsLBYuApa66NcVHJpCECQQDTjI2AQhFc1yRnCU/YgDnSpJVm1nASoRUnU8Jfm3Oz\nuku7JUXcVpt08DFSceCEX9unCuMcT72rAQlLpdZir876\n-----END RSA PRIVATE KEY-----"
val privateKey: PrivateKey = {
val removedStr = privateKeyStr.replace("-----BEGIN RSA PRIVATE KEY-----\n", "").replace("-----END RSA PRIVATE KEY-----", "").replaceAll("\\s", "")
val derReader = new DerInputStream(Base64.getDecoder.decode(removedStr))
val seq = derReader.getSequence(0)
val modulus = seq(1).getBigInteger
val publicExp = seq(2).getBigInteger
val privateExp = seq(3).getBigInteger
val prime1 = seq(4).getBigInteger
val prime2 = seq(5).getBigInteger
val exp1 = seq(6).getBigInteger
val exp2 = seq(7).getBigInteger
val crtCoef = seq(8).getBigInteger
val keySpec = new RSAPrivateCrtKeySpec(modulus, publicExp, privateExp, prime1, prime2, exp1, exp2, crtCoef)
val kf = KeyFactory.getInstance("RSA")
kf.generatePrivate(keySpec)
}
def decrypt(text: String): String = {
val cipher = Cipher.getInstance("RSA")
cipher.init(Cipher.DECRYPT_MODE, privateKey)
new String(cipher.doFinal(Base64.getDecoder.decode(text)))//TODO: verify text to be 128bit
}
}
示例11: JiraApi
//设置package包名称以及导入依赖的类
package de.codecentric.ccdashboard.service.timesheet.oauth
import java.security.spec.PKCS8EncodedKeySpec
import java.security.{KeyFactory, PrivateKey}
import java.util.Base64
import com.github.scribejava.core.builder.api.DefaultApi10a
import com.github.scribejava.core.model.OAuth1RequestToken
import com.github.scribejava.core.services.{RSASha1SignatureService, SignatureService}
class JiraApi(val privateKey: String, val authorizeUrl: String, val requestTokenResource: String, val accessTokenResource: String) extends DefaultApi10a {
def getAccessTokenEndpoint: String = accessTokenResource
def getAuthorizationUrl(requestToken: OAuth1RequestToken): String = String.format(authorizeUrl, requestToken.getToken)
def getRequestTokenEndpoint: String = requestTokenResource
override def getSignatureService: SignatureService = new RSASha1SignatureService(getPrivateKey)
private def getPrivateKey: PrivateKey = try {
val key: Array[Byte] = Base64.getDecoder.decode(privateKey) // this is the PEM encoded PKCS#8 private key
val keySpec: PKCS8EncodedKeySpec = new PKCS8EncodedKeySpec(key)
val kf: KeyFactory = KeyFactory.getInstance("RSA")
kf.generatePrivate(keySpec)
}
catch {
case e: Exception => {
throw new RuntimeException(e)
}
}
}
示例12: PemKeyUtil
//设置package包名称以及导入依赖的类
package jwtyped
import java.security.{PrivateKey, PublicKey}
import java.security.spec.PKCS8EncodedKeySpec
import java.util.Base64
import java.security.spec.X509EncodedKeySpec
import java.security.KeyFactory
object PemKeyUtil {
def decodePublicKey(pem: String): PublicKey = {
val bytes = pemToDer(pem)
decodePublicKey(bytes)
}
def decodePrivateKey(pem: String): PrivateKey = {
val bytes = pemToDer(pem)
decodePrivateKey(bytes)
}
def pemToDer(pem: String): Array[Byte] = {
Base64.getDecoder.decode(trimBeginEnd(pem))
}
def trimBeginEnd(pem: String) = {
pem.replaceAll("-----BEGIN (.*)-----", "")
.replaceAll("-----END (.*)----", "")
.replaceAll("\r\n", "")
.replaceAll("\n", "")
.trim()
}
def decodePublicKey(der: Array[Byte]): PublicKey = {
val spec = new X509EncodedKeySpec(der)
val rsa = KeyFactory.getInstance("RSA")
rsa.generatePublic(spec)
}
def decodePrivateKey(der: Array[Byte]): PrivateKey = {
val spec = new PKCS8EncodedKeySpec(der)
val rsa = KeyFactory.getInstance("RSA")
rsa.generatePrivate(spec)
}
}
示例13: JWTPublicKeyProviderImpl
//设置package包名称以及导入依赖的类
package com.eigenroute.authentication
import java.security.spec.{ECPoint, ECPublicKeySpec}
import java.security.{KeyFactory, PublicKey, Security}
import com.typesafe.config.ConfigFactory
import org.bouncycastle.jce.ECNamedCurveTable
import org.bouncycastle.jce.spec.ECNamedCurveSpec
class JWTPublicKeyProviderImpl extends JWTPublicKeyProvider {
val configuration = ConfigFactory.load()
override def publicKey: PublicKey = {
val xRaw: String = configuration.getString("eigenrouteAuthenticatedAction.publicKey.X")
val yRaw: String = configuration.getString("eigenrouteAuthenticatedAction.publicKey.Y")
val X = BigInt(xRaw, 16)
val Y = BigInt(yRaw, 16)
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider())
val curveParams = ECNamedCurveTable.getParameterSpec("P-521")
val curveSpec =
new ECNamedCurveSpec("P-521", curveParams.getCurve, curveParams.getG, curveParams.getN, curveParams.getH)
val publicSpec = new ECPublicKeySpec(new ECPoint(X.underlying(), Y.underlying()), curveSpec)
KeyFactory.getInstance("ECDSA", "BC").generatePublic(publicSpec)
}
}
开发者ID:shafiquejamal,项目名称:eigenroute-authenticated-action,代码行数:28,代码来源:JWTPublicKeyProviderImpl.scala
示例14: JWTPrivateKeyProviderImpl
//设置package包名称以及导入依赖的类
package com.eigenroute.authentication
import java.security.spec.ECPrivateKeySpec
import java.security.{KeyFactory, PrivateKey, Security}
import com.typesafe.config.ConfigFactory
import org.bouncycastle.jce.ECNamedCurveTable
import org.bouncycastle.jce.spec.ECNamedCurveSpec
class JWTPrivateKeyProviderImpl extends JWTPrivateKeyProvider {
val configuration = ConfigFactory.load()
override def privateKey: PrivateKey = {
val sRaw: String = configuration.getString("eigenrouteAuthenticatedAction.privateKey.S")
val S = BigInt(sRaw, 16)
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider())
val curveParams = ECNamedCurveTable.getParameterSpec("P-521")
val curveSpec =
new ECNamedCurveSpec("P-521", curveParams.getCurve, curveParams.getG, curveParams.getN, curveParams.getH)
val privateSpec = new ECPrivateKeySpec(S.underlying(), curveSpec)
KeyFactory.getInstance("ECDSA", "BC").generatePrivate(privateSpec)
}
}
开发者ID:shafiquejamal,项目名称:eigenroute-authenticated-action,代码行数:26,代码来源:JWTPrivateKeyProviderImpl.scala