Skip to content
This repository was archived by the owner on Apr 10, 2025. It is now read-only.

Commit 6cd01ea

Browse files
authored
Adds FromBase64 & ToBase64 to PrivateKey (#34)
1 parent 2d9dc89 commit 6cd01ea

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

GeneXusCryptography/src/main/java/com/genexus/cryptography/asymmetric/AsymmetricCipher.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ private String doEncryptInternal(String hashAlgorithm, String asymmetricEncrypti
142142
return doEncrypt(algorithm, hash, padding, asymKey, plainText);
143143
} catch (InvalidCipherTextException e) {
144144
this.error.setError("AE036", "Algoritmo inválido" + algorithm);
145-
e.printStackTrace();
145+
//e.printStackTrace();
146146
return "";
147147
}
148148
}
@@ -213,7 +213,7 @@ private String doDecryptInternal(String hashAlgorithm, String asymmetricEncrypti
213213
return doDecyrpt(algorithm, hash, padding, asymKey, encryptedInput);
214214
} catch (InvalidCipherTextException | UnsupportedEncodingException e) {
215215
this.error.setError("AE039", "Algoritmo inválido" + algorithm);
216-
e.printStackTrace();
216+
//e.printStackTrace();
217217
return "";
218218
}
219219
}

SecurityAPICommons/src/main/java/com/genexus/securityapicommons/commons/PrivateKey.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ public PrivateKey() {
77
}
88
public abstract boolean load(String path);
99
public abstract boolean loadPKCS12(String path, String alias, String password);
10+
public abstract boolean fromBase64(String base64);
11+
public abstract String toBase64();
1012
}

SecurityAPICommons/src/main/java/com/genexus/securityapicommons/keys/PrivateKeyManager.java

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import javax.crypto.EncryptedPrivateKeyInfo;
1919

20+
import org.bouncycastle.asn1.ASN1InputStream;
21+
import org.bouncycastle.asn1.ASN1Sequence;
2022
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
2123
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
2224
import org.bouncycastle.cert.X509CertificateHolder;
@@ -26,8 +28,8 @@
2628
import org.bouncycastle.openssl.PEMKeyPair;
2729
import org.bouncycastle.openssl.PEMParser;
2830
import org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo;
31+
import org.bouncycastle.util.encoders.Base64;
2932

30-
import com.genexus.securityapicommons.config.EncodingUtil;
3133
import com.genexus.securityapicommons.utils.SecurityUtils;
3234

3335
/**
@@ -67,8 +69,56 @@ public boolean loadPKCS12(String privateKeyPath, String alias, String password)
6769
}
6870
return true;
6971
}
72+
73+
@Override
74+
public boolean fromBase64(String base64)
75+
{
76+
boolean res;
77+
try {
78+
res = readBase64(base64);
79+
} catch (IOException e) {
80+
this.error.setError("PK0015", e.getMessage());
81+
return false;
82+
}
83+
this.hasPrivateKey = res;
84+
return res;
85+
}
86+
87+
@Override
88+
public String toBase64()
89+
{
90+
if(this.hasPrivateKey) {
91+
String encoded = "";
92+
try {
93+
encoded = Base64.toBase64String(this.privateKeyInfo.getEncoded());
94+
} catch (IOException e) {
95+
this.error.setError("PK0017", e.getMessage());
96+
return "";
97+
}
98+
return encoded;
99+
}
100+
this.error.setError("PK0016", "No private key loaded");
101+
return "";
102+
}
70103

71-
/******** EXTERNAL OBJECT PUBLIC METHODS - END ********/
104+
/******** EXTERNAL OBJECT PUBLIC METHODS - END
105+
* @throws IOException ********/
106+
107+
private boolean readBase64(String base64) throws IOException
108+
{
109+
byte[] keybytes = Base64.decode(base64);
110+
ASN1InputStream istream = new ASN1InputStream(keybytes);
111+
ASN1Sequence seq = (ASN1Sequence) istream.readObject();
112+
this.privateKeyInfo = PrivateKeyInfo.getInstance(seq);
113+
istream.close();
114+
if (this.privateKeyInfo == null)
115+
{
116+
this.error.setError("PK015", "Could not read private key from base64 string");
117+
return false;
118+
}
119+
this.privateKeyAlgorithm = this.privateKeyInfo.getPrivateKeyAlgorithm().getAlgorithm().getId(); // 1.2.840.113549.1.1.1
120+
return true;
121+
}
72122

73123
/**
74124
* @return PrivateKey type for the key type

0 commit comments

Comments
 (0)