Generate large prime numbers using boost library

Recently I am working on a RSA like algorithm project, which needs to generate large secret prime numbers, e.g. 512 bits, or 1024 bits. Luckily we are using boost library, which is inbuilt lots of functions to assist this generation.

#include <boost/multiprecision/miller_rabin.hpp>
#include <boost/random/mersenne_twister.hpp>

using namespace boost::multiprecision;
using namespace boost::random;

...
//Generate a secret RSA-like 512 bits primes p
cpp_int p = GetPrime();
...
cpp_int GetPrime()
{
    mt11213b base_gen(clock());
    independent_bits_engine<mt11213b, 512, cpp_int> gen(base_gen);

    // Generate some large random primes
    // Note 25 trials of Miller-Rabin 
    // likelihood that number is prime
    cpp_int n;
    do
    {
        n = gen();
    }while (!miller_rabin_test(n, 25));
    return n;
}

FFmpeg is very handy utility to edit multimedia file

FFmpeg is very powerful open source multimedia tool. The source code is hosted at https://github.com/FFmpeg/FFmpeg, and the pre-compiled binary version can be found at https://ffmpeg.zeranoe.com/builds/. However, sometimes, it not user friendly, and you have to remember many parameters.

Here are some samples, which I regular used:

Converting video and audio into different format

$ ffmpeg -i input.mp4 output.avi

Compressing video

$ ffmpeg -i input.mp4 -s 640x360 output.mp4

Changing bitrate

$ ffmpeg.exe -i input.avi -b 3000k  -maxrate 4000k -bufsize 1835k output.avi

Concating video

$ ffmpeg.exe -f concat -i filelist.txt -c copy concat.avi

filelist.txt

file '1.AVI'   
file '2.AVI'   
file '3.AVI'

Remove audio from video file

$ ffmpeg -i input.mp4 -c copy -an output.mp4

Extract audio from video

$ ffmpeg -i input.avi -vn -acodec copy output.aac

Combining audio into video

$ ffmpeg -i video.mp4 -i audio.m4a -c:v copy -c:a copy output.mp4

Cutting the videos based on start and end time

$ ffmpeg -i input.mp4 -ss 00:00:45 -codec copy -t 40 output.mp4

Adding audio into video file on start and time

$ ffmpeg -i video.mp4 -ss 00:04:00 -i audio.mp3 -c copy -t 40 output.mkv

Adding Poster Image to an Audio File

$ ffmpeg -loop 1 -y -i image.jpg -i audio.mp3 -acodec copy -vcode
c libx264 -shortest ouput.mp4

PHP download large file randomly breaks

Last two days, I was trying to make a PHP page to support large file downloading. It’s supposed to be fairly easy because PHP sample code is everywhere.

	
if (file_exists($file) && is_file($file)) 
{
    set_time_limit(0);
    //write HTTP header
    header('Cache-control: private');
    header('Content-Type: application/octet-stream');
    header('Content-Length: '.filesize($file));
    header('Content-Disposition: filename='.basename($file));
    $handle = fopen($file, 'rb');
    while (!feof($handle)) 
    {
        //limited to 256kb/s
        print(fread($handle, 256 * 1024));
        // flush the content to the browser
        flush();
        // sleep one second
        sleep(1);
        if(connection_status() != 0 || connection_aborted()) 
        {
            break;
        }
    }
    fclose($handle);
    exit;
}

However, a small issue causes a big trouble. For unknown reason, HTTP downloading interrupts and breaks randomly, and I couldn’t download the entire file. I checked the PHP code and changed the PHP.ini, but still couldn’t solve the problem. After hours and hours digging, I found this page, https://www.lacisoft.com/blog/2012/01/21/php-download-script-for-big-files/, which magically solves the issue and saves me heaps.

it is :  ob_end_clean();

Here is the final code:

	
if (file_exists($file) && is_file($file)) 
{
    set_time_limit(0);
    //write HTTP header
    header('Cache-control: private');
    header('Content-Type: application/octet-stream');
    header('Content-Length: '.filesize($file));
    header('Content-Disposition: filename='.basename($file));
    //!!!very important!!!
    ob_end_clean();
    $handle = fopen($file, 'rb');
    while (!feof($handle)) 
    {
        //limited to 256kb/s
        print(fread($handle, 256 * 1024));
        // flush the content to the browser
        flush();
        // sleep one second
        sleep(1);
        if(connection_status() != 0 || connection_aborted()) 
        {
            break;
        }
    }
    fclose($handle);
    exit;
}

