/// <summary>
 /// AES-GCM encryption.
 /// </summary>
 /// <param name="plainData">the data to be encrypted.</param>
 /// <param name="secretKeyStr"> encryption secret key.</param>
 /// <param name="iv">encryption random iv.</param>
 /// <returns>the encrypted string.</returns>
 public static string EncryptByGcm(string plainData, string secretKeyStr, sbyte[] iv)
 {
     try
     {
         sbyte[]                 secretKeyByte = (sbyte[])(Array)Encoding.UTF8.GetBytes(secretKeyStr);
         sbyte[]                 plainByte     = (sbyte[])(Array)Encoding.UTF8.GetBytes(plainData);
         SecretKeySpec           secretKey     = new SecretKeySpec((byte[])(Array)secretKeyByte, "AES");
         Cipher                  cipher        = Cipher.GetInstance("AES/GCM/NoPadding");
         IAlgorithmParameterSpec spec          = new GCMParameterSpec(128, (byte[])(Array)iv);
         cipher.Init((CipherMode)1, secretKey, spec);
         byte[]  fBytes  = cipher.DoFinal((byte[])(Array)plainByte);
         sbyte[] fSBytes = (sbyte[])(Array)fBytes;
         return(new string(HwHex.EncodeHexString(fSBytes)));
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         throw;
     }
 }
        public static string GenerateJwe(string issuerId, string dataJson)
        {
            string  jwePrivateKey       = Constant.PrivateKey;
            string  sessionKeyPublicKey = Constant.SessionPublicKey;
            string  sessionKey          = RandomUtils.GenerateSecureRandomFactor(16);
            JObject jObject             = JObject.Parse(dataJson);

            jObject.Add("iss", issuerId);

            // The first part: JWE Head
            JweHeader jweHeader       = GetHeader();
            string    jweHeaderEncode = GetEncodeHeader(jweHeader);

            // The Second part: JWE Encrypted Key
            string encryptedKeyEncode = GetEncryptedKey(sessionKey, sessionKeyPublicKey);

            // The third part: JWE IV
            sbyte[] iv       = AESUtils.GetIvByte(12);
            string  ivHexStr = new string(HwHex.EncodeHexString(iv));
            //Java.Lang.String ivHexString = (Java.Lang.String)ivHexStr;
            string ivEncode = Base64.EncodeToString(Encoding.UTF8.GetBytes(ivHexStr), Base64Flags.UrlSafe | Base64Flags.NoWrap);

            // The fourth part: JWE CipherText empty
            string cipherTextEncode = GetCipherText(jObject.ToString(), sessionKey, iv, jweHeader);

            // The fifth part: JWE Authentication Tag
            string authenticationTagEncode =
                GetAuthenticationTag(jwePrivateKey, sessionKey, jObject.ToString(), jweHeaderEncode, ivEncode);

            Java.Lang.StringBuilder stringBuilder = new Java.Lang.StringBuilder();
            return(stringBuilder.Append(jweHeaderEncode)
                   .Append(".")
                   .Append(encryptedKeyEncode)
                   .Append(".")
                   .Append(ivEncode)
                   .Append(".")
                   .Append(cipherTextEncode)
                   .Append(".")
                   .Append(authenticationTagEncode)
                   .ToString());
        }
Пример #3
0
 public static sbyte[] HexStr2Byte(string hexStr)
 {
     return(hexStr == null ? new sbyte[0] : HwHex.DecodeHex(hexStr.ToCharArray()));
 }
Пример #4
0
 public static string Byte2HexStr(sbyte[] array)
 {
     return(array == null ? null : new string(HwHex.EncodeHex(array, false)));
 }
Пример #5
0
 private static sbyte[] HexStr2Byte(string hex)
 {
     return(hex == null ? new sbyte[0] : HwHex.DecodeHex(hex.ToCharArray()));
 }
 public static string GenerateSecureRandomFactor(int size)
 {
     sbyte[] factor = GenerateSecureRandomByte(size);
     return(HwHex.EncodeHexString(factor));
 }