Encryption algorithm
public static string base64encrypt (string saltstr,string SOURCESTR) { string targetstr = string. Empty; sourcestr = saltstr + sourceStr; if ( SOURCESTR.LENGTH&NBSP;%&NBSP;3&NBSP;==&NBSP;1) {sourceStr = sourceStr + "=" + "="; } else if (sourcestr.length%3==2) { sourceStr = sourceStr + "="; } byte[]&nBsp;sourcebyte = unicodeencoding.unicode.getbytes (SOURCESTR); int count = sourceByte.Length; List<byte> by = new List<byte> (); for (int current = 0; current < count; current++) { by. AddRange (ToBit (sourcebyte[current)); } byte[] bit8 = new byte[8]; byte[] bit24 = new byte[24]; //temp int i = 0; Int j = 0; list<byte > targetByte = new List<byte> (); foreach (Byte b in by) { bit24[i] = b; if (i == 23) { bit24[i] = b; targetbyte.addrange (Bit24tobit32 (Bit24)); i =-1; } i ++; } i = 0; byte[] byy = new byte[targetbyte.count / 8]; foreach (Byte b in targetbyte) { bit8[i] = b; if (i == 7) { bit8[7] = b; byy[j++] = (Byte) tonum (BIT8); i = -1; } i++; } targetstr = unicodeencoding.unicode.getstring ( BYY); return targetstr; }
decryption algorithm
Public static string base64unencrypt (STRING&NBSP;SALTSTR,STRING&NBSP;SOURCESTR) { String targetstr = string. empty; byte[] temp = UnicodeEncoding.Unicode.GetBytes (SOURCESTR); //Turn byte byte[] sourceByte=new byte[0]; list<byte> targetbyte = new list<byte > (); byte[] bit8 = new byte[8]; int i = 0; int j = 0; for (int m = 0; m < temp. length; m++) { sourceByte = Sourcebyte.concat (ToBit (Temp[m])). ToArray (); } foreach (byte b in sourcebyte) { bit8[i] = b; if (i == 7) { bit8[i] = b; targetbyte.addrange (bit8. Tolist<byte> (). GetRange (2,6)); i = -1; } i++; } i = 0; byte[] byy=new byte[targetByte.Count/8]; foreach (Byte b in targetbyte) { bit8[i] = b; if (i == 7) { bit8[7] = b; byy[j++] = (byte) tonum (BIT8); i = -1; } i ++; } targetstr = unicodeencoding.unicode.getstring (BYY); targetstr = targetstr.trimend (' = ') ; targetstr = Targetstr.substring (saltstr.length - 1); return targetstr; }
required functions
public static byte[] bit24tobit32 (BYTE[]&NBSP;BIT24) { byte[] bit32 = new byte[32]; int j = -1; for (int i = 0; i < 24; i++) { if (i % 6 == 0) { bit32[++j] = 0; bit32[++j] = 0; bit32[++j] = bit24[i]; } else { bit32[++j] = bit24[i]; } } return bit32; } &nBsp; public static int tonum (Byte[] by) { int num = 0; int i = 7; foreach (Byte b in by) { if (b != 0) { num = num + (int) Math.pow (2,i); } i--; } return num; } public static byte[] tobit ( BYTE&NBSP;NUM1) { byte[] bit = new byte[8]; int num = convert.toint32 (NUM1); if (num > -1 | | num < 128) { for (int i = 0; i < 8; i++) { bit[7 - i] = (Byte) ((num - (num = num >> 1) << 1)); } } else { throw new indexoutofrangeexception ("Out of range"); } return bit; }
test code
console.writeline ("Input required base64 encrypted string"); string saltstr = string . empty; string sourcestr = string. Empty; console.writeline ("Input add salt string"); saltstr = console.readline () ; console.writeline ("Input required base64 encrypted string"); sourcestr = console.readline (); string encryptstr = Base64encrypt (SALTSTR,&NBSP;SOURCESTR); consoLe. WriteLine ("Encrypted string" +encryptstr); Console.WriteLine ("Input Add salt string"); saltstr = console.readline (); Console.WriteLine ("Input needs base64 decryption string, character is not lost"); //sourcestr = console.readline (); string unencryptstr = base64unencrypt (SALTSTR,&NBSP;ENCRYPTSTR); console.writeline ("String after Encryption" + UNENCRYPTSTR);
Test results
(manual) Base64 encryption and decryption