I read the PHP manual about this function, and guess it could be PHP out buffering overflow or underflow causes the  break issue. If you are facing the similar issue, I hope this can help you as well.

Change IP address & Mask address (C++) Windows

Introduction

This is one way of changing IP address & mask address in C++ using Iphlpapi.h. It’s a simple program, no UI for you, sorry…

  • Use GetAdaptersInfo to get adapters information.
  • Then use GetPerAdapterInfo to get information for every ethernet network card. The information contains IP address.
  • Use DeleteIPAddress remove the old IP address.
  • Use AddIPAddress to add new IP&Mask address.
// ChangeIPAddress("192.168.0.123", "192.168.0.44","255,255,255,0");
bool ChangeIPAddress(char oldIPAddress[], char newIPAddress[], char newMaskAddress[])
{
    DWORD dwRetVal = 0;
    PIP_ADAPTER_INFO pAdapter = NULL;

    ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
    PIP_ADAPTER_INFO pAdapterInfo = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO));
    if (pAdapterInfo == NULL)
    {
        return false;
    }

    if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW)
    {
        free(pAdapterInfo);
        pAdapterInfo = (IP_ADAPTER_INFO *)malloc(ulOutBufLen);
        if (pAdapterInfo == NULL)
        {
            return false;
        }
    }

    if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR)
    {
        pAdapter = pAdapterInfo;
        while (pAdapter)
        {
            if (strcmp(oldIPAddress, pAdapter->IpAddressList.IpAddress.String) == 0)
            {
                IPAddr addr = inet_addr(newIPAddress);
                IPMask mask = inet_addr(newMaskAddress);
                ULONG context, instance;

                if (DeleteIPAddress(pAdapter->IpAddressList.Context) != NO_ERROR
                    || AddIPAddress(addr, mask, pAdapter->Index, &context, &instance) != NO_ERROR)
                {
                    return false;
                }
                return true;
            }
            pAdapter = pAdapter->Next;
        }
        return false;
    }
}

Windows change computer name without reboot

Save this as a batch file, and run from command console

set /p newName=Please input the new computer name:

echo Modifying registry keys...

reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName" /v "ComputerName" /d "%newName%" -f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName" /v "ComputerName" /d "%newName%" -f 
reg add "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet002\Control\ComputerName\ComputerName" /v "ComputerName" /d "%newName%" -f 
reg add "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\ComputerName\ActiveComputerName" /v "ComputerName" /d "%newName%" -f 
reg add "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\ComputerName\ComputerName" /v "ComputerName" /d "%newName%" -f 
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters" /v "Hostname" /d "%newName%" -f 
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters" /v "NV Hostname" /d "%newName%" -f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services\Tcpip\Parameters" /v "Hostname" /d "%newName%" -f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services\Tcpip\Parameters" /v "NV Hostname" /d "%newName%" -f
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v "DefaultDomainName" /d "%newName%" -f
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v "AltDefaultDomainName" /d "%newName%" -f

echo Finish updating registry...

echo Update DNS...
ipconfig /renew
ipconfig /flushdns
ipconfig /registerdns
echo Finish updating DNS...

set computername=%newName%

Windows physical disk image writer in C

My recent project is to write a raw image file into CFast card, and I was looking for a physical disk writer on google. Finally I found this http://m0n0.ch/wall/physdiskwrite.php, but it doesn’t work properly.

  1. GUI is not Englisht
  2. When image size is too big, it doesn’t show the correct size.

Then I decide to make my own physical disk image writer in C.

Be extremely careful to use this tool, incorrect disk number can completely corrupt your disk data! I am not responsible for this.

Download

PhyDiskWrite.zip (4k)

#include <windows.h>

void PrintHelp()
{
    printf("Usage: phydiskwrite [imagefile]\r\n\r\n");
}

