Exemplo n.º 1
0
        /// <summary>
        /// 文字列をAESで暗号化.
        /// </summary>
        public static string Encrypt(this string value, AesCryptoKey aesCryptoKey, bool escape = false)
        {
            if (string.IsNullOrEmpty(value))
            {
                return(null);
            }

            var bytes = Encoding.UTF8.GetBytes(value);

            bytes = Encrypt(bytes, aesCryptoKey);

            var result = Convert.ToBase64String(bytes);

            if (escape)
            {
                result = result.Replace('+', '-').Replace('/', '_');
            }

            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 文字列をAESで復号化.
        /// </summary>
        public static string Decrypt(this string value, AesCryptoKey aesCryptoKey, bool escape = false)
        {
            if (string.IsNullOrEmpty(value))
            {
                return(null);
            }

            if (escape)
            {
                value = value.Replace('-', '+').Replace('_', '/');
            }

            var bytes = Convert.FromBase64String(value);

            bytes = Decrypt(bytes, aesCryptoKey);

            var result = Encoding.UTF8.GetString(bytes);

            // string.Lengthした際に終端にNull文字が混入する為Null文字を削る.
            return(result.TrimEnd('\0'));
        }
Exemplo n.º 3
0
        /// <summary>
        /// バイト配列をAESで復号化.
        /// </summary>
        public static byte[] Decrypt(this byte[] value, AesCryptoKey aesCryptoKey)
        {
            if (value == null || value.IsEmpty())
            {
                return(null);
            }

            byte[] result = null;

            using (var memoryStream = new MemoryStream())
            {
                lock (aesCryptoKey.Decryptor)
                {
                    using (var cryptoStream = new CryptoStream(memoryStream, aesCryptoKey.Decryptor, CryptoStreamMode.Write))
                    {
                        cryptoStream.Write(value, 0, value.Length);
                    }
                }

                result = memoryStream.ToArray();
            }

            return(result);
        }
Exemplo n.º 4
0
        //====== CryptKey ======

        public static void SetCryptoKey(AesCryptoKey aesCryptoKey)
        {
            SecurePrefs.aesCryptoKey = aesCryptoKey;
        }