C # encryption and decryption algorithm

Source: Internet
Author: User
Tags base64 empty
Encryption | decryption | algorithm /************************* string Encryption Algorithm ***********************/
public string encryptstring (String str)
{
char[] base64code=new char[]{' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' j ', ' K ', ' l ', ' m ', ' n ', ' o ', ' P ', ' Q ', ' R ', ' s ', ' t ', ' u ' , ' V ', ' w ', ' x ', ' y ', ' z ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ', ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' + ', '/', ' = '};
byte empty= (byte) 0;
System.Collections.ArrayList bytemessage=new System.Collections.ArrayList (System.Text.Encoding.Default.GetBytes ( STR));
System.Text.StringBuilder Outmessage;
int messagelen=bytemessage.count;
int PAGE=MESSAGELEN/3;
int use=0;
if ((use=messagelen%3) >0)
{
for (int i=0;i<3-use;i++)
Bytemessage.add (empty);
page++;
}
Outmessage=new System.Text.StringBuilder (page*4);
for (int i=0;i<page;i++)
{
byte[] InStr = new Byte[3];
Instr[0]= (Byte) bytemessage[i*3];
Instr[1]= (Byte) bytemessage[i*3+1];
Instr[2]= (Byte) bytemessage[i*3+2];
Int[] Outstr=new int[4];
outstr[0]=instr[0]>>2;

Outstr[1]= ((instr[0]&0x03) <<4) ^ (instr[1]>>4);
if (!instr[1). Equals (empty))
Outstr[2]= ((instr[1]&0x0f) <<2) ^ (instr[2]>>6);
Else
outstr[2]=64;
if (!instr[2). Equals (empty))
Outstr[3]= (instr[2]&0x3f);
Else
outstr[3]=64;
Outmessage. Append (Base64code[outstr[0]]);
Outmessage. Append (Base64code[outstr[1]]);
Outmessage. Append (Base64code[outstr[2]]);
Outmessage. Append (Base64code[outstr[3]]);
}
Return outmessage. ToString ();
}
/************************************************** String decryption algorithm **************************************************/
public string decryptstring (String str)
{
if (str. length%4)!=0)
{
throw new ArgumentException ("Not the correct BASE64 encoding, please check.") "," str ");
}
if (! System.Text.RegularExpressions.Regex.IsMatch (str, "^[a-z0-9/+=]*$", System.Text.RegularExpressions.RegexOptions.IgnoreCase))
{
throw new ArgumentException ("contains incorrect BASE64 encoding, please check.") "," str ");
}
String base64code= "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/=";
int PAGE=STR. LENGTH/4;
System.Collections.ArrayList outmessage=new System.Collections.ArrayList (page*3);
Char[] Message=str. ToCharArray ();
for (int i=0;i<page;i++)
{
Byte[] Instr=new byte[4];
Instr[0]= (Byte) base64code.indexof (Message[i*4]);
Instr[1]= (Byte) base64code.indexof (message[i*4+1]);
Instr[2]= (Byte) base64code.indexof (message[i*4+2]);
Instr[3]= (Byte) base64code.indexof (Message[i*4+3]);
Byte[] Outstr=new byte[3];
Outstr[0]= (Byte) ((instr[0]<<2) ^ (instr[1]&0x30) >>4));
if (instr[2]!=64)
{
Outstr[1]= (Byte) ((instr[1]<<4) ^ (instr[2]&0x3c) >>2));
}
Else
{
outstr[2]=0;
}
if (instr[3]!=64)
{
Outstr[2]= (Byte) ((INSTR[2]&LT;&LT;6) ^instr[3]);
}
Else
{
outstr[2]=0;
}
Outmessage.add (Outstr[0]);
if (outstr[1]!=0)
Outmessage.add (outstr[1]);
if (outstr[2]!=0)
Outmessage.add (outstr[2]);
}
Byte[] outbyte= (byte[]) Outmessage.toarray (Type.GetType ("System.Byte"));
Return System.Text.Encoding.Default.GetString (Outbyte);

}
Encryption and decryption of files description:

When encrypting a file, it reads the contents of the file into a string variable, encrypts the string, and finally writes the encrypted string to the file. See:
File operation. txt.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.