加密与解密

好久没写文章了,最近迷恋上加密与解密,没那么复杂。

 

直接给程序。大家有什么好注意一起分享啊。

 

主要思想。循环加密 

 

每个字符和一个密钥循环一次加密,这样解密也很方面。

 

大家有什么好的注意一起分享。

 

弄写主代码

 

 

  internal class Crypt {

        #region << fileds >>

        private String _keys = "/?ABCDEFGhijklmnOPQrstUVWxyz0123456789[abcdefgHIJKLMNopqRSTuvwXYZ]";

        #endregion

        #region << properties >>

        /// <summary>
        /// 密钥
        /// </summary>
        public String Keys {
            get { return _keys; }
            set { _keys = value; }
        }

        #endregion

        #region << Constructor >>

        public Crypt() { }

        #endregion

        #region << public method >>

        public byte[] Encrypt(String data) {
            return Encrypt(Encoding.Default.GetBytes(data), Encoding.Default.GetBytes(_keys));
        }

        public byte[] Encrypt(String data, String key) {
            return Encrypt(Encoding.Default.GetBytes(data), Encoding.Default.GetBytes(key));
        }

        public byte[] Encrypt(byte[] data, String key) {
            return Encrypt(data, Encoding.Default.GetBytes(key));
        }

        public byte[] Encrypt(byte[] data) {
            return Encrypt(data, Encoding.Default.GetBytes(_keys));
        }

        public byte[] Decrypt(String data) {
            return Decrypt(Encoding.Default.GetBytes(data), Encoding.Default.GetBytes(_keys));
        }

        public byte[] Decrypt(String data, String key) {
            return Decrypt(Encoding.Default.GetBytes(data), Encoding.Default.GetBytes(key));
        }

        public byte[] Decrypt(byte[] data, String key) {
            return Decrypt(data, Encoding.Default.GetBytes(key));
        }

        public byte[] Decrypt(byte[] data) {
            return Decrypt(data, Encoding.Default.GetBytes(_keys));
        }

        #endregion

        #region << private Method >>

        private byte[] Encrypt(byte[] data, byte[] key) {
            byte[] output = new byte[data.Length];
            for (int i = 0; i < data.Length; i++) {
                byte tem = data[i];
                for (int j = 0; j < key.Length; j++) {
                    tem = (byte)(tem ^ key[j]);
                }
                output[i] = tem;
            }
            return output;
        }

        private byte[] Decrypt(byte[] data, byte[] key) {
            byte[] output = new byte[data.Length];
            for (int i = 0; i < data.Length; i++) {
                byte tem = data[i];
                for (int j = key.Length - 1; j >= 0; j--) {
                    tem = (byte)(tem ^ key[j]);
                }
                output[i] = tem;
            }
            return output;
        }

        #endregion

    }

 

这个类可以直接用

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值