/// <summary> /// Get PublicKey /// </summary> /// <param name="key">key(by Base64)</param> /// <returns></returns> private static IPublicKey GetPublicKey(string key) { X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64Hw.Decode(key)); KeyFactory keyFactory = KeyFactory.GetInstance("RSA"); IPublicKey publicKey = keyFactory.GeneratePublic(keySpec); return(publicKey); }
/// <summary> /// encrypt bytes: src data /// </summary> /// <param name="bytes">bytes</param> /// <param name="publicKey">publicKey</param> /// <param name="algorithm">algorithm</param> /// <param name="inputCharset">inputCharset</param> /// <returns></returns> public static string Encrypt(sbyte[] bytes, string publicKey, string algorithm, string inputCharset) { IKey key = GetPublicKey(publicKey); Cipher cipher = Cipher.GetInstance(algorithm); cipher.Init(Javax.Crypto.CipherMode.EncryptMode, key); if (CommonUtil.IsNull(inputCharset)) { inputCharset = "UTF-8"; } byte[] b1 = cipher.DoFinal((byte[])(Array)bytes); sbyte[] sbyteArray = (sbyte[])(Array)b1; string encoded = Base64Hw.Encode(sbyteArray); return(encoded); }