update RSA

This commit is contained in:
tursom 2020-03-02 00:36:29 +08:00
parent b4f3433180
commit cd2be4c409

View File

@ -1,81 +1,111 @@
package cn.tursom.core.encrypt package cn.tursom.core.encrypt
import java.security.KeyFactory import java.security.KeyFactory
import java.security.KeyPair
import java.security.KeyPairGenerator import java.security.KeyPairGenerator
import java.security.Signature
import java.security.interfaces.RSAPrivateKey import java.security.interfaces.RSAPrivateKey
import java.security.interfaces.RSAPublicKey import java.security.interfaces.RSAPublicKey
import java.security.spec.X509EncodedKeySpec import java.security.spec.X509EncodedKeySpec
import javax.crypto.Cipher import javax.crypto.Cipher
class RSA : Encrypt { @Suppress("unused")
val publicKey: RSAPublicKey class RSA(val publicKey: RSAPublicKey, val privateKey: RSAPrivateKey? = null) : Encrypt {
val publicKeyEncoded get() = publicKey.encoded!!
private val cipher = Cipher.getInstance("RSA")!! val privateKeyEncoded get() = privateKey?.encoded
private val encryptCipher = Cipher.getInstance("RSA")!! private val encryptCipher = Cipher.getInstance("RSA")!!
private val decryptCipher: Cipher? private val decryptCipher = Cipher.getInstance("RSA")!!
constructor() { init {
val keyPairGenerator = KeyPairGenerator.getInstance("RSA") encryptCipher.init(Cipher.ENCRYPT_MODE, publicKey)
keyPairGenerator.initialize(1024)//512-65536 & 64的倍数 if (privateKey != null) decryptCipher.init(Cipher.DECRYPT_MODE, privateKey)
val keyPair = keyPairGenerator.generateKeyPair() }
publicKey = keyPair.public as RSAPublicKey
encryptCipher.init(Cipher.ENCRYPT_MODE, publicKey) constructor(keyPair: KeyPair) : this(keyPair.public as RSAPublicKey, keyPair.private as RSAPrivateKey)
decryptCipher = Cipher.getInstance("RSA")!!
decryptCipher.init(Cipher.DECRYPT_MODE, keyPair.private as RSAPrivateKey) constructor(keySize: Int = 1024) : this(KeyPairGenerator.getInstance("RSA").let {
} it.initialize(keySize)
it.generateKeyPair()
constructor(publicKey: RSAPublicKey) { })
this.publicKey = publicKey
encryptCipher.init(Cipher.ENCRYPT_MODE, publicKey) constructor(publicKey: ByteArray) : this(KeyFactory.getInstance("RSA").generatePublic(X509EncodedKeySpec(publicKey)) as RSAPublicKey)
decryptCipher = null
} override fun encrypt(data: ByteArray, offset: Int, size: Int): ByteArray {
return if (size < 117)
constructor(publicKey: ByteArray) : this(KeyFactory.getInstance("RSA").generatePublic(X509EncodedKeySpec(publicKey)) as RSAPublicKey) encryptCipher.doFinal(data, offset, size)
else {
override fun encrypt(data: ByteArray, offset: Int, size: Int): ByteArray { val buffer = ByteArray(size / 117 * 128 + 128)
return if (size < 117) var readPosition = offset
encryptCipher.doFinal(data, offset, size) var decodeIndex = 0
else {
val buffer = ByteArray(size / 117 * 128 + 128) while (readPosition + 117 < size) {
var readPosition = offset decodeIndex += encryptCipher.doFinal(data, readPosition, 117, buffer, decodeIndex)
var decodeIndex = 0 readPosition += 117
}
while (readPosition + 117 < size) { decodeIndex += encryptCipher.doFinal(data, readPosition, size - readPosition, buffer, decodeIndex)
decodeIndex += cipher.doFinal(data, readPosition, 117, buffer, decodeIndex)
readPosition += 117 buffer.copyOf(decodeIndex)
} }
decodeIndex += cipher.doFinal(data, readPosition, size - readPosition, buffer, decodeIndex) }
buffer.copyOf(decodeIndex) override fun decrypt(data: ByteArray, offset: Int, size: Int): ByteArray {
} return if (data.size < 128) {
} decryptCipher.doFinal(data, offset, size)
} else {
override fun decrypt(data: ByteArray, offset: Int, size: Int): ByteArray { val buffer = ByteArray(size / 128 * 117 + 11)
return if (data.size < 128) { var readPostion = offset
decryptCipher!!.doFinal(data, offset, size) var decodeIndex = 0
} else {
val buffer = ByteArray(size / 128 * 117 + 11) while (readPostion + 128 < size) {
var readPostion = offset decodeIndex += decryptCipher.doFinal(data, readPostion, 128, buffer, decodeIndex)
var decodeIndex = 0 readPostion += 128
}
while (readPostion + 128 < size) { decodeIndex += decryptCipher.doFinal(data, readPostion, size - readPostion, buffer, decodeIndex)
decodeIndex += cipher.doFinal(data, readPostion, 128, buffer, decodeIndex) buffer.copyOf(decodeIndex)
readPostion += 128 }
} }
decodeIndex += cipher.doFinal(data, readPostion, size - readPostion, buffer, decodeIndex)
buffer.copyOf(decodeIndex) override fun encrypt(data: ByteArray, buffer: ByteArray, bufferOffset: Int, offset: Int, size: Int): Int {
} return encryptCipher.doFinal(data, offset, 128, buffer, bufferOffset)
} }
override fun encrypt(data: ByteArray, buffer: ByteArray, bufferOffset: Int, offset: Int, size: Int): Int { override fun decrypt(data: ByteArray, buffer: ByteArray, bufferOffset: Int, offset: Int, size: Int): Int {
return encryptCipher.doFinal(data, offset, 128, buffer, bufferOffset) return decryptCipher.doFinal(data, offset, 128, buffer, bufferOffset)
} }
override fun decrypt(data: ByteArray, buffer: ByteArray, bufferOffset: Int, offset: Int, size: Int): Int { fun sign(data: ByteArray): ByteArray {
return decryptCipher!!.doFinal(data, offset, 128, buffer, bufferOffset) val signature: Signature = Signature.getInstance("MD5withRSA")
} signature.initSign(privateKey)
signature.update(data)
class NoPrivateKeyException(message: String? = null) : Exception(message) return signature.sign()
}
fun verify(data: ByteArray, sign: ByteArray): Boolean {
val signature = Signature.getInstance("MD5withRSA")
signature.initVerify(publicKey)
signature.update(data)
return signature.verify(sign)
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as RSA
if (publicKey != other.publicKey) return false
if (privateKey != other.privateKey) return false
return true
}
override fun hashCode(): Int {
var result = publicKey.hashCode()
result = 31 * result + (privateKey?.hashCode() ?: 0)
return result
}
class NoPrivateKeyException(message: String? = null) : Exception(message)
} }