RSA - yamase0394/memo GitHub Wiki

Kotlin

//ๅ…ฌ้–‹้ตใ‚’ๅ—ไฟก
val modulus = BigInteger(1, Base64.decode(base64Modulus, Base64.NO_WRAP))
val exponent = BigInteger(1, Base64.decode(base64Exponent, Base64.NO_WRAP))

//้ตใ‚’็”Ÿๆˆ
val pubKeySpec = RSAPublicKeySpec(modulus, exponent)
val keyFactory = KeyFactory.getInstance("RSA")
val pubKey = keyFactory.generatePublic(pubKeySpec)
val cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding")
cipher.init(Cipher.ENCRYPT_MODE, pubKey)
//ๆš—ๅทๅŒ–ใ—ใฆ้€ไฟก
val encrypted = cipher.doFinal(byteArray)

C#

//ๅ…ฌ้–‹้ตใ€็ง˜ๅฏ†้ตใฎใƒšใ‚ขใ‚’็”Ÿๆˆ
var rsa = new RSACryptoServiceProvider(2048);
//ๅ…ฌ้–‹้ตใ‚’XMLใซใ‚จใ‚ฏใ‚นใƒใƒผใƒˆ
var xml = new XmlDocument();
xml.LoadXml(rsa.ToXmlString(false));
//้€ไฟก
var base64Modulus = xml.GetElementsByTagName("Modulus")[0].InnerText;
var base64Exponent = xml.GetElementsByTagName("Exponent")[0].InnerText;

//่งฃ่ชญ
var plain = rsa.Decrypt(encrypted, false);