webencrypter.cs

来自「AJAX开发工具包」· CS 代码 · 共 61 行

CS
61
字号
using System;
using System.Text;

namespace AjaxPro.Cryptography
{
	/// <summary>
	/// PC-Topp.NET encryptor for AuthenticationTicket, Drawings filename,...
	/// </summary>
	public class WebEncrypter
	{
		/// <summary>
		/// 
		/// </summary>
		public WebEncrypter()
		{
		}

		/// <summary>
		/// Encrypts a string.
		/// </summary>
		/// <param name="Text">The string should be encrypted.</param>
		/// <param name="Key">The 8-bit string for encryption.</param>
		/// <returns>The encrypted string.</returns>
		public string Encrypt(string Text, string Key)
		{
			if(Key.Length != 8)
				throw new Exception("Key must be a 8-bit string!");

			byte[] IV = null;
			byte[] cipherText = null;
			byte[] key = null;

			try
			{
				Encryptor enc = new Encryptor(EncryptionAlgorithm.Des);
				byte[] plainText = Encoding.ASCII.GetBytes(Text);

				key = Encoding.ASCII.GetBytes(Key);
				IV = Encoding.ASCII.GetBytes("init vec");		// "init vec is big."

				enc.IV = IV;

				cipherText = enc.Encrypt(plainText, key);
				IV = enc.IV;
				key = enc.Key;

			}
			catch(Exception ex)
			{
				throw new Exception("Exception while encrypting. " + ex.Message);
			}

			return Convert.ToBase64String(cipherText);
		}
	}
}




⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?