不囉唆, 直接看原始碼:
import java.math.BigInteger
fun main(args: Array < String > ) {
fun String.toStringHEX() = this.toByteArray(Charsets.UTF_8).map( { String.format("%02X", (it.toInt() and 0xFF)) }).joinToString(separator="")
fun String.fmStringHEX() = ByteArray(this.length/2, { this.substring(it * 2, it * 2 + 2).toInt(16).toByte() }).toString(Charsets.UTF_8)
var sd="7E134469882EC2FA67B3142B96240043BDDCB946956773301736A81E45B5378CB6C11506C6BD2B4367E200B5D31C9F05C81220C43E9E7F405A3FB64C951F559B00F7F1586AE251E1EFE6793B43FBF2A283883C4843FF593ADD0273ED2786545AE42539911A9415AB41686E367A838E4CD4F6C6A2AC5B41B821651DD857DB29110DDAF7C2044B1227696E5F1EEC6FCAECD021DC4AD318937DAF58130B5594308D8EE7F6CCBB6CEE7B9B71CC2002FE342609E0717EB4FA67B61D55D11DFBABC6933FDA203400405E71CE87450968216A8C883187B562207216F213B12931F8D8AD4DF2361BC3FC404C8B088F33C0F66B7CAD2EDD1D796182DDC51153B34EBE7335"
var sn="00CCCC68E135CDAD3A1BA300BBE2AEA65BD45F73EAD963A4C087E1E4CF60050F0E9077C59F62A98BB1DF20B371756C0DF310056FD678FF11011EB486C683B6F1F5C66A93C21F532F5E7C6EF2AABBCD681F5C013E1437C28268CCCEF01D83C4DC7E462AB06D103EBE5DDDB88982000D0D113F8520AB16679B2834517E6D1605733B6E7486E9629F9DD8E366D16A1E7D6AC836795C1889B74319816550ADB4EB8E5EAE805217E28E8FCA61496AE4DC7FB5E5C2A03E77FEAFCCFEFFB1A6C9EE96F8E239F2912665BEC90E3D960A997172E0C30C12ABEA96BBEAD59DD65506619099AA1829AB3494B6A8FE694296CB5AB08961DD11FAC39A80026CB39E6E287D1ABBC1"
var se="10001"
val 明文 = "This is a chinese 這是中文"
var sx=明文.toStringHEX()
println("原文: "+明文);
println("轉成暗碼: "+sx);
var x=BigInteger(sx,16)
var n=BigInteger(sn,16)
var d=BigInteger(sd,16)
var e=BigInteger(se,16)
var m=x.modPow(d,n); // d, n private key to encode
var z=m.modPow(e,n); // e, n public key to decode
println("\n加密暗碼: "+m.toString(16));
println("\n暗碼解密: "+z.toString(16));
println("還原明文: "+z.toString(16).fmStringHEX() )
}
輸出結果:
原文: This is a chinese 這是中文
轉成暗碼: 546869732069732061206368696E65736520E98099E698AFE4B8ADE69687
加密暗碼: bb3f1f7d5b45233c4b01ee9fdc378ead99da02b99cd4a8c1196403808ac37588468d4885c49a3a4b1f6bb71cec259dce3a4bfcbc4ae8023f59434f3f6a393670a13d914cdbfd8eea28c4e49f2653b99b22a4156e0a9891ec33e887b4a6e45e053ee91da29f7ea9d53941d00be51c613b72aa38820879ba256d13d33e08942d7abda10e322452543c62667b404589480b745cdcb4bde34e54eb09f61fc71d3d20c1edeec2cee6e7515d7802f9236b6af48ea25edf0de3de7edb960d2d05f1bbb322c820c1fa3db84c7cb2b8b4a3e079a82b2479a23d0e184bb26618a4daba83370f96c78d4aa4ff9785f2bb884b54e4f59f33942ba042f3b1827682d6c235cfb9
暗碼解密: 546869732069732061206368696e65736520e98099e698afe4b8ade69687
還原明文: This is a chinese 這是中文
沒有留言:
張貼留言