3232import com .google .api .services .cloudkms .v1 .model .ListKeyRingsResponse ;
3333import java .io .IOException ;
3434import java .io .StringReader ;
35- import java .nio .charset .StandardCharsets ;
3635import java .security .InvalidKeyException ;
3736import java .security .KeyFactory ;
3837import java .security .MessageDigest ;
4342import java .security .Signature ;
4443import java .security .SignatureException ;
4544import java .security .spec .InvalidKeySpecException ;
46- import java .security .spec .PKCS8EncodedKeySpec ;
4745import java .security .spec .X509EncodedKeySpec ;
4846import java .util .Base64 ;
4947import javax .crypto .BadPaddingException ;
5755public class Asymmetric {
5856
5957 // [START kms_get_asymmetric_public]
60- /** Retrieves the public key from a saved asymmetric key pair on Cloud KMS */
58+ /**
59+ * Retrieves the public key from a saved asymmetric key pair on Cloud KMS
60+ *
61+ * Requires:
62+ * java.io.StringReader
63+ * java.security.KeyFactory
64+ * java.security.PublicKey
65+ * java.security.Security
66+ * java.security.spec.X509EncodedKeySpec
67+ * org.bouncycastle.jce.provider.BouncyCastleProvider
68+ * org.bouncycastle.util.io.pem.PemReader
69+ */
6170 public static PublicKey getAsymmetricPublicKey (CloudKMS client , String keyPath )
6271 throws IOException , NoSuchAlgorithmException , InvalidKeySpecException ,
6372 NoSuchProviderException {
@@ -86,26 +95,32 @@ public static PublicKey getAsymmetricPublicKey(CloudKMS client, String keyPath)
8695 * Decrypt a given ciphertext using an 'RSA_DECRYPT_OAEP_2048_SHA256' private key
8796 * stored on Cloud KMS
8897 */
89- public static String decryptRSA (String ciphertext , CloudKMS client , String keyPath )
98+ public static byte [] decryptRSA (byte [] ciphertext , CloudKMS client , String keyPath )
9099 throws IOException {
91- AsymmetricDecryptRequest request = new AsymmetricDecryptRequest ().setCiphertext (ciphertext );
100+ AsymmetricDecryptRequest request = new AsymmetricDecryptRequest ().encodeCiphertext (ciphertext );
92101 AsymmetricDecryptResponse response = client .projects ()
93102 .locations ()
94103 .keyRings ()
95104 .cryptoKeys ()
96105 .cryptoKeyVersions ()
97106 .asymmetricDecrypt (keyPath , request )
98107 .execute ();
99- return new String ( response .decodePlaintext () );
108+ return response .decodePlaintext ();
100109 }
101110 // [END kms_decrypt_rsa]
102111
103112 // [START kms_encrypt_rsa]
104113 /**
105- * Encrypt message locally using an 'RSA_DECRYPT_OAEP_2048_SHA256' public key
106- * retrieved from Cloud KMS
114+ * Encrypt data locally using an 'RSA_DECRYPT_OAEP_2048_SHA256' public key
115+ * retrieved from Cloud KMS
116+ *
117+ * Requires:
118+ * java.security.PublicKey
119+ * java.security.Security
120+ * javax.crypto.Cipher
121+ * org.bouncycastle.jce.provider.BouncyCastleProvider
107122 */
108- public static String encryptRSA (String message , CloudKMS client , String keyPath )
123+ public static byte [] encryptRSA (byte [] plaintext , CloudKMS client , String keyPath )
109124 throws IOException , IllegalBlockSizeException , NoSuchPaddingException ,
110125 InvalidKeySpecException , NoSuchProviderException , BadPaddingException ,
111126 NoSuchAlgorithmException , InvalidKeyException {
@@ -114,20 +129,23 @@ public static String encryptRSA(String message, CloudKMS client, String keyPath)
114129
115130 Cipher cipher = Cipher .getInstance ("RSA/NONE/OAEPWITHSHA256ANDMGF1PADDING" , "BC" );
116131 cipher .init (Cipher .ENCRYPT_MODE , rsaKey );
117- byte [] ciphertext = cipher .doFinal (message .getBytes (StandardCharsets .UTF_8 ));
118- return Base64 .getEncoder ().encodeToString (ciphertext );
132+ return cipher .doFinal (plaintext );
119133 }
120134 // [END kms_encrypt_rsa]
121135
122136 // [START kms_sign_asymmetric]
123- /** Create a signature for a message using a private key stored on Cloud KMS */
124- public static String signAsymmetric (String message , CloudKMS client , String keyPath )
137+ /** Create a signature for a message using a private key stored on Cloud KMS
138+ *
139+ * Requires:
140+ * java.security.MessageDigest
141+ * java.util.Base64
142+ */
143+ public static byte [] signAsymmetric (byte [] message , CloudKMS client , String keyPath )
125144 throws IOException , NoSuchAlgorithmException {
126- byte [] msgBytes = message .getBytes (StandardCharsets .UTF_8 );
127145 Digest digest = new Digest ();
128146 // Note: some key algorithms will require a different hash function
129147 // For example, EC_SIGN_P384_SHA384 requires SHA-384
130- digest .encodeSha256 (MessageDigest .getInstance ("SHA-256" ).digest (msgBytes ));
148+ digest .encodeSha256 (MessageDigest .getInstance ("SHA-256" ).digest (message ));
131149
132150 AsymmetricSignRequest signRequest = new AsymmetricSignRequest ();
133151 signRequest .setDigest (digest );
@@ -139,16 +157,22 @@ public static String signAsymmetric(String message, CloudKMS client, String keyP
139157 .cryptoKeyVersions ()
140158 .asymmetricSign (keyPath , signRequest )
141159 .execute ();
142- return response .getSignature ();
160+ return Base64 . getMimeDecoder (). decode ( response .getSignature () );
143161 }
144162 // [END kms_sign_asymmetric]
145163
146164 // [START kms_verify_signature_rsa]
147165 /**
148166 * Verify the validity of an 'RSA_SIGN_PSS_2048_SHA256' signature for the
149- * specified plaintext message
167+ * specified message
168+ *
169+ * Requires:
170+ * java.security.PublicKey
171+ * java.security.Security
172+ * java.security.Signature
173+ * org.bouncycastle.jce.provider.BouncyCastleProvider
150174 */
151- public static boolean verifySignatureRSA (String signature , String message , CloudKMS client ,
175+ public static boolean verifySignatureRSA (byte [] signature , byte [] message , CloudKMS client ,
152176 String keyPath ) throws IOException , NoSuchAlgorithmException , InvalidKeySpecException ,
153177 SignatureException , NoSuchProviderException , InvalidKeyException {
154178 Security .addProvider (new BouncyCastleProvider ());
@@ -157,18 +181,23 @@ public static boolean verifySignatureRSA(String signature, String message, Cloud
157181 Signature rsaVerify = Signature .getInstance ("SHA256withRSA/PSS" );
158182
159183 rsaVerify .initVerify (rsaKey );
160- rsaVerify .update (message .getBytes (StandardCharsets .UTF_8 ));
161- byte [] sigBytes = Base64 .getMimeDecoder ().decode (signature );
162- return rsaVerify .verify (sigBytes );
184+ rsaVerify .update (message );
185+ return rsaVerify .verify (signature );
163186 }
164187 // [END kms_verify_signature_rsa]
165188
166189 // [START kms_verify_signature_ec]
167190 /**
168191 * Verify the validity of an 'EC_SIGN_P256_SHA256' signature for the
169- * specified plaintext message
192+ * specified message
193+ *
194+ * Requires:
195+ * java.security.PublicKey
196+ * java.security.Security
197+ * java.security.Signature
198+ * org.bouncycastle.jce.provider.BouncyCastleProvider
170199 */
171- public static boolean verifySignatureEC (String signature , String message , CloudKMS client ,
200+ public static boolean verifySignatureEC (byte [] signature , byte [] message , CloudKMS client ,
172201 String keyPath ) throws IOException , NoSuchAlgorithmException , InvalidKeySpecException ,
173202 SignatureException , NoSuchProviderException , InvalidKeyException {
174203 Security .addProvider (new BouncyCastleProvider ());
@@ -177,9 +206,8 @@ public static boolean verifySignatureEC(String signature, String message, CloudK
177206 Signature ecVerify = Signature .getInstance ("SHA256withECDSA" , "BC" );
178207
179208 ecVerify .initVerify (ecKey );
180- ecVerify .update (message .getBytes (StandardCharsets .UTF_8 ));
181- byte [] sigBytes = Base64 .getMimeDecoder ().decode (signature );
182- return ecVerify .verify (sigBytes );
209+ ecVerify .update (message );
210+ return ecVerify .verify (signature );
183211 }
184212 // [END kms_verify_signature_ec]
185213
0 commit comments