.net 加密算法

public static string encrypt(string source)
        {
            md5cryptoserviceprovider md5 = new md5cryptoserviceprovider();
            byte[] bytes = encoding.utf8.getbytes(source);
            byte[] output = md5.computehash(bytes);

            return bitconverter.tostring(output);
        }
最常见的md5加密,但不带解密。

des加解密。
  public class des
    {
        private const string key = “av&6^3*e”;
       
        public static string encrypt(string source)
        {
            descryptoserviceprovider des = new descryptoserviceprovider();

            byte[] bytes = encoding.utf8.getbytes(source);

            des.key = asciiencoding.ascii.getbytes(key);
            des.iv = asciiencoding.ascii.getbytes(key);

            memorystream ms = new memorystream();
            cryptostream cs = new cryptostream(ms, des.createencryptor(), cryptostreammode.write);

            cs.write(bytes, 0, bytes.length);
            cs.flushfinalblock();

            stringbuilder sb = new stringbuilder();
            foreach (byte b in ms.toarray())
            {
                sb.appendformat(“{0:x2}”, b);
            }

            return sb.tostring();
        }

        public static string decrypt(string source)
        {
            if (source == null || source.length == 0)
            {
                return source;
            }

            descryptoserviceprovider des = new descryptoserviceprovider();
            byte[] bytes = new byte[source.length / 2];
            for (int x = 0; x             {
                int i = (convert.toint32(source.substring(x * 2, 2), 16));
                bytes[x] = (byte)i;
            }

            des.key = asciiencoding.ascii.getbytes(key);
            des.iv = asciiencoding.ascii.getbytes(key);

            memorystream ms = new memorystream();
            cryptostream cs = new cryptostream(ms, des.createdecryptor(), cryptostreammode.write);
            cs.write(bytes, 0, bytes.length);
            cs.flushfinalblock();

            return encoding.utf8.getstring(ms.toarray());
        }
    }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2554260.html

(0)
上一篇 2025年3月6日 07:00:13
下一篇 2025年3月6日 07:00:22

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论