Tag: string

C#

Calculate SHA1 hash from String in C#

Calculate SHA1 hash with this function. public String SHA1(String plaintext) { try { System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create(); byte[] bytes = System.Text.Encoding.ASCII.GetBytes(plaintext); byte[] hash = sha1.ComputeHash(bytes);   StringBuilder sb = new StringBuilder(); for (int i = 0; i < hash.Length; i++) sb.Append(hash[i].ToString("x2"));   return sb.ToString(); } catch { return null; } }public String SHA1(String plaintext) { …

C#

Load Picture into PictureBox from base64 String

  String ImageText = "blahblahblahblahblah=="; Byte[] bitmapData = new Byte[ImageText.Length]; bitmapData = Convert.FromBase64String(FixBase64ForImage(ImageText)); System.IO.MemoryStream streamBitmap = new System.IO.MemoryStream(bitmapData); Bitmap bitImage = new Bitmap((Bitmap)Image.FromStream(streamBitmap)); pictureBox1.Image = bitImage;   private String FixBase64ForImage(String Image) { try { System.Text.StringBuilder st = new System.Text.StringBuilder(Image, Image.Length); st.Replace("rn", String.Empty); st.Replace(" ", String.Empty); return st.ToString(); } catch (Exception ex) { return ""; } …

General

Convert String to byte array in C#

Converting a byte array to string (unicode): byte[] b = Encoding.Unicode.GetBytes("unicode characters"); String s = Encoding.Unicode.GetString(b);byte[] b = Encoding.Unicode.GetBytes("unicode characters"); String s = Encoding.Unicode.GetString(b); Converting a byte array to string (ascii): byte[] b = Encoding.Unicode.GetBytes("ascii characters"); String s = Encoding.Unicode.GetString(b);byte[] b = Encoding.Unicode.GetBytes("ascii characters"); String s = Encoding.Unicode.GetString(b);

C#

Convert byte array to String in C#

Assuming that byte array contains data in Unicode format: byte[] data = ….; String data_s = Encoding.Unicode.GetString(data);byte[] data = ….; String data_s = Encoding.Unicode.GetString(data); Assuming that byte array contains data in ASCII format: byte[] data = ….; String data_s = Encoding.ASCII.GetString(data);byte[] data = ….; String data_s = Encoding.ASCII.GetString(data);

Linux, Sniffing, Wireshark

Run Wireshark as a user rather than root – Ubuntu

Messages from wireshark: Running as user “root” and group “root”. This could be dangerous. Lua: Error during loading: [string “/usr/share/wireshark/init.lua”]:45: dofile has been disabled To fix them and run wireshark as normal user and not as root which is very dangerous do the following: sudo chgrp adm /usr/bin/dumpcapsudo chgrp adm /usr/bin/dumpcap sudo chmod 750 /usr/bin/dumpcapsudo …

Java

Encrypt string using AES 128 – Java

You need Apache Commons Codec. package encryption.aes;   import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Base64;   public class aes128 {   public static void main(String args[]) { String plain = "testtesttesttesttesttesttesttest"; String ivkey = "abcdefgh12345678";   byte[] ivraw = ivkey.getBytes(); SecretKeySpec skeySpec = new SecretKeySpec(ivraw, "AES"); …

PHP

Remove last character from string

substr – Return part of a string. Usage: substr($str, 0, -1);substr($str, 0, -1); Example: $str = "Hi there.!"; echo $str.'<br>’; echo substr($str, 0, -1).'<br>’;$str = "Hi there.!"; echo $str.'<br>’; echo substr($str, 0, -1).'<br>’; Output: Hi there.! Hi there.Hi there.! Hi there. mb_substr – Get part of string – Performs a multi-byte safe substr() operation based …