HANDLE OpenDisk(int deviceID)
{
    HANDLE hDisk;
    TCHAR diskName[MAX_PATH];
    _stprintf_s(diskName, _T("\\\\.\\PhysicalDrive%d"), deviceID);
    hDisk = CreateFile(diskName, 
        GENERIC_READ | GENERIC_WRITE, 
        FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
    return hDisk;
}

int PrintDisks()
{
    printf("Searching for physical drives...\r\n\r\n");
    DWORD bytes;
    TCHAR diskName[MAX_PATH];
    HANDLE hDisk;
    int selectedDisk = -1;
    for (int i = 0; i < 255; i++)
    {
        hDisk = OpenDisk(i);
        if (hDisk == INVALID_HANDLE_VALUE)
            continue;
        printf("Information for Device %d\r\n", i);

        DISK_GEOMETRY pdg = { 0 };
        if (!DeviceIoControl(hDisk, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &pdg, sizeof(pdg), &bytes, NULL))
        {
            printf("error: Get disk info failed %d\r\n", GetLastError());
            continue;
        }
        ULONGLONG DiskSize = pdg.Cylinders.QuadPart 
            * (ULONG)pdg.TracksPerCylinder 
            * (ULONG)pdg.SectorsPerTrack 
            * (ULONG)pdg.BytesPerSector;
        CloseHandle(hDisk);
        printf("\tByte/Sec %d, Cylinders: %d, Sector/Track %d, Track/Cylinder %d\r\n", 
            pdg.BytesPerSector, pdg.Cylinders, pdg.SectorsPerTrack, pdg.TracksPerCylinder);
        printf("\tDevice:%d, Media Type:%d Disk Size: %.2f (Gb)\r\n\r\n", 
            i, pdg.MediaType, (double)DiskSize / (1024.0f * 1024.0f * 1024.0f));
    }
    printf("Which disk do you want to write?\r\n");
    printf("(Be careful, wrong disk number could corrupt your entire disk!)\r\n");
    scanf("%d", &selectedDisk);
    return selectedDisk;
}

int _tmain(int argc, _TCHAR* argv[])
{   
    if (argc != 2)
    {
        PrintHelp();
        return 0;
    }
    
    int devID = PrintDisks();
    if (devID != -1)
    {
        HANDLE hDisk = OpenDisk(devID);
        BYTE buffer[65536];
        DWORD byteRead, byteWrite, status;
        ULONGLONG totalWrite = 0;
        LARGE_INTEGER fileSize;
        HANDLE hImage = CreateFile(argv[1], 
            GENERIC_READ, FILE_SHARE_READ, NULL, 
            OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
        if (hImage == INVALID_HANDLE_VALUE)
        {
            printf("error: Open file failed %d", GetLastError());
            return -1;
        }

        GetFileSizeEx(hImage, &fileSize);
        while (ReadFile(hImage, buffer, 65536, &byteRead, NULL) && byteRead > 0)
        {
            if (!WriteFile(hDisk, buffer, byteRead, &byteWrite, NULL))
            {
                printf("error: Write to disk failed %d", GetLastError());
                break;
            }
            else
            {
                printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
                totalWrite += byteRead;
                printf("%f%%", totalWrite * 100.0f / fileSize.QuadPart);
            }
        }
        CloseHandle(hImage);
        CloseHandle(hDisk);
    }
    return 0;
}

C# UserControl IPTextBox

I am looking for a TextBox control, which can validate and filter out IP Address. After a bit research, I come with this https://social.msdn.microsoft.com/Forums/vstudio/en-US/4c2aefae-ca6a-4e71-8564-fe73da7107f1/maskedtextbox-formatting-for-an-ip-address?forum=csharpgeneral. It’s easy to implement and simple to use. Just like normal TextBox.

iptextbox

using System.Windows.Forms;
class IPTextBox : TextBox
{
    public IPTextBox() : base()
    {
        SetStyle(ControlStyles.ResizeRedraw, true);
    }

    protected override CreateParams CreateParams
    {
        get
        {
            var cp = base.CreateParams;
            cp.ClassName = "SysIPAddress32";
            return cp;
        }
    }
}

Another complete solution can be found http://www.sanity-free.org/127/an_ipaddress_control_the_win32_sysipaddress32_control_in_csharp.html

Microsoft Visual Studio 2013 Update 5 direct download link for full ISO

Visual Studio 2013 Update 5 is now released and maybe you are also struggling where to find the download link for full ISO.

Direct link: https://go.microsoft.com/fwlink/?LinkId=519391 (6GB in size!)

ISO also can be downloaded from http://download.microsoft.com/download/A/F/9/AF95E6F8-2E6E-49D0-A48A-8E918D7FD768/vs2013.5.iso.

This is the link for Visual Studio 2013 full ISO

Direct link: http://go.microsoft.com/fwlink/?LinkId=320673

 

Visual Studio Ultimate 2013 with Update 5 (x86) – DVD (Chinese-Simplified)
Language Chinese – Simplified
Size: 5.18GB
SHA1:87269A0C8AE4ACBF515532F0759F6029167A318B
Direct link: http://download.microsoft.com/download/9/3/E/93EA27FF-DB02-4822-8771-DCA0238957E9/vs2013.5_ult_chs.iso?type=ISO

Visual Studio Professional 2013 with Update 5 (x86) – DVD (Chinese-Simplified)
Language: Chinese – Simplified
Size: 5.14GB
SHA1:BDD95F90F1BBC8214B64E4E8D36170CCD4C7A845
Direct link: http://download.microsoft.com/download/6/F/2/6F297D22-B6A5-428F-AFC3-5A887FF036BE/vs2013.5_pro_chs.iso?type=ISO

Visual Studio Ultimate 2013 with Update 5 (x86) – DVD (English)
Language: English
Size: 4.82GB
SHA1:918EA4A911858D32C977148026E7EDB7B238E6F6
Direct link: http://download.microsoft.com/download/E/2/A/E2ADF1BE-8FA0-4436-A260-8780444C8355/vs2013.5_ult_enu.iso?type=ISO

Visual Studio Professional 2013 with Update 5 (x86) – DVD (English)
Language: English
Size: 4.77GB
SHA1:8C77219E81F50191F193586CD8766D21B7FD3740
Direct link: http://download.microsoft.com/download/F/2/E/F2EFF589-F7D7-478E-B3AB-15F412DA7DEB/vs2013.5_pro_enu.iso?type=ISO

Re-config Brother HL-2130 sharing after Lenovo EZ firmware update

Follow last post Share Brother HL-2130 through Lenovo Iomega EZ Media & Backup Center USB port.

Recently I update Lenovo EZ firmware, but unfortunately Brother HL-2130 sharing stops working.

After some research, I found that CUPS web page is not configured for external IP address.

By default you are only able to access CUPS admin web interface from localhost by going to https://localhost:631.
But to access this page from another remote machine, you need to first make some edits to the cups conf file. So do the following:

1. Edit the cups configuration file located in /etc/cups/cupsd.conf and make the section that looks like:

    # Only listen for connections from the local machine.
    Listen localhost:631
    Listen /var/run/cups/cups.sock

look like this:

    # Listen on all interfaces
    Port 631
    Listen /var/run/cups/cups.sock

2. Then you need to modify the Apache specific directives to allow connections from everywhere as well. Replace the followin section:

    <Location />
    # Restrict access to the server...
    Order allow,deny
    </Location>
    <Location /admin>
    # Restrict access to the admin pages...
    Order allow,deny
    < /Location>
    <Location /admin/conf>
    AuthType Default
    Require user @SYSTEM
    # Restrict access to the configuration files...
    Order allow,deny
    </Location>

to:

    <Location />
    # Restrict access to the server...
    Order allow,deny
    Allow all
    </Location>
    <Location /admin>
    # Restrict access to the admin pages...
    Order allow,deny
    Allow all
    </Location>
    <Location /admin/conf>
    AuthType Default
    Require user @SYSTEM
    # Restrict access to the configuration files...
    Order allow,deny
    Allow all
    </Location>

3. Restart your cups daemon:

    # /etc/init.d/cupsd restart

4. You should now be able to log into cups on your server with:

https://yourserverip:631

Directly Download Apk from Google Play Store on PC/Mobile

Website #1. Evozi Apk Downloader

Website #2. ApkLeecher.com

Website #3.  Downloader-Apk.com

Website #4. Apk-Freedownload.com
Thanks albertine samy!
Website #5. apkappownload.com


Two more new website added:

Website #6. Apk-Dl.com

Website #7. Apkpure.com

Thanks Abi!

Website #8. APKMirror.com

Website #9. APKMonk.com