encryption - Java/Kotlin Encrypt AES key with private and public key -
so lately saw video computerphile said when using encryption should using aes keys , encrypt key public , private key. means: have public key else, , own private key.
the reason behind encrypting own private key is, verifies message has comming me, because no 1 else has private key, encryption public key can work on messages comming me.
problem after first encryption, byte array outcomming becomes long , cant encrypt time. there way around this?
this code:
val akey = generateaeskey() val kg = keypairgenerator.getinstance("rsa") kg.initialize(2048) val own = kg.genkeypair() val strange = kg.genkeypair() string(akey.encoded).encryptrsa(strange.public).encryptrsa(own.public) fun generateaeskey(): key { val generator = keygenerator.getinstance("aes") generator.init(128) return generator.generatekey() fun string.encryptrsa(key: key): string { val encryptcipher = cipher.getinstance("rsa/ecb/pkcs1padding") encryptcipher.init(cipher.encrypt_mode, key) val ciphertext = encryptcipher.dofinal(this.tobytearray(charset("utf- 8"))) return string(ciphertext) } fun string.decryptrsa(key: key): string { val bytes = this.tobytearray() val decryptcipher = cipher.getinstance("rsa/ecb/pkcs1padding") decryptcipher.init(cipher.decrypt_mode, key) return string(decryptcipher.dofinal(bytes), charset("utf-8")) }
this example gives me following error:
exception in thread "main" javax.crypto.illegalblocksizeexception: data must not longer 245 bytes @ com.sun.crypto.provider.rsacipher.dofinal(rsacipher.java:344) @ com.sun.crypto.provider.rsacipher.enginedofinal(rsacipher.java:389) @ javax.crypto.cipher.dofinal(cipher.java:2165) @ de.intektor.kentai_http_common.util.encryptionkt.encryptrsa(encryption.kt:30) @ de.intektor.test.testkt.main(test.kt:19)
of course testcase.
Comments
Post a Comment