Thursday, 12 January 2023

AWS Certificate Manager - Import Certificate

How to import a certificate.

Use Key Store Explorer to open the cert and export keypair in PEM format 




This will save a file cert.pem. Make sure you do not set the password for pem file. Use the details and add in aws acm to import cert. Example below

1) Private Key 


 

2) Cert Body 





EXAMPLE FROM AWS ACM 








Sunday, 23 May 2021

JAVA UTF-8 encoding

Many times developers are stuck with issues where arabic / hebrew / chinese characters in their application either look as junk characters or ???????? in their Backend Java based applications. 


 Why Does this happen. 

 1) Application Encoding Settings. Make sure your application is set to use the right encoding mechanism. the most important. (UTF-8) For example :- setting default in encoding in Apache Velocity

 2) Validating JVM Encoding Sometimes servers like tomcat , jboss etc might need to be started with an additional parameter for encoding For Eg.
3) Making Sure your IDE has the right encoding settings.

Sunday, 14 February 2021

JAVA RSA 256 Private Key JWT Creation - OpenSSL Key Pair

Steps to create RSA256 Keypair using OpenSSL
You need to convert your private key to PKCS8 format using following command: This will ensure your code to read the PEM openssl pkcs8 -topk8 -inform PEM -outform DER -in C:\certs\2021.key -nocrypt > pkcs8_key.key

Please note that the rsa key you have need to be converted to der format online 
can be done using below link
https://decoder.link/rsa_converter
 
       
       
import java.io.UnsupportedEncodingException;
import java.security.Key;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Base64;
import java.util.Date;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.impl.TextCodec;

public class JwtTokenUtilRSA {

	public static void main(String[] args) throws Exception {

		System.out.println(createAccessJwtToken());

	}

	public static String createAccessJwtToken() throws Exception {

		SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.RS256;
		Claims claims = Jwts.claims().setSubject("EXXXXXXXXciOSbWCwG");
		claims.put("aud", "XYZ.com");

		LocalDateTime currentTime = LocalDateTime.now();

		Key signingKey = getPrivateKey2();

		String token = Jwts.builder().setClaims(claims).setIssuer("issuer")
				.setIssuedAt(Date.from(currentTime.atZone(ZoneId.systemDefault()).toInstant()))
				.signWith(SignatureAlgorithm.RS256, signingKey).compact();

		return token;
	}
    
