Exemple #1
0
        public static byte[] BlockCipher(byte[] bytes, IBufferedCipher cipher, bool isEncrypt)
        {
            if ((bytes.Length <= 62 && isEncrypt) || (bytes.Length <= 128 && !isEncrypt))
            {
                return(cipher.DoFinal(bytes));
            }
            byte[] suffix = new byte[0];
            byte[] prefix = new byte[0];
            int    num    = (!isEncrypt) ? 128 : 62;

            byte[] array = new byte[num];
            for (int i = 0; i < bytes.Length; i++)
            {
                if (i > 0 && i % num == 0)
                {
                    suffix = cipher.DoFinal(array);
                    prefix = SwrveManagerUtils.AppendBytes(prefix, suffix);
                    int num2 = num;
                    if (i + num > bytes.Length)
                    {
                        num2 = bytes.Length - i;
                    }
                    array = new byte[num2];
                }
                array[i % num] = bytes[i];
            }
            suffix = cipher.DoFinal(array);
            return(SwrveManagerUtils.AppendBytes(prefix, suffix));
        }
Exemple #2
0
        public static string RSAEncrypt(string pemStreamText, KeyParameter secretKeyParameter)
        {
            string result = string.Empty;

            try
            {
                StreamReader reader = new StreamReader(new MemoryStream(Convert.FromBase64String(pemStreamText)));
                Org.BouncyCastle.OpenSsl.PemReader pemReader = new Org.BouncyCastle.OpenSsl.PemReader(reader);
                PemObject        pemObject = pemReader.ReadPemObject();
                RsaKeyParameters parameters;
                if (pemObject != null)
                {
                    AsymmetricKeyParameter asymmetricKeyParameter = PublicKeyFactory.CreateKey(pemObject.Content);
                    parameters = (RsaKeyParameters)asymmetricKeyParameter;
                }
                else
                {
                    parameters = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(pemStreamText));
                }
                byte[]          key    = secretKeyParameter.GetKey();
                IBufferedCipher cipher = CipherUtilities.GetCipher("RSA/ECB/OAEPWithSHA_1AndMGF1Padding");
                cipher.Init(true, parameters);
                byte[] inArray = SwrveManagerUtils.BlockCipher(key, cipher, true);
                result = Convert.ToBase64String(inArray, Base64FormattingOptions.None);
            }
            catch (Exception ex)
            {
                Debug.LogError("### SwrveManagerUtils::RSAEncrypt: " + ex.Message);
            }
            return(result);
        }
Exemple #3
0
        public static string AESEncrypt(string saltString, string plainText)
        {
            if (SwrveManagerUtils.secretKeyParameter == null)
            {
                SwrveManagerUtils.secretKeyParameter = SwrveManagerUtils.KeyGen(saltString);
            }
            string          text   = string.Empty;
            IBufferedCipher cipher = CipherUtilities.GetCipher(SwrveManagerUtils.ENCRYPTION_ALGORITHM + "/CBC/PKCS5PADDING");

            byte[]       array    = new byte[cipher.GetBlockSize()];
            SecureRandom instance = SecureRandom.GetInstance("SHA1PRNG");

            instance.NextBytes(array);
            ParametersWithIV parametersWithIV = new ParametersWithIV(SwrveManagerUtils.secretKeyParameter, array);

            cipher.Init(true, parametersWithIV);
            int num = array.Length;

            byte[] array2 = cipher.DoFinal(Encoding.UTF8.GetBytes(plainText));
            Debug.Log("AESEncrypt:: IV as string: " + Convert.ToBase64String(parametersWithIV.GetIV()));
            Debug.Log("AESEncrypt:: encryptedByte as string: " + Convert.ToBase64String(array2));
            byte[] array3 = new byte[num + array2.Length];
            Array.Copy(parametersWithIV.GetIV(), 0, array3, 0, num);
            Array.Copy(array2, 0, array3, num, array2.Length);
            text = Convert.ToBase64String(array3, Base64FormattingOptions.None);
            Debug.Log("AESEncrypt:: encryptedString: " + text);
            return(text);
        }
Exemple #4
0
        public static string GetRSAEncryptedKey()
        {
            string result = string.Empty;

            if (SwrveManagerUtils.secretKeyParameter == null)
            {
                Debug.LogError("### SwrveManagerUtils::GetRSAEncryptedKey: secretKeyParameter is null! There is nothing to encrypt");
                return(result);
            }
            try
            {
                string text = (Resources.Load("pub") as TextAsset).text;
                text   = text.Replace("-----BEGIN PUBLIC KEY-----", string.Empty).Replace("-----END PUBLIC KEY-----", string.Empty);
                result = SwrveManagerUtils.RSAEncrypt(text, SwrveManagerUtils.secretKeyParameter);
            }
            catch (Exception ex)
            {
                Debug.LogError("### SwrveManagerUtils::GetRSAEncryptedKey: " + ex.Message);
            }
            return(result);
        }