Showing posts with label Cryptographic. Show all posts
Showing posts with label Cryptographic. Show all posts

Sunday, March 19, 2023

golang: compute PBKDF2 of a password


 package main

import (
  "bytes"
  "crypto/rand"
  "crypto/sha256"
  "encoding/hex"
  "fmt"
  "golang.org/x/crypto/pbkdf2"
  "golang.org/x/term"
  "log"
  "strings"
  "syscall"
)

func main() {
  fmt.Print("Input password: ")

  password, err := term.ReadPassword(int(syscall.Stdin))

  if err != nil {
    log.Fatal(err)
  }

  fmt.Print("\nConfirm password: ")

  confirmPw, err := term.ReadPassword(int(syscall.Stdin))
 
  if err != nil {
    log.Fatal(err)
  }
 
  if !bytes.Equal(password, ConfirmPw) {
    log.Fatal("Error: inputs mismatch")
  }

  salt := make([]byte, 32)

  _, err = rand.Read(salt)

  if err != nil {
    log.Fatal(err)
  }

  pwPbkdf2 := pbkdf2.Key(password, salt, 10240, 32, sha256.New)

  fmt.Println("\nSalt: ", strings.ToUpper(hex.EncodeToString(salt))
  fmt.Println("PBKDF2: ", strings.ToUpper(hex.EncodeToString(pwPbkdf2))
}




Tuesday, May 5, 2020

java.security.NoSuchProviderException - no such provider: BC


1. Download the latest BouncyCastle library, e.g. bcprov-jdk15on-165.jar

2. Copy the JAR file to $JAVA_HOME/jre/lib/ext/.

3. Edit file $JAVA_HOME/jre/lib/security/java.security. Add the following:
security.provider.10=org.bouncycastle.jce.provider.BouncyCastleProvider

(Note: if you have 10 providers or more already, adjust the number 10 to a higher number accordingly.)

Ref: https://docs.oracle.com/cd/E19830-01/819-4712/ablsc/index.html

Sunday, June 14, 2015

Free SSH Servers for Windows


KpyM Telnet/SSH Server

KpyM Telnet/SSH Server is open source. It uses the BSD license.

The downlaod URL is http://www.kpym.com/2/kpym/download.htm and the license can be viewed there.

There is not GUI interface for it. Neither a GUI configuration.

The password authentication is straightforward but the public key authentication is somehow strange. How to use the public key authentication is described in their forum. You need to add the public key into a configuration file c:\program files\kts\publickey_logon.ini. And you also need to write the login credentials (username and password) together with the public key. It seems the software does not really do the public key authentication. Instead, it uses the public key to find the password in the configuration file and authenticate the user with the password. The big problem is that the password must be in clear text in the file.

Copssh free version

Copssh comes with a free version and a commercial version. The free version can be downloaded at https://www.itefix.net/content/copssh-free-edition.

The free version does not support Windows Server. And it allows only one local user.

It has a GUI for the configuration. You can add users there and set up public key authentication etc.

freeSSHd

freeSSHd can be downloaded from http://www.freesshd.com/?ctt=download. Although it is free to use, the source code is not free.  The license information is found here http://www.freesshd.com/?ctt=terms.

A step by step installation and configuration document can be found on IBM's: ftp://ftp.software.ibm.com/software/iea/content/com.ibm.iea.wpi_v6/wa/6.2/FTP/WBPMv62_IEA_AdapterInstallConfigureSSHServerLab.pdf.

It has a GUI setup and we can also use the public key authentication. They are all written in detail in IBM's document.

Their website promotes a commercial product with source code, which they claim contains the source code of freeSSHd.

Monday, June 1, 2015

Speeding ticket issued with SSL/TLS handshake protocol (fiction)


Servaas is stopped by a cop because of speeding. In this virtual world, people communicate in secure ways. Here is the conversation between the cop (C) and Servaas (S):

C: Hello. You are stopped because of speeding. Before we start, I have assigned a random case number RNC for this case.

   You can access your case in the speeding ticket system by Windows, iOS, Android. Which one are you going to use?

   And, can I see your driver's license?

S: Hello. I am test driving this car so its plate number is like a random number to me. Oh, the plate number is RNS.

   I am going to use Android to access my case in the system.

   Here is my driver's license.

   (Being bold) Can I see your badge?

C: (Checking the driver's license.)

   Here is my badge.

S: (Checking the badge.)

C: Here is the description of your case with my signature and badge number.

S: (Checking the description and the signature.)

C: Now I am giving you the encrypted ticket number. It is encrypted by your driver's license number.

S: (Decrypt the ticket number with his own birthday date and time -- which should be private to himself in this virtual world.)

Both C & S: (Compile RNC, RNS and Ticket# into a key to login to the speeding ticket system to get connected and start chatting.)

Thursday, October 3, 2013

To compute a hash of a string


On Linux, to compute the SHA1 for a string "my_string", run:
   $ echo -n "my_string" | openssl dgst -sha1

To compute the SHA-256, run:
   $ echo -n "my_string" | openssl dgst -sha256

To compute the MD5, run:
   $ echo -n "my_string" | openssl dgst -md5

etc...




Thursday, September 26, 2013

Use OpenSSL to verify the SHA1 and MD5 of a file


When you download a file from the internet, the web site may also provide the SHA1 or MD5 of the file as a way to verify the integrity of the file. If you have OpenSSL installed in your machine, it has a handy dgst command to compute the SHA1 and MD5 of a file.

To compute the SHA1 of a file:
    openssl dgst -sha1 filename

To compute the MD5 of a file:
    openssl dgst -md5 filename

If you like to have a little more fun, you can even build your own with a very simple Java program.

Thursday, March 22, 2012

Generate RSA key pair with OpenSSL


The OpenSSL web site tells us to generate the RSA keys by running (without using a protecting password for the keys):

openssl genrsa -out privkey.pem 2048

That command outputs the private key in the default PEM format. It is said that "With OpenSSL, the private key contains the public key information as well, so a public key doesn't need to be generated separately." -- sure, but please show me how to extract the public key from it!

It is the openssl rsa command that will do it:

openssl rsa -pubout -in privkey.pem -out pubkey.pem

This post shows how to encrypt/decrypt with the RSA keys. This post gives an example of RSA decryption in C++.

Wednesday, March 7, 2012

C++: Generate SHA-512 hash


The EVP interfaces from OpenSSL library can compute the SHA hash in C/C++ programming. The document in OpenSSL web site gives an example in http://www.openssl.org/docs/crypto/EVP_DigestInit.html. So does the MAN page of the function EVP_DigestInit().

It is not a single function call that can do the job. The serial EVP functions involved are:

 const EVP_MD *md = EVP_get_digestbyname("sha512");
 EVP_MD_CTX_init(&mdctx);
 EVP_DigestInit_ex(&mdctx, md, NULL);
 EVP_DigestUpdate(&mdctx, mess1, strlen(mess1));
 EVP_DigestUpdate(&mdctx, mess2, strlen(mess2));
 EVP_DigestFinal_ex(&mdctx, md_value, &md_len);
 EVP_MD_CTX_cleanup(&mdctx);

In the document, EVP_MAX_MD_SIZE is defined as 36. Actually it is 64 in the real OpenSSL library. Perhaps the upgrade it because the result of SHA-512 is 64 bytes (512 bits).

To verify the output of your implementation, you can use the openssl command line:

 echo -n "string to be hashed" | openssl dgst -sha512

The -n option of the echo command is important. Without it, the trailing newline of the echo command will cause a different result.

Thursday, June 23, 2011

OpenSSL: to encrypt/decrypt a file with RSA public/private key


To encrypt a file with RSA public key, run command:

openssl rsautl -in plain_file -out encrypted_file -inkey public_key.pem -pubin -encrypt

where plain_file is the file you want to encrypt, encrypted_file is the output. public_key.pem is the RSA public key. -pubin tells that the key file is a public key.

To decrypt the output encrypted_file from the last command, we can only use the paired private key.

openssl rsautl -in encrypted_file -out decrypted_file -inkey private_key.pem -decrypt

If private_key.pem and public_key.pem are not paired, trying to decrypt will get errors. To generate a pair of RSA private and public keys, see this post.

On Linux, you can run man rsautl to see the manual of the commands.

Now, run command diff plain_file decrypted_file to verify that they are the same.

We also have a snippet of C++ code in this post to show how to decrypt encrypted_file with the OpenSSL library.

Tuesday, June 21, 2011

OpenSSL: decrypt with RSA private key in C++


The following C++ code demonstrates how to decrypt with RSA private key with OpenSSL library. For practical use, you will need to add error handling.

For how to use the OpenSSL commands to encrypt and decrypt files, see this post.

#include <fstream>
#include <openssl/pem.h>
#include <openssl/rsa.h>

char *private_key_file_name = "private_key.pem";
char *encrypted_file_name = "encrypted_file";
char *decrypted_file_name = "decrypted_file";

int main()
{
    FILE *fp = fopen(private_key_file_name, "r");

    RSA *rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);

    fclose(fp);

    std::ifstream enc_file;

    enc_file.open(encrypted_file_name, std::ifstream::in);

    int keysize = RSA_size(rsa);

    std::auto_ptr<unsigned char> rsa_in(new unsigned char[keysize * 2]);
    std::auto_ptr<unsigned char> rsa_out(new unsigned char[keysize]);

    memset(rsa_in.get(), 0, keysize * 2);
    memset(rsa_out.get(), 0, keysize);

    enc_file.read(reinterpret_cast<char *>(rsa_in.get()), keysize * 2);

    int rsa_inlen = enc_file.gcount();

    int rsa_outlen  = RSA_private_decrypt(
            rsa_inlen, rsa_in.get(), rsa_out.get(),
            rsa, RSA_PKCS1_PADDING);

    std::ofstream dec_file;

    dec_file.open(decrypted_file_name, std::ifstream::out);

    dec_file.write(reinterpret_cast<char *>(rsa_out.get()), rsa_outlen);

    enc_file.close();
    dec_file.close();

    RSA_free(rsa);

    return 0;
}

 
Get This <