    	private static PrivateKey getPrivateKey2() throws NoSuchAlgorithmException, InvalidKeySpecException {
	    String rsaPrivateKey = "-----BEGIN PRIVATE KEY-----" +
	            "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDJPwCTr8LDSi/7" +
	            "f0PXtiGZHcT/7B2AAIlATC178lp1mI42Exmw2X/yOiNU6mkiYQxxkv1vr8blViga" +
	            "mfhwCjahzI9ix0PzyEjVxfFWYlnfoorNGOwWgAqD3kzgnnUVPQXFmYRjtVD43sM4" +
	            "54h9Q1nFfPnVe9vgyckKI/p2nVUyZZlMo36/PRZdAV15vVvuvBJLW3gW3Xb2OuLy" +
	            "sF7/NEQe2zi5A87j4pbeRpBmGWzmloF3dNjwtCZAiSmX01nLe0bZYJIHgDgmPDKt" +
	            "qond8Pe1+zEDDf6fyq9mfT/naSOL1mQbLK2VQdGUGWmNgIpTgPEpolv3AdwbOtMx" +
	            "47GiOBPdAgMBAAECggEAeimjiQNNoEzdmbakkUJ/XLpEhvJm6ffpWT2k8rOok/Aa" +
	            "i9rGYqN1Vq5SGk79qR/udNc8v1qdy0Okrpk+xYx9o3RQqi66Ijc3d4VkPgMP+YBq" +
	            "75usXPA28du1ITkva8gSQ+QwCHLdh2V6boWmRhvnV8nIwv4f+HGld2FlvjE9sYwF" +
	            "Zwuo51G5TBkMnLaklHzMB18sTrcC4EmFvFfEsH6lzY3XaZXbMiwV802VbTasiXGi" +
	            "+HDMfUrxza1AVUz4zqlZhWvJL2c9RinLnSsS1eZ/FGbRrjZbfyE/IMFiMapP9LGi" +
	            "Cw1Gdt9NAwWtx78lgOrrW1+YYLVJEUlXZmPcPBkCZQKBgQDrP9tl10I8wlp6XyRY" +
	            "1Ge2xqAaSuHSwIMdSw9eRyO9K/CCV7hmzNOUvVe7U3Q5knxD5aoIul2Behn6BzDA" +
	            "hovtQALmFn0lmwsb7IKa/zM1o3BM5qrSwCTXSiBfKyi207jCK35wJPAJeemCl1JQ" +
	            "NY0Fug31/+SPLcGyO0HIcxaZrwKBgQDa/1HEHQ6yn2Eb3H/KtNrh5CQYu004LBaz" +
	            "ISsR7JwfyHscrE34Fx1eyEuJDVehGC30U61zh5h/dibr9M4wJqUTvJuhJh3X/w7r" +
	            "w1vWSE9rOJsOFenjWVv+2VO6TkaayWPDC79mCR/UBIXULOdMwi6NhDd8eqVraZzg" +
	            "b5TTHntqMwKBgB6KP7VM6LBymsRgLZvKl1xHFOnU5tQxYdQftnMK0Tzxw1tof+v4" +
	            "k08v9X/AKcp/ldpKqnRPnWv9qfmnjVCxJuE5+iZcrQqAwkRxw9LTZpaEDUQufea2" +
	            "QxIf8WvpcnOpsMakVtnAMdlJFmN5uZnoGdKwgLd67PUZf5ff0X5wUnLJAoGBAM60" +
	            "Itc6nf7XFG+ZxjzsS5TfI3r8fzw/0QUuj1qRvCL7tcgwkj9SgfROlY08vLNfATEW" +
	            "MidTmwrj2ITqQhuMvk9MvJB0S8LuCYValbpB8kQ/34qCAIsIcx6uvqqg6196KuGU" +
	            "mwmSHyqdQjwsTf1TY2FLpJunn68stlfYud4zbUWlAoGBAMCxCk2QabirfKSpKeXY" +
	            "On52j23o/8g2OfDPtd1cS30mTtDUStv3Sqr5Tegz+/7IwugORQujae4b20qgBSds" +
	            "NP9gb8RGmxLryZsEfwV1qppx0d1gEcyYcySYBhz+9qKyByeD8lotbZ0WGMu/UAg/" +
	            "ftaiqVv/CKA/h256T/5uThS3" +
	            "-----END PRIVATE KEY-----";

	    rsaPrivateKey = rsaPrivateKey.replace("-----BEGIN PRIVATE KEY-----", "");
	    rsaPrivateKey = rsaPrivateKey.replace("-----END PRIVATE KEY-----", "");

	    PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(rsaPrivateKey));
	    KeyFactory kf = KeyFactory.getInstance("RSA");
	    PrivateKey privKey = kf.generatePrivate(keySpec);
	    return privKey;
	
	}

}
       
       
       
       
       
          
 

Monday, 11 May 2020

Security File Type Detection with Apache Tikka - OWASP

       
import java.io.IOException;

import java.io.InputStream;

import org.apache.tika.detect.DefaultDetector;

import org.apache.tika.detect.Detector;

import org.apache.tika.metadata.Metadata;

import org.apache.tika.mime.MediaType;



public class FileypeDetection {

 

 public static void main(String[] args) throws IOException {

   InputStream stream = FileypeDetection.class.getClassLoader()

         .getResourceAsStream("C:\\template.pdf");

  System.out.println(detectDocTypeUsingDetector(stream));

 }


 public static String detectDocTypeUsingDetector(InputStream stream)

     throws IOException {

       Detector detector = new DefaultDetector();

       Metadata metadata = new Metadata();

   

       MediaType mediaType = detector.detect(stream, metadata);

       return mediaType.toString();

   }



}









   

 

AWS Certificate Manager - Import Certificate

How to import a certificate. Use Key Store Explorer to open the cert and export keypair in PEM format  This will save a file cert.pem. Make ...