加密编码 [英] Encoding for encryption

查看:67
本文介绍了加密编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我是C#开发者,

i找到很多关于加密的东西,但所有人都选择加密他们喜欢



我正在寻找的东西是2件事:

1.什么是支持所有编码的最佳加密编码

2.do所有加密算法都支持所有编码?这是好事,我让用户在使用算法后选择他需要的编码?或者我必须限制选择?





我也希望看到加密存储文件的最佳选择编码..



,因为我同时使用字符串加密和文件加密选项。



注意:我使用了所有对称和非对称加密在VS库中提供。

很高兴逐一了解所有这些:| sry导致你麻烦

Hello there,

i'm C# developer,
i find lot of thing about encryption, but all of them choose what encryption they liked

the thing i'm looking for is 2 thing:
1. what's best encoding for encryption which support all encoding
2.do all encryption algorithm support all encoding? and is this good that i let user choose the encoding he require once he use algorithm? or i have to limit the choice?


also i want to see what's best choice encoding to encrypt a stored file..

because i use both string encryption and file encryption option.

Note: i used all symmetric and asymmetric encryption provided in VS library.
it's good to know about all of them one by one :| sry cause you trouble

推荐答案

加密不考虑编码 - 它适用于字节流,并且根本不承担任何编码。如果你考虑一下,这是它可以工作的唯一方法,除非加密算法被告知这是一个二进制可执行文件,对待它的方式不同,这将是适得其反的。只需将加密方法传递给一个字节数组,让它继续使用它 - 这样你就可以进行任何非编码和重新编码,而不会加密。
Encryption does not look at encoding - it works on a stream of bytes, and does not assume any encoding at all. If you think about it, that is the only way it can work, unless the encryption algorithm is told "this is a binary executable file, treat it differently" which would be counter productive. Just hand the encrypt method a byte array and let it get on with it - that way you can do any un-encoding and re-encoding without encryption messing things up.


Hello Hassan,



这是你要找的吗?



  • 你可以交换 alg 以及与您的需求相匹配的相关辅助函数。
  • 如果您有一个字节数组而不是C#字符串,您可以将Unicode转换保留为字节数组。
  • 如果您不需要可打印的表示,请将base64部分留下。
Hello Hassan,

Is this what you are looking for?

  • You may exchange alg and the associated aux functions to match your needs.
  • If you have a byte array instead of a C# string, you may leave away the Unicode conversions to byte array.
  • If you do not need a printable representation, leave the base64 parts away.
/// <summary>
/// Encryption utilities for string encryption.
/// This is using know-how obtained from
/// http://www.codeproject.com/KB/security/DotNetCrypto.aspx
/// (no license restrictions apply).
/// </summary>
public class StringEncryption
{
    /// <summary>Salt: some static byte array, e.g. your name</summary>
    private static readonly byte[] salt = new byte[] { ... };

    /// <summary>Encrypt a string and return it as hex encocded.</summary>
    /// <param name="plainText">plain text to encrypt</param>
    /// <param name="password">password</param>
    /// <returns>encrypted text as base64 string (without new lines)</returns>
    public static string Encrypt(string plainText, params byte[] password)
    {
        using (var ms = new MemoryStream())
        {
            using (var alg = Rijndael.Create())
            using (var pwd = new Rfc2898DeriveBytes(password, salt, 1000))
            using (var cs = new CryptoStream(ms, GetEncryptor(alg, pwd),
                                                 CryptoStreamMode.Write))
            {
                byte[] plainBytes = Encoding.Unicode.GetBytes(plainText);
                cs.Write(plainBytes, 0, plainBytes.Length);
            }
            byte[] encodedBytes = ms.ToArray();
            string base64string = Convert.ToBase64String(encodedBytes,
                                            base64FormattingOptions.None);
            return base64string;
        }
    }
    /// <summary>
    /// Decrypt a base64 encocded and encrypted string and return it as plain text.
    /// </summary>
    /// <param name="encryptedText">base64 encoded and encrypted text</param>
    /// <param name="password">password</param>
    /// <returns>decrypted text</returns>
    public static string Decrypt(string encryptedText, params byte[] password)
    {
        using (var ms = new MemoryStream())
        {
            using (var alg = Rijndael.Create())
            using (var pwd = new Rfc2898DeriveBytes(password, salt, 1000))
            using (var cs = new CryptoStream(ms, GetDecryptor(alg, pwd),
                                                 CryptoStreamMode.Write))
            {
                byte[] decodedBytes = Convert.FromBase64String(encryptedText);
                cs.Write(decodedBytes, 0, decodedBytes.Length);
            }
            byte[] decryptedBytes = ms.ToArray();
            string plainText = Encoding.Unicode.GetString(decryptedBytes);
            return plainText;
        }
    }

    private static ICryptoTransform GetEncryptor(SymmetricAlgorithm algorithm,
                                                 DeriveBytes pwd)
    {
        return algorithm.CreateEncryptor(pwd.GetBytes(32), pwd.GetBytes(16));
    }
    private static ICryptoTransform GetDecryptor(SymmetricAlgorithm algorithm,
                                                 DeriveBytes pwd)
    {
        return algorithm.CreateDecryptor(pwd.GetBytes(32), pwd.GetBytes(16));
    }
}


1。所以,如果我将字符串转换为utf,我只是做更大的字节数组?并且它仍然适用于方法吗?



2.我没有用你的语言完全解决我的问题,你说如果我想转换为字节可执行文件文件或DLL,我必须做特定的编码?
1. so if i convert string to utf, i just make bigger array of byte? and still it work same for method?

2. i didn't got you completely for my issue in your language, you say if i want to convert to byte executable file or dll, i have to do specific encoding?


这篇关于加密编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