update RSA

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

View File

@ -1,36 +1,34 @@
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")
keyPairGenerator.initialize(1024)//512-65536 & 64的倍数
val keyPair = keyPairGenerator.generateKeyPair()
publicKey = keyPair.public as RSAPublicKey
encryptCipher.init(Cipher.ENCRYPT_MODE, publicKey) encryptCipher.init(Cipher.ENCRYPT_MODE, publicKey)
decryptCipher = Cipher.getInstance("RSA")!! if (privateKey != null) decryptCipher.init(Cipher.DECRYPT_MODE, privateKey)
decryptCipher.init(Cipher.DECRYPT_MODE, keyPair.private as RSAPrivateKey)
} }
constructor(publicKey: RSAPublicKey) { constructor(keyPair: KeyPair) : this(keyPair.public as RSAPublicKey, keyPair.private as RSAPrivateKey)
this.publicKey = publicKey
encryptCipher.init(Cipher.ENCRYPT_MODE, publicKey) constructor(keySize: Int = 1024) : this(KeyPairGenerator.getInstance("RSA").let {
decryptCipher = null it.initialize(keySize)
} it.generateKeyPair()
})
constructor(publicKey: ByteArray) : this(KeyFactory.getInstance("RSA").generatePublic(X509EncodedKeySpec(publicKey)) as RSAPublicKey) constructor(publicKey: ByteArray) : this(KeyFactory.getInstance("RSA").generatePublic(X509EncodedKeySpec(publicKey)) as RSAPublicKey)
@ -43,10 +41,10 @@ class RSA : Encrypt {
var decodeIndex = 0 var decodeIndex = 0
while (readPosition + 117 < size) { while (readPosition + 117 < size) {
decodeIndex += cipher.doFinal(data, readPosition, 117, buffer, decodeIndex) decodeIndex += encryptCipher.doFinal(data, readPosition, 117, buffer, decodeIndex)
readPosition += 117 readPosition += 117
} }
decodeIndex += cipher.doFinal(data, readPosition, size - readPosition, buffer, decodeIndex) decodeIndex += encryptCipher.doFinal(data, readPosition, size - readPosition, buffer, decodeIndex)
buffer.copyOf(decodeIndex) buffer.copyOf(decodeIndex)
} }
@ -54,17 +52,17 @@ class RSA : Encrypt {
override fun decrypt(data: ByteArray, offset: Int, size: Int): ByteArray { override fun decrypt(data: ByteArray, offset: Int, size: Int): ByteArray {
return if (data.size < 128) { return if (data.size < 128) {
decryptCipher!!.doFinal(data, offset, size) decryptCipher.doFinal(data, offset, size)
} else { } else {
val buffer = ByteArray(size / 128 * 117 + 11) val buffer = ByteArray(size / 128 * 117 + 11)
var readPostion = offset var readPostion = offset
var decodeIndex = 0 var decodeIndex = 0
while (readPostion + 128 < size) { while (readPostion + 128 < size) {
decodeIndex += cipher.doFinal(data, readPostion, 128, buffer, decodeIndex) decodeIndex += decryptCipher.doFinal(data, readPostion, 128, buffer, decodeIndex)
readPostion += 128 readPostion += 128
} }
decodeIndex += cipher.doFinal(data, readPostion, size - readPostion, buffer, decodeIndex) decodeIndex += decryptCipher.doFinal(data, readPostion, size - readPostion, buffer, decodeIndex)
buffer.copyOf(decodeIndex) buffer.copyOf(decodeIndex)
} }
} }
@ -74,7 +72,39 @@ class RSA : Encrypt {
} }
override fun decrypt(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 decryptCipher!!.doFinal(data, offset, 128, buffer, bufferOffset) return decryptCipher.doFinal(data, offset, 128, buffer, bufferOffset)
}
fun sign(data: ByteArray): ByteArray {
val signature: Signature = Signature.getInstance("MD5withRSA")
signature.initSign(privateKey)
signature.update(data)
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) class NoPrivateKeyException(message: String? = null) : Exception(message)