/** Ċ Ġ* ĠMethods Ġfrom Ġthe Ġfollowing Ġblog Ġpost : Ċ Ġ* Ġhttp :// n iel s . nu / blog / 2 0 1 6 / java - rsa . html Ċ Ġ*/ Ċ package Ġcom . will win der . ly nx Ċ Ċ import Ġjava . nio . charset . StandardCharsets . UTF _ 8 Ċ import Ġjava . security .* Ċ import Ġjava . security . Key Factory Ċ import Ġjava . security . spec . X 5 0 9 Encoded KeySpec Ċ import Ġjava . util .* Ċ import Ġjavax . crypto . Cipher Ċ Ċ @ Throws ( Exception :: class ) Ċ fun Ġgenerate KeyPair (): ĠKey Pair Ġ{ ĊĠ Ġval Ġgenerator Ġ= ĠKey Pair Generator . getInstance (" RSA ") ĊĠ Ġgenerator . initialize ( 2 0 4 8 , ĠSecure Random ()) ĊĊĠ Ġreturn Ġgenerator . generate KeyPair () Ċ } Ċ Ċ @ Throws ( Exception :: class ) Ċ fun Ġget PublicKey ( key : ĠString ): ĠPublicKey Ġ{ ĊĠ Ġval Ġbyte Key Ġ= ĠBase 6 4 . get Decoder (). decode ( key . toByteArray ()) ĊĠ Ġval Ġk Ġ= ĠX 5 0 9 Encoded KeySpec ( byte Key ) ĊĠ Ġval Ġk f Ġ= ĠKey Factory . getInstance (" RSA ") ĊĊĠ Ġreturn Ġk f . generate Public ( k ) Ċ } Ċ Ċ @ Throws ( Exception :: class ) Ċ fun Ġget PrivateKey ( key : ĠString ): ĠPrivate Key Ġ{ ĊĠ Ġval Ġbyte Key Ġ= ĠBase 6 4 . get Decoder (). decode ( key . toByteArray ()) ĊĠ Ġval Ġk Ġ= ĠX 5 0 9 Encoded KeySpec ( byte Key ) ĊĠ Ġval Ġk f Ġ= ĠKey Factory . getInstance (" RSA ") ĊĊĠ Ġreturn Ġk f . generate Private ( k ) Ċ } ĊĊ Ċ @ Throws ( Exception :: class ) Ċ fun Ġsign ( text : ĠString , ĠprivateKey : ĠPrivate Key ): ĠString Ġ{ ĊĠ Ġval Ġprivate Signature Ġ= ĠSignature . getInstance (" SHA 2 5 6 with RSA ") ĊĠ Ġprivate Signature . init Sign ( privateKey ) ĊĠ Ġprivate Signature . update ( text . toByteArray ( UTF _ 8 )) ĊĊĠ Ġval Ġsignature Ġ= Ġprivate Signature . sign () ĊĊĠ Ġreturn ĠBase 6 4 . get Encoder (). encode ToString ( signature ) Ċ } Ċ Ċ @ Throws ( Exception :: class ) Ċ fun Ġverify ( text : ĠString , Ġsignature : ĠString , ĠpublicKey : ĠPublicKey ): ĠBoolean Ġ{ ĊĠ Ġval Ġpublic Signature Ġ= ĠSignature . getInstance (" SHA 2 5 6 with RSA ") ĊĠ Ġpublic Signature . init Verify ( publicKey ) ĊĠ Ġpublic Signature . update ( text . toByteArray ( UTF _ 8 )) ĊĊĠ Ġval Ġsignature Bytes Ġ= ĠBase 6 4 . get Decoder (). decode ( signature ) ĊĊĠ Ġreturn Ġpublic Signature . verify ( signature Bytes ) Ċ } Ċ Ċ @ Throws ( Exception :: class ) Ċ fun Ġencrypt ( text : ĠString , ĠpublicKey : ĠPublicKey ): ĠString Ġ{ ĊĠ Ġval Ġcipher Ġ= ĠCipher . getInstance (" RSA ") ĊĠ Ġcipher . init ( Cipher . ENCRYPT _ MODE , ĠpublicKey ) ĊĊĠ Ġval Ġcipher Text Ġ= Ġcipher . do Final ( text . toByteArray ( UTF _ 8 )) ĊĊĠ Ġreturn ĠBase 6 4 . get Encoder (). encode ToString ( cipher Text ) ĊĠĠĠĠĠ Ġ?: Ġthrow ĠIllegalArgumentException (" Failed Ġto Ġencode : Ġ$ text ") Ċ } Ċ Ċ @ Throws ( Exception :: class ) Ċ fun Ġdecrypt ( cipher Text : ĠString , ĠprivateKey : ĠPrivate Key ): ĠString Ġ{ ĊĠ Ġval Ġbytes Ġ= ĠBase 6 4 . get Decoder (). decode ( cipher Text ) ĊĊĠ Ġval Ġcipher Ġ= ĠCipher . getInstance (" RSA ") ĊĠ Ġcipher . init ( Cipher . DE CRYPT _ MODE , ĠprivateKey ) ĊĊĠ Ġreturn ĠString ( cipher . do Final ( bytes ), ĠUTF _ 8 ) Ċ } Ċ