<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    paulwong

    API接口中的敏感數(shù)據(jù)的加密與解密

    簽名

    為防止request中的json在傳輸過(guò)程中被更改,
    1. 需要在傳送雙方保存一個(gè)字符串sinature-key
    2. 用SHA256 hash請(qǐng)求中的json字符串,結(jié)果為hash1
    3. {"payload":hash1}以此為字符和sinature-key用JWS HS256算法進(jìn)行簽名,得到sinature1
    4. 在請(qǐng)求的json中加入字段:"sinature":sinature1
    5. 接收方進(jìn)行逆過(guò)程,驗(yàn)證是否會(huì)得到該字符串sinature-key
    import java.nio.charset.StandardCharsets;
    import java.security.SecureRandom;
    import java.text.ParseException;
    import java.util.Base64;

    import com.nimbusds.jose.JOSEException;
    import com.nimbusds.jose.JWSAlgorithm;
    import com.nimbusds.jose.JWSHeader;
    import com.nimbusds.jose.JWSObject;
    import com.nimbusds.jose.JWSSigner;
    import com.nimbusds.jose.JWSVerifier;
    import com.nimbusds.jose.Payload;
    import com.nimbusds.jose.crypto.MACSigner;
    import com.nimbusds.jose.crypto.MACVerifier;

    public final class SignatureUtil {
        
        private SignatureUtil() {}
        
        public static byte[] genSignatureKey() {
            
            // Generate random 256-bit (32-byte) shared secret
            SecureRandom random = new SecureRandom();
            byte[] sharedSecret = new byte[32];
            random.nextBytes(sharedSecret);
    //        a0a2abd8-6162-41c3-83d6-1cf559b46afc
            
            return sharedSecret;
        }
        
        public static String genSignatureKeyString() {
            return Base64.getEncoder().encodeToString(genSignatureKey());
        }
        
        public static byte[] decodeSignatureKey(String signatureKey) {
            return Base64.getDecoder().decode(signatureKey);
        }
        
        /**
         * P.147
         * sign app_body json string
         * 
         * 
    @param content
         * 
    @param signatureKey
         * 
    @return
         * 
    @throws JOSEException
         
    */
        public static String sign(String content, byte[] signatureKey) throws JOSEException {
            // Create HMAC signer
            JWSSigner signer = new MACSigner(signatureKey);

            // Prepare JWS object with "Hello, world!" payload
            JWSObject jwsObject = new JWSObject(new JWSHeader(JWSAlgorithm.HS256), new Payload(content));

            // Apply the HMAC
            jwsObject.sign(signer);

            // To serialize to compact form, produces something like
            
    // eyJhbGciOiJIUzI1NiJ9.SGVsbG8sIHdvcmxkIQ.onO9Ihudz3WkiauDO2Uhyuz0Y18UASXlSc1eS0NkWyA
            return jwsObject.serialize();
            
        }
        
        public static boolean verify(String content, byte[] signatureKey) {
            
            try {
                verifyAndGet(content, signatureKey);
                
                return true;
            } catch (Exception e){
                return false;
            }
        }
        
        public static String verifyAndGet(String content, byte[] signatureKey) throws ParseException, JOSEException {
            
            // To parse the JWS and verify it, e.g. on client-side
            JWSObject jwsObject = JWSObject.parse(content);
            
            JWSVerifier verifier = new MACVerifier(signatureKey);
            
            jwsObject.verify(verifier);
            
            return jwsObject.getPayload().toString();
        }
        
        public static void main(String [] args) throws JOSEException {
            String content = "Hello, world!";
            String signatureKey = "a0a2abd8-6162-41c3-83d6-1cf559b46afc";
            byte[] signatureKeyByte = signatureKey.getBytes(StandardCharsets.UTF_8);
            byte[] signatureKeyByte2 = genSignatureKey();
            
            System.out.println(sign(content, signatureKeyByte));
            System.out.println(sign(content, signatureKeyByte2));
        }


    }


    如果有文件傳輸,需對(duì)文件加密:

    1. 每個(gè)要傳輸?shù)奈募ile,生成唯一的密鑰symmetric-key
    2. 用此symmetric-key根據(jù)AES-256-CBC算法對(duì)文件file進(jìn)行加密,得到enc-file
    3. 將此symmetric-key用public-key加密得到enc-symmetric-key
    4. 將enc-symmetric-key和enc-file一同放進(jìn)request中進(jìn)行傳輸
    5. 接收方用private-key將enc-symmetric-key解密,得到symmetric-key
    6. 接收方用此symmetric-key解密文件enc-file,即可得到file
    import static org.apache.commons.codec.digest.MessageDigestAlgorithms.SHA_256;
    import static org.apache.commons.codec.digest.MessageDigestAlgorithms.SHA_512;
    import static com.novacredit.mcra.mcracommon.common.util.crypto.PasswordHashUtil.*;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.nio.charset.StandardCharsets;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.security.SecureRandom;
    import java.util.Arrays;
    import java.util.Properties;

    import javax.crypto.Cipher;
    import javax.crypto.CipherOutputStream;
    import javax.crypto.Mac;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;

    import org.apache.commons.codec.digest.DigestUtils;
    import org.apache.commons.codec.digest.HmacAlgorithms;
    import org.apache.commons.crypto.stream.CryptoInputStream;
    import org.apache.commons.crypto.stream.CryptoOutputStream;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;


    public final class MyFileUtils {
        
        private static final Logger LOGGER = LoggerFactory.getLogger(MyFileUtils.class);

        public static final String HMAC_SHA_256 = HmacAlgorithms.HMAC_SHA_256.getName();
        
        public static final String TRANSFORM = "AES/CBC/PKCS5Padding";
        
        public static final String ALGORITHM_AES = "AES";
        
        private MyFileUtils() {}

        /**
         * check sum
         * 
         * 
    @param file
         * 
    @return
         * 
    @throws IOException
         
    */
        public static String getFileSHA512(File file) throws IOException {
            return new DigestUtils(SHA_512).digestAsHex(file);
        }

        /**
         * check sum
         * 
         * 
    @param file
         * 
    @return
         * 
    @throws IOException
         
    */
        public static String getFileSHA512Quietly(File file) {
            try {
                return new DigestUtils(SHA_512).digestAsHex(file);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        
        /**
         * check sum for source string
         * 
         * 
    @param source
         * 
    @return
         * 
    @throws IOException
         
    */
        public static String getStringSHA256(String source) throws IOException {
            return new DigestUtils(SHA_256).digestAsHex(source);
        }
        
        @Deprecated
        public static byte[] makeKey(String prf, byte[] pass, byte[] salt, int iter, int len) throws Exception {
            
            byte[] result = new byte[len];
            Mac mac = Mac.getInstance(prf);
            mac.init(new SecretKeySpec(pass, prf));
            byte[] saltcnt = Arrays.copyOf(salt, salt.length + 4);
            
            while ( /* remaining */len > 0) {
                for (int o = saltcnt.length; --o >= saltcnt.length - 4;)
                    if (++saltcnt[o] != 0)
                        break;
                byte[] u = saltcnt, x = new byte[mac.getMacLength()];
                for (int i = 1; i <= iter; i++) {
                    u = mac.doFinal(u);
                    for (int o = 0; o < x.length; o++)
                        x[o] ^= u[o];
                }
                int len2 = Math.min(len, x.length);
                System.arraycopy(x, 0, result, result.length - len, len2);
                len -= len2;
            }
            return result;
        }
        
        /**
         * P.149
         * P.79
         * AES256-CBC
         * 
         * 
    @param inFilePath
         * 
    @param outFilePath
         * 
    @param symmetricKey
         * 
    @throws Exception
         
    */
        public static void encryptFile(String inFilePath, String outFilePath, String symmetricKey) throws Exception {
            
            char[] pass = symmetricKey.toCharArray();
            byte[] salt = (new SecureRandom()).generateSeed(8);
    //        byte[] derive = makeKey(HMAC_SHA_256, pass, salt, PBKDF2_ITERATIONS, SALT_BYTE_SIZE);
            byte[] derive = PasswordHashUtil.pbkdf2(pass, salt, PBKDF2_ITERATIONS, SALT_BYTE_SIZE);
            
            Cipher cipher = Cipher.getInstance(TRANSFORM);
            
            try (
                FileInputStream fis = new FileInputStream(inFilePath);
                FileOutputStream fos = new FileOutputStream(outFilePath);
                CipherOutputStream cos = new CipherOutputStream(fos, cipher);
            ){
                
                //  key is first 32, IV is last 16
                cipher.init(
                          Cipher.ENCRYPT_MODE, 
                          new SecretKeySpec(derive, 0, 32, ALGORITHM_AES), 
                          new IvParameterSpec(derive, 32, 16)
                       );
                
                fos.write("Salted__".getBytes(StandardCharsets.UTF_8));
                fos.write(salt);
                
                
                int b;
                byte[] d = new byte[8];
                
                while ((b = fis.read(d)) != -1) {
                    cos.write(d, 0, b);
                }
                cos.flush();
    //            cos.close();
    //            fis.close();
            }
            
        }
        
        @Deprecated
        public static void decryptFileOld(String inFilePath, String outFilePath, String symmetricKey) throws Exception {
            
            try (FileOutputStream fos = new FileOutputStream(outFilePath);){
                
                byte[] file = Files.readAllBytes(Paths.get(inFilePath));
                byte[] pass = symmetricKey.getBytes();
                Cipher cipher = Cipher.getInstance(TRANSFORM);
                
                // extract salt and derive 
                byte[] salt = Arrays.copyOfRange(file, 8, 16);
                byte[] derive = makeKey(HMAC_SHA_256, pass, salt, PBKDF2_ITERATIONS, SALT_BYTE_SIZE);
                
                //  key is first 32, IV is last 16
                cipher.init(
                          Cipher.DECRYPT_MODE, 
                          new SecretKeySpec(derive, 0, 32, ALGORITHM_AES), 
                          new IvParameterSpec(derive, 32, 16)
                       );
                
                // remainder of file is ciphertext
    //            String contentDecoded = new String(cipher.doFinal(file,16,file.length-16) , StandardCharsets.UTF_8);
                fos.write(cipher.doFinal(file,16,file.length-16));
    //            fos.close();
                
            }
            
        }
        
        public static void decryptFile(String inFilePath, String outFilePath, String symmetricKey) throws Exception {
            try(
                FileInputStream fis = new FileInputStream(inFilePath);
                FileOutputStream fos = new FileOutputStream(outFilePath, true);
            ){
                char[] pass = symmetricKey.toCharArray();
                Cipher cipher = Cipher.getInstance(TRANSFORM);
                
                int i=0;
                int b;
                byte[] bytes = new byte[2048];
                while ((b = fis.read(bytes)) != -1) {
                
                    byte[] contentBytes = Arrays.copyOf(bytes, b);
                        
                    byte[] content = null;
                    if(i == 0) {
                        byte[] salt = Arrays.copyOfRange(bytes, 8, 16);
    //                    byte[] derive = makeKey(HMAC_SHA_256, pass, salt, PBKDF2_ITERATIONS, SALT_BYTE_SIZE);
                        byte[] derive = PasswordHashUtil.pbkdf2(pass, salt, PBKDF2_ITERATIONS, SALT_BYTE_SIZE);
                        cipher.init(
                                  Cipher.DECRYPT_MODE, 
                                  new SecretKeySpec(derive, 0, 32, ALGORITHM_AES), 
                                  new IvParameterSpec(derive, 32, 16)
                               );
                    
                        content = cipher.doFinal(contentBytes, 16, contentBytes.length-16); 
                    } else {
                        content = cipher.doFinal(contentBytes); 
                    }
                    fos.write(content);
                    fos.flush();
                    
                    i++;
                }
                
            } catch(Exception e) {
                LOGGER.error(e.getMessage(),e);
                throw e;
            }

        }
        
        private static byte[] getUTF8Bytes(final String input) {
            return input.getBytes(StandardCharsets.UTF_8);
        }
        
        public static void encryptFile2(String inFilePath, String outFilePath, String symmetricKey) throws Exception {
            
            byte [] semmetryKeyByte = getUTF8Bytes(symmetricKey);
            final SecretKeySpec key = new SecretKeySpec(semmetryKeyByte, ALGORITHM_AES);
            final IvParameterSpec iv = new IvParameterSpec(semmetryKeyByte);
            final Properties properties = new Properties();
            
            //Encryption with CryptoOutputStream.
            
            try (
                FileInputStream fis = new FileInputStream(inFilePath);
                FileOutputStream fos = new FileOutputStream(outFilePath);
                CryptoOutputStream cos = new CryptoOutputStream(TRANSFORM, properties, fos, key, iv)
            ) {
                
                byte[] buf = new byte[1024 * 16];
                int len;
                while ((len = fis.read(buf)) != -1) {
                    cos.write(buf, 0, len);
                }
                
    //            fis.close();
                cos.flush();
            }
        }
        
        public static void decryptFile2(String inFilePath, String outFilePath, String semmetryKey) throws Exception {
            
            byte [] semmetryKeyByte = getUTF8Bytes(semmetryKey);
            final SecretKeySpec secretKey = new SecretKeySpec(semmetryKeyByte, ALGORITHM_AES);
            final IvParameterSpec initializationVector = new IvParameterSpec(semmetryKeyByte);
            Properties properties = new Properties();
            
            try (
                FileInputStream fis = new FileInputStream(inFilePath);
                FileOutputStream fos = new FileOutputStream(outFilePath);
                CryptoInputStream cis = new CryptoInputStream(TRANSFORM, properties, fis, secretKey,
                                            initializationVector);
            ){
                
                byte[] buf = new byte[1024 * 16];
                int len;
                while ((len = cis.read(buf)) != -1) {
                   fos.write(buf, 0, len);
                }
                
    //            cis.close();
                fos.flush();
                
            }
            
        }

    }



    import java.security.PrivateKey;
    import java.security.PublicKey;
    import java.util.Base64;

    import javax.crypto.Cipher;

    import org.apache.commons.codec.binary.StringUtils;

    public class SymmetricKeyUtil {
        
        private SymmetricKeyUtil() {}
        
        /**
         * P.79
         * 
         * 
    @param symmetricKey
         * 
    @param publicKey
         * 
    @return
         * 
    @throws Exception
         
    */
        public static String encryptWithPublicKey(String symmetricKey, PublicKey publicKey) throws Exception {
            
            // --- encrypt given algorithm string
            Cipher oaepFromAlgo = Cipher.getInstance("RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING");
            oaepFromAlgo.init(Cipher.ENCRYPT_MODE, publicKey);
            byte[] result = oaepFromAlgo.doFinal(StringUtils.getBytesUtf8(symmetricKey));
            
            return Base64.getEncoder().encodeToString(result);
        }
        
        public static String decryptWithPrivateKey(String symmetricKey, PrivateKey privateKey) throws Exception {
            
            // --- encrypt given algorithm string
            Cipher oaepFromAlgo = Cipher.getInstance("RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING");
    //        OAEPParameterSpec oaepParams = 
    //                new OAEPParameterSpec("SHA-256", "MGF1", new MGF1ParameterSpec("SHA-256"), PSource.PSpecified.DEFAULT);
    //        oaepFromAlgo.init(Cipher.DECRYPT_MODE, privateKey, oaepParams);
            oaepFromAlgo.init(Cipher.DECRYPT_MODE, privateKey);
            
            byte[] decodedContent = Base64.getDecoder().decode(symmetricKey);
            byte[] result = oaepFromAlgo.doFinal(decodedContent);
            
            return StringUtils.newStringUtf8(result);
            
        }

    }


    import java.security.NoSuchAlgorithmException;
    import java.security.SecureRandom;
    import java.security.spec.InvalidKeySpecException;

    import javax.crypto.SecretKeyFactory;
    import javax.crypto.spec.PBEKeySpec;

    public class PasswordHashUtil {
        
        public static final String PBKDF2_ALGORITHM = "PBKDF2WithHmacSHA256";
        
        public static final int PBKDF2_ITERATIONS = 10000;
        
        public static final int HASH_BYTE_SIZE = 48;
        
        public static final int SALT_BYTE_SIZE = 48;
        
        private PasswordHashUtil() {}

        /**
         * Computes the PBKDF2 hash of a password.
         * 
         * 
    @param password
         *            the password to hash.
         * 
    @param salt
         *            the salt
         * 
    @param iterations
         *            the iteration count (slowness factor)
         * 
    @param bytes
         *            the length of the hash to compute in bytes
         * 
    @return the PBDKF2 hash of the password
         
    */
        protected static byte[] pbkdf2(final char[] password, final byte[] salt, final int iterations, final int bytes)
                throws NoSuchAlgorithmException, InvalidKeySpecException {
            
            final PBEKeySpec spec = new PBEKeySpec(password, salt, iterations, bytes * 8);
            final SecretKeyFactory skf = SecretKeyFactory.getInstance(PBKDF2_ALGORITHM);
            return skf.generateSecret(spec).getEncoded();
        }

        /**
         * Returns a salted PBKDF2 hash of the password.
         * 
         * 
    @param password
         *            the password to hash
         * 
    @return a salted PBKDF2 hash of the password
         
    */
        public static byte[] createHash(final char[] password) throws NoSuchAlgorithmException, InvalidKeySpecException {
            
            // Generate a random salt
            final SecureRandom random = new SecureRandom();
            final byte[] salt = new byte[SALT_BYTE_SIZE];
            random.nextBytes(salt);

            // Hash the password
            final byte[] hash = pbkdf2(password, salt, PBKDF2_ITERATIONS, HASH_BYTE_SIZE);
            // format iterations:salt:hash
    //        return PBKDF2_ITERATIONS + ":" + toEncoding(salt) + ":" + toEncoding(hash);
            return hash;
        }

    }



    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;

    public final class PasswordGenerator {

        private static final String LOWER = "abcdefghijklmnopqrstuvwxyz";
        private static final String UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        private static final String DIGITS = "0123456789";
        private static final String PUNCTUATION = "!@#$%&*()_+-=[]|,./?><";
        private boolean useLower;
        private boolean useUpper;
        private boolean useDigits;
        private boolean usePunctuation;

        private PasswordGenerator() {
            throw new UnsupportedOperationException("Empty constructor is not supported.");
        }

        private PasswordGenerator(PasswordGeneratorBuilder builder) {
            this.useLower = builder.useLower;
            this.useUpper = builder.useUpper;
            this.useDigits = builder.useDigits;
            this.usePunctuation = builder.usePunctuation;
        }

        public static class PasswordGeneratorBuilder {

            private boolean useLower;
            private boolean useUpper;
            private boolean useDigits;
            private boolean usePunctuation;

            public PasswordGeneratorBuilder() {
                this.useLower = false;
                this.useUpper = false;
                this.useDigits = false;
                this.usePunctuation = false;
            }

            /**
             * Set true in case you would like to include lower characters
             * (abcxyz). Default false.
             *
             * 
    @param useLower true in case you would like to include lower
             * characters (abcxyz). Default false.
             * 
    @return the builder for chaining.
             
    */
            public PasswordGeneratorBuilder useLower(boolean useLower) {
                this.useLower = useLower;
                return this;
            }

            /**
             * Set true in case you would like to include upper characters
             * (ABCXYZ). Default false.
             *
             * 
    @param useUpper true in case you would like to include upper
             * characters (ABCXYZ). Default false.
             * 
    @return the builder for chaining.
             
    */
            public PasswordGeneratorBuilder useUpper(boolean useUpper) {
                this.useUpper = useUpper;
                return this;
            }

            /**
             * Set true in case you would like to include digit characters (123..).
             * Default false.
             *
             * 
    @param useDigits true in case you would like to include digit
             * characters (123..). Default false.
             * 
    @return the builder for chaining.
             
    */
            public PasswordGeneratorBuilder useDigits(boolean useDigits) {
                this.useDigits = useDigits;
                return this;
            }

            /**
             * Set true in case you would like to include punctuation characters
             * (!@#..). Default false.
             *
             * 
    @param usePunctuation true in case you would like to include
             * punctuation characters (!@#..). Default false.
             * 
    @return the builder for chaining.
             
    */
            public PasswordGeneratorBuilder usePunctuation(boolean usePunctuation) {
                this.usePunctuation = usePunctuation;
                return this;
            }

            /**
             * Get an object to use.
             *
             * 
    @return the {@link gr.idrymavmela.business.lib.PasswordGenerator}
             * object.
             
    */
            public PasswordGenerator build() {
                return new PasswordGenerator(this);
            }
        }

        /**
         * This method will generate a password depending the use* properties you
         * define. It will use the categories with a probability. It is not sure
         * that all of the defined categories will be used.
         *
         * 
    @param length the length of the password you would like to generate.
         * 
    @return a password that uses the categories you define when constructing
         * the object with a probability.
         
    */
        public String generate(int length) {
            // Argument Validation.
            if (length <= 0) {
                return "";
            }

            // Variables.
            StringBuilder password = new StringBuilder(length);
            Random random = new Random(System.nanoTime());

            // Collect the categories to use.
            List<String> charCategories = new ArrayList<>(4);
            if (useLower) {
                charCategories.add(LOWER);
            }
            if (useUpper) {
                charCategories.add(UPPER);
            }
            if (useDigits) {
                charCategories.add(DIGITS);
            }
            if (usePunctuation) {
                charCategories.add(PUNCTUATION);
            }

            // Build the password.
            for (int i = 0; i < length; i++) {
                String charCategory = charCategories.get(random.nextInt(charCategories.size()));
                int position = random.nextInt(charCategory.length());
                password.append(charCategory.charAt(position));
            }
            return new String(password);
        }
        
        public static String genDefaultPassword() {
            return new PasswordGeneratorBuilder()
                            .useDigits(true)
                            .usePunctuation(true)
                            .useUpper(true)
                            .useLower(true)
                            .build()
                            .generate(16);
        }
        
        public static String genDefault32Password() {
            return new PasswordGeneratorBuilder()
                            .useDigits(true)
                            .usePunctuation(true)
                            .useUpper(true)
                            .useLower(true)
                            .build()
                            .generate(32);
        }
        
        public static void main(String [] args) {
            String password = new PasswordGeneratorBuilder()
                                    .useDigits(true)
                                    .usePunctuation(true)
                                    .useUpper(true)
                                    .useLower(true)
                                    .build()
                                    .generate(8);
            System.out.println(password);
        } 
    }

    單元測(cè)試



    import java.io.File;

    import org.apache.commons.io.FileUtils;
    import org.junit.jupiter.api.BeforeEach;
    import org.junit.jupiter.api.Test;

    import com.novacredit.mcra.democrpcp.test.base.AbstractTestApp;
    import com.novacredit.mcra.mcracommon.common.util.crypto.MyFileUtils;
    import com.novacredit.mcra.mcracommon.common.util.crypto.PasswordGenerator;

    public class EncryptFileTest {
        
        private String inputFilePath = AbstractTestApp.getAbsolutePath(
                    "src/test/resources/KCLO_WS01_20210531_103933_ENQ.zip");
        
        private String encryptFilePath = inputFilePath.replace(".zip", ".enc.zip");
        
        private String password = PasswordGenerator.genDefaultPassword();
        
        private String decryptFilePath = inputFilePath.replace(".zip", ".dec.zip");
        
        @BeforeEach
        public void deleteFile() {
            FileUtils.deleteQuietly(new File(encryptFilePath));
            FileUtils.deleteQuietly(new File(decryptFilePath));
        }
        
        @Test
        public void testEncryptFile() throws Exception {
            
            MyFileUtils.encryptFile(inputFilePath, encryptFilePath, password);
            
            MyFileUtils.decryptFile(encryptFilePath, decryptFilePath, password);
            
            
        }
        
        @Test
        public void testEncryptFile2() throws Exception {
            
            MyFileUtils.encryptFile2(inputFilePath, encryptFilePath, password);

            MyFileUtils.decryptFile2(encryptFilePath, decryptFilePath, password);
            
    //        FileUtils.deleteQuietly(new File());
            
        }

    }


    import static org.junit.jupiter.api.Assertions.assertEquals;
    import static org.junit.jupiter.api.Assertions.assertTrue;

    import java.nio.charset.StandardCharsets;
    import java.text.ParseException;
    import java.util.Arrays;
    import java.util.Base64;

    import org.junit.jupiter.api.Test;

    import com.nimbusds.jose.JOSEException;
    import com.novacredit.mcra.mcracommon.common.util.crypto.PasswordGenerator;
    import com.novacredit.mcra.mcracommon.common.util.crypto.SignatureUtil;

    public class SignatureUtilTest {
        
        private String content = "Hello, world!";

        @Test
        public void test1() throws JOSEException, ParseException{
            
    //        String signatureKey = "a0a2abd8-6162-41c3-83d6-1cf559b46afc";
            String signatureKey = PasswordGenerator.genDefault32Password();
    //        String signatureKey = SignatureUtil.genSignatureKeyString();
            System.out.println(signatureKey);
            byte[] signatureKeyByte = signatureKey.getBytes(StandardCharsets.UTF_8);
            
            String result = SignatureUtil.sign(content, signatureKeyByte);
            System.out.println(result);
            
            assertTrue(SignatureUtil.verify(result, signatureKeyByte));
            assertEquals(content, SignatureUtil.verifyAndGet(result, signatureKeyByte));
        }
        
        @Test
        public void test2() throws JOSEException, ParseException{
            
            byte[] signatureKeyByte = SignatureUtil.genSignatureKey();
            
            String signatureKeyString = Base64.getEncoder().encodeToString(signatureKeyByte);
            byte [] decodeSignatureKeyByte = SignatureUtil.decodeSignatureKey(signatureKeyString);
            System.out.println("signatureKeyByte == decodeSignatureKeyByte ? " + Arrays.equals(signatureKeyByte, decodeSignatureKeyByte));
            
            String result = SignatureUtil.sign(content, signatureKeyByte);
            System.out.println(result);
            
            assertTrue(SignatureUtil.verify(result, signatureKeyByte));
            assertEquals(content, SignatureUtil.verifyAndGet(result, signatureKeyByte));
            
        }

    }

    import static org.junit.jupiter.api.Assertions.assertEquals;

    import java.security.KeyPair;
    import java.security.PrivateKey;
    import java.security.PublicKey;

    import org.junit.jupiter.api.Test;

    import com.novacredit.mcra.mcracommon.common.util.crypto.PublicCertificateUtil;
    import com.novacredit.mcra.mcracommon.common.util.crypto.SymmetricKeyUtil;

    public class SymmetricKeyUtilTest {
        
        @Test
        public void test1() throws Exception {
            
            KeyPair keyPair = PublicCertificateUtil.genKeyPair();
            
            String privateKeyString = PublicCertificateUtil.keyToBase64String(keyPair.getPrivate());
            String publicKeyString = PublicCertificateUtil.keyToBase64String(keyPair.getPublic());
            
            System.out.println(privateKeyString);
            System.out.println(publicKeyString);
            
            String content = "Hello World.";
            
            PrivateKey privateKey = PublicCertificateUtil.genPrivateKey(privateKeyString);
            PublicKey  publicKey  = PublicCertificateUtil.genPublicKey(publicKeyString);
            
            String encryptResult = SymmetricKeyUtil.encryptWithPublicKey(content, publicKey);
            String decryptResult = SymmetricKeyUtil.decryptWithPrivateKey(encryptResult, privateKey);
            
            assertEquals(decryptResult, content);
        }

    }


    posted on 2022-05-11 14:37 paulwong 閱讀(970) 評(píng)論(0)  編輯  收藏 所屬分類: J2SE

    主站蜘蛛池模板: 97在线线免费观看视频在线观看| 亚洲AV无码乱码国产麻豆| 真人做人试看60分钟免费视频| jyzzjyzz国产免费观看| 国产美女精品久久久久久久免费| 亚洲 日韩经典 中文字幕| 在线免费视频一区二区| 免费看又黄又无码的网站| 国产黄在线播放免费观看| 精品国产亚洲AV麻豆| 亚洲综合丁香婷婷六月香| 亚洲综合男人的天堂色婷婷| 亚洲高清国产AV拍精品青青草原| 亚洲一级特黄大片无码毛片| 国产做床爱无遮挡免费视频| 卡一卡二卡三在线入口免费| 毛片免费视频观看| 青青在线久青草免费观看| 91精品成人免费国产片| 99在线观看视频免费| 香港a毛片免费观看 | 99久久精品国产免费| 最新久久免费视频| 免费无码又爽又刺激网站| 最近更新免费中文字幕大全| 精品97国产免费人成视频 | 波多野结衣视频在线免费观看| 成人黄软件网18免费下载成人黄18免费视频 | 久久国产精品免费观看| 国产色爽免费无码视频| 国内精品久久久久影院免费| 18禁在线无遮挡免费观看网站| 日韩a级无码免费视频| 国产精品区免费视频| 99久久99久久免费精品小说| 99久久99热精品免费观看国产| 99久久国产免费中文无字幕| 国产精品视频免费观看| 毛片免费全部播放一级| 香蕉视频在线观看免费国产婷婷| 国产成人无码区免费A∨视频网站|