Pages

This tutorial covers the basics of the science of cryptography. It explains how programmers and network professionals can use cryptography to maintain the privacy of computer data. Starting with the origins of cryptography, it moves on to explain cryptosystems, various traditional and modern ciphers, public key encryption, data integration, message authentication, and digital signatures.

Sunday 19 April 2020

[SOLVED] java.io.IOException: keystore password was incorrect

PROBLEM
Exception in thread "main" java.io.IOException: keystore password was incorrect
at sun.security.pkcs12.PKCS12KeyStore.engineLoad(PKCS12KeyStore.java:2059)
at sun.security.provider.KeyStoreDelegator.engineLoad(KeyStoreDelegator.java:238)
at sun.security.provider.JavaKeyStore$DualFormatJKS.engineLoad(JavaKeyStore.java:70)
at java.security.KeyStore.load(KeyStore.java:1445)
at getprivatekeyfrompkcs12.GetPrivateKeyFromPKCS12.main(GetPrivateKeyFromPKCS12.java:21)

Caused by: java.security.UnrecoverableKeyException: failed to decrypt safe contents entry: javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.

SOLVED
you must enter the passphrase correctly.

Friday 17 April 2020

ANALYSIS OF HARDWARE SECURITY MODULE UTILIZATION FOR CERTIFICATION AUTHORITY SERVICES

  ANALYSIS OF HARDWARE SECURITY MODULE UTILIZATION FOR CERTIFICATION AUTHORITY SERVICES 

zaenal suhardono

The reliability of the development and application of information technology must be guaranteed so therefore it is always ready to use in accordance with the level of service required, as well as the level of data security has to be in accordance with laws and regulations. Security is an absolute aspect in the implementation of digital certification to build trust within users. However, the higher level of security applied to a system will reduce the system's performance. Therefore, the writer conducted a research on the security analysis of utilizing HSM and tested the performance of digital certificate issuance with and without HSM. Security analysis shows that the utilization of HSM can increase a higher level of security since it provides physical and logical security when generating, storing and using the private keys. In addition, with its anti-tamper feature, private keys are protected so they will not presented outside the HSM in a plain state. Therefore, keys are safe from unauthorized parties. The performance test shows that generation of keys using HSM takes 306,6 ms, which is 163,8 ms slower without HSM.


Wednesday 15 April 2020

Java Cryptography : Encrypt and Decrypt Video Using AES 256 Java

I wrote a sample program to encrypt and decrypt a video file. 

public static String initVector = "1234567812345678";

//Method Encrypt Video   
  public static void encrypt(String key, String filePath, String outPath) throws FileNotFoundException, IOException {
        try {
            IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);

            try (FileInputStream fis = new FileInputStream(filePath)) {

                FileOutputStream fos = new FileOutputStream(outPath);
                // Write bytes
                try (CipherOutputStream cos1 = new CipherOutputStream(fos, cipher)) {
                    // Write bytes
                    int b;
                    byte[] d = new byte[8];
                    while ((b = fis.read(d)) != -1) {
                        cos1.write(d, 0, b);
                        
                    }
                    // Flush and close streams.
                    cos1.flush();
                }
            }

        } catch (UnsupportedEncodingException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException ex) {

            System.out.print(ex.getMessage());
        }
    }

//Method Decrypt Video    
public static void decrypt(String key, String outPath, String inPath) throws FileNotFoundException, IOException {
        try {
            IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);

            FileInputStream fis = new FileInputStream(outPath);

            CipherInputStream cis1;
            try (FileOutputStream fos = new FileOutputStream(inPath)) {
                cis1 = new CipherInputStream(fis, cipher);
                //     CipherInputStream cis2 = new CipherInputStream(fis, cipher2);
                int b;
                byte[] d = new byte[8];
                while ((b = cis1.read(d)) != -1) {
                    fos.write(d, 0, b);
                }   fos.flush();
            }
            //     CipherInputStream cis2 = new CipherInputStream(fis, cipher2);
            cis1.close();

        } catch (UnsupportedEncodingException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException ex) {

            System.out.print(ex.getMessage());
        }

    }

//Main Class     public static void main(String[] args) throws Exception {
        String key = "uxjdNijiyJDyOJ3RuxjdNijiyJDyOJ3";

        System.out.print("ENCRYPTION :");

        encrypt(key, "C:\\Users\\CRYPTOGRAPHY\\Documents\\EncryptFileAES.mp4", "C:\\Users\\CRYPTOGRAPHY\\Documents\\Encrypted.enc");
        System.out.println("DONE");
        
        
        System.out.print("DECRYPTION :");
        decrypt(key, "C:\\Users\\CRYPTOGRAPHY\\Documents\\Encrypted.enc", "C:\\Users\\CRYPTOGRAPHY\\Documents\\DecrypFile.mp4");
        System.out.println("DONE");
    }


Tuesday 14 April 2020

[SOLVED] Invalid AES key length: 31 bytes

PROBLEM :
Invalid AES key length: 31 bytes

CAUSE
key length AES256 must 32 bytes

SOLVED
Edit key until key length 32 bytes

Friday 10 April 2020

(SOLVED) MessageDigest.getInstance("SHA256") java.security.NoSuchAlgorithmException: SHA256

PROBLEM :
MessageDigest.getInstance("SHA256") java.security.NoSuchAlgorithmException: SHA256 MessageDigest not available

java.security.NoSuchAlgorithmException: SHA256 MessageDigest not available
 at sun.security.jca.GetInstance.getInstance(GetInstance.java:159)
 at java.security.Security.getImpl(Security.java:730)
 at java.security.MessageDigest.getInstance(MessageDigest.java:167)

SOLVED : 

Rename MessageDigest.getInstance("SHA256")  with MessageDigest.getInstance("SHA-256")

Tuesday 10 March 2020

Command Generate Certificate Signing Request (CSR) with Openssl

openssl req -nodes -newkey rsa:2048 -keyout example.key -out example.csr -subj "/emailAddress=testing@test.id/C=ID/O=Personal/CN=testing123"

Return --> example.key
-----BEGIN PRIVATE KEY-----MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQC+GY97PIu37Nmo1xCvzQp7z53S2OtmKV2c1VdthSze03Ce9Fal9Wg4Y17uo3znnYdRESKBzBPEBjVuFiv7tEf+VC2osfgD83rvIIFOjzQMozdyX6JWitmce8x+P+be1vKE3+HlG5z6r1zGuORC3YNDaSp0rzuMZRo+wi053zLVngI1hXW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eROBRXQtDYfhR0VXSSB51rmK7Vc4J0t3VlMyPgNav+JewoygspkSYOCxJ4IcsHSQ0ATuLu52+6DfLhJ42ukocyRnSiUp8WYDRwK+yCPkqojtjhS19xb0v85Lz7OgHy62xXRmWiBR6SZmNSYaafzzwFkS5QfQ2eZnIGubas+u6uRVuSKkJPWH/ga1g==-----END CERTIFICATE REQUEST-----