Skip to content

Commit

Permalink
add some test classes
Browse files Browse the repository at this point in the history
  • Loading branch information
tvd12 committed Nov 11, 2023
1 parent 345497f commit 2e4fed6
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.tvd12.ezyfox.testing.security;

import com.tvd12.ezyfox.security.EzyAsyCrypt;
import com.tvd12.ezyfox.security.EzyBase64;
import com.tvd12.test.base.BaseTest;

public class EzyAsyCryptEncryptDecryptTest extends BaseTest {

public static void main(String[] args) throws Exception {
String publicKey =
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwmI6UDOjfjs/aDdn7hm2CvFs/Wc1E4C4GAzy20/ggLKZT+rf1EBe7034osMaFHne/FVQ56UNbphIYZtll6aaB+Z/r+Sm0oyOmr7S0wMftwBNaCnJN7rR4AlXIEJwfv9LZGhmt0VFvXdYm/0uRpmUaX+oU8mCCD/yxZp1w/m1x4TeUFMSpBMtWq+KcodbqBmNu7nLxWrbDkygxK1970dPADurT5MSCxdFR2sdkDNRLSYtAmXq6rMIsyhL8U+N0510zizVTlzcgB5p6vfDmUYocKy5l3LAYrk2JwPZ+c0Ik1jYcL8fU8sCSCwoXgY0fnXmfTDCoJ+xwd1nz7foiPLDwwIDAQAB";
String privateKey =
"MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCD+eQdACBwhVvLN82bv0hKBz34bfcX226jMUpJ21SvFevK/yqXDg/F85sxpfAZG0cKKAdywx8byqakzVMycZilzNGMLX+nPGkZ4pfM4fKEJuNesp0bsUtWDYdtvXTFB/TtDqIjWvVr4GvwsPiKuky6FmePDv3p1sog3taEZ+BEh8Wk8AnfIgHEhk4afNcF1GGZNQVHxxEBU+7laV8djkp0+v28XHafN58DSjhFuNUUMOEsWRqDS932BhOFUT73ie/GRJ7XSXf79G5duKUkbsnBgktMeVLm4wbCc4tGxqoGTEgtPKYGrEw7ZnZqAY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Wk5sCgYAp04lG1BYijnh7jGzEXtjO//ItmV7us48atzfbwH/DiT85oNmcE4PdZpeNr3Whn/tpCmBCvX5GnQrEI9p63XWSmkOiCcd7UrukP7WHzJ8VWyGyUSbK1Zke+EoPpAoQ9RfHXpkQkyT2y4pUWTjleqTLggRPlsxFsIuOcl/F2SVghA==";
EzyAsyCrypt asyCrypt = EzyAsyCrypt.builder()
.publicKey(EzyBase64.decode(publicKey))
.privateKey(EzyBase64.decode(privateKey))
.build();
byte[] encryptedBytes = asyCrypt.encrypt("Hello");
System.out.println(encryptedBytes.length);
String encryptedText = EzyBase64.encode2utf(encryptedBytes);
System.out.println(encryptedText);
String decryptedText = new String(
asyCrypt.decrypt(EzyBase64.decode(encryptedText))
);
System.out.println(decryptedText);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@
import com.tvd12.test.base.BaseTest;
import org.testng.annotations.Test;

import java.security.KeyPair;

public class EzyKeysGeneratorTest extends BaseTest {

public static void main(String[] args) {
KeyPair keyPair = EzyKeysGenerator
.builder()
.build()
.generate();
System.out.println(keyPair.getPublic());
}

@Test(expectedExceptions = {IllegalArgumentException.class})
public void test() {
EzyKeysGenerator.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.tvd12.ezyfox.testing.security;

import com.tvd12.ezyfox.security.EzyBase64;

import java.security.spec.RSAPublicKeySpec;
import java.security.interfaces.RSAPublicKey;
import java.security.KeyFactory;
import java.math.BigInteger;
import java.util.Base64;

public class ModulusExponentToX509KeySpec {

public static void main(String[] args) throws Exception {
// Replace these with your actual modulus and exponent values
String modulusBase64 = "pGxIROVFa/fHqhMyrX4L53BCb72XTIK2+Hw2htdqF5wigSbDuHPDFetmQCdGUcTE2pCO/a6c3VrBU3vt45bXN4wqpZDM60XCsPOFZ1L3PIKmFnmuRwFdz+QIaoEo8sgkicPSG3ZHiHBr7t7+aIEnSpHr4jGInVUHSHnLt8m9MUr743XtgZynarfugUKz8utnRyhnoW/4Q8w+XivijDWf3VWWFpjfc78NxWJw+I3h7bdAO61N4O2JYXQMFgh26nPTAxrdk7r7spHJCBi6QHKLnRm8EXHC2pH4xFvKdxJamDC0evL/5Samy7vjIN8jibFtsWe/tKe/blbVF7cas4r0Iw==";
String exponentBase64 = "AQAB";

// Decode base64 strings to get byte arrays
byte[] modulusBytes = Base64.getDecoder().decode(modulusBase64);
byte[] exponentBytes = Base64.getDecoder().decode(exponentBase64);

// Convert byte arrays to BigInteger
BigInteger modulus = new BigInteger(1, modulusBytes);
BigInteger exponent = new BigInteger(1, exponentBytes);

// Create RSAPublicKeySpec
RSAPublicKeySpec rsaPublicKeySpec = new RSAPublicKeySpec(modulus, exponent);

// Create an RSAPublicKey from RSAPublicKeySpec
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPublicKey publicKey = (RSAPublicKey) keyFactory.generatePublic(rsaPublicKeySpec);
publicKey.getEncoded();
// Now you have an RSAPublicKey that can be used for various cryptographic operations
System.out.println("RSAPublicKey: " + publicKey);
System.out.println("RSAPublicKey: " + EzyBase64.encode2utf(publicKey.getEncoded()));
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.tvd12.ezyfox.testing.security;

import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.util.Arrays;
import java.util.Base64;

public class PEMToPublicKey {
public static void main(String[] args) {
try {
// Replace this with your PEM-encoded public key
String base64PublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApGxIROVFa/fHqhMyrX4L53BCb72XTIK2+Hw2htdqF5wigSbDuHPDFetmQCdGUcTE2pCO/a6c3VrBU3vt45bXN4wqpZDM60XCsPOFZ1L3PIKmFnmuRwFdz+QIaoEo8sgkicPSG3ZHiHBr7t7+aIEnSpHr4jGInVUHSHnLt8m9MUr743XtgZynarfugUKz8utnRyhnoW/4Q8w+XivijDWf3VWWFpjfc78NxWJw+I3h7bdAO61N4O2JYXQMFgh26nPTAxrdk7r7spHJCBi6QHKLnRm8EXHC2pH4xFvKdxJamDC0evL/5Samy7vjIN8jibFtsWe/tKe/blbVF7cas4r0IwIDAQAB";

// Decode the base64 string into a byte array
byte[] keyBytes = Base64.getDecoder().decode(base64PublicKey);
System.out.println(keyBytes.length);
System.out.println(Arrays.toString(keyBytes));

// Create a KeyFactory and a public key specification
KeyFactory keyFactory = KeyFactory.getInstance("RSA"); // or your desired algorithm
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);

// Generate the PublicKey object
PublicKey publicKey = keyFactory.generatePublic(keySpec);

// Now you have the PublicKey object
System.out.println("Public Key: " + publicKey);
} catch (Exception e) {
e.printStackTrace();
}
}
}

0 comments on commit 2e4fed6

Please sign in to comment.