日本熟妇hd丰满老熟妇,中文字幕一区二区三区在线不卡 ,亚洲成片在线观看,免费女同在线一区二区

加密SDK快速入門(Java)

加密SDK(Encryption SDK)是一個客戶端密碼庫,通過與密鑰管理服務KMS(Key Management Service)結合使用,幫助您快速實現數據的加解密、簽名驗簽功能。本文以Java語言為例,為您介紹如何快速使用加密SDK進行數據加解密。

背景信息

您可以訪問alibabacloud-encryption-sdk-java,查看代碼示例。

在本地安裝加密SDK

  1. 編譯安裝加密SDK。

    git clone https://github.com/aliyun/alibabacloud-encryption-sdk-java.git
    cd alibabacloud-encryption-sdk-java
    mvn clean install -DskipTests
  2. 在項目中添加依賴。

    <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.1</version>
    </dependency>
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>alibabacloud-encryption-sdk-java</artifactId>
        <version>X.X.X</version>
    </dependency>

通過Maven倉庫安裝加密SDK

在項目中添加alibabacloud-encryption-sdk-java的依賴,可從Maven倉庫中自動下載發布的Java安裝包。

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>alibabacloud-encryption-sdk-java</artifactId>
    <version>X.X.X</version>
</dependency>
說明

加密SDK的最新版本,請參見Alibabacloud Encryption SDK Java

數據加解密示例

說明

阿里云賬號AccessKey擁有所有OpenAPI的訪問權限,建議您使用RAM用戶進行API訪問或日常運維。強烈建議不要把AccessKey ID和AccessKey Secret保存到工程代碼里,否則可能導致AccessKey泄露,威脅您賬號下所有資源的安全。

本示例以將AccessKey配置在環境變量ALIBABA_CLOUD_ACCESS_KEY_ID和ALIBABA_CLOUD_ACCESS_KEY_SECRET的方式來實現身份驗證為例。

  • 對字節數組類型的數據進行加解密

    public class BasicEncryptionExample {
        private static final String ACCESS_KEY_ID = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        private static final String ACCESS_KEY_SECRET = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        private static final String CMK_ARN = "acs:kms:RegionId:UserId:key/CmkId";
        private static final byte[] PLAIN_TEXT = "Hello World".getBytes(StandardCharsets.UTF_8);
    
        public static void main(String[] args) {
            //1.創建訪問aliyun配置。
            AliyunConfig config = new AliyunConfig();
            config.withAccessKey(ACCESS_KEY_ID, ACCESS_KEY_SECRET);
    
            //2.創建SDK,傳入訪問aliyun配置。
            AliyunCrypto aliyunSDK = new AliyunCrypto(config);
    
            //3.創建provider,用于提供數據密鑰或簽名。
            BaseDataKeyProvider provider = new DefaultDataKeyProvider(CMK_ARN);
            //設置不同的算法(默認為AES_GCM_NOPADDING_256)。
            //provider.setAlgorithm(CryptoAlgorithm.SM4_GCM_NOPADDING_128);
    
            //4.加密上下文。
            Map<String, String> encryptionContext = new HashMap<>();
            encryptionContext.put("one", "one");
            encryptionContext.put("two", "two");
    
            //5.調用加密和解密接口。
            CryptoResult<byte[]> cipherResult = aliyunSDK.encrypt(provider, PLAIN_TEXT, encryptionContext);
            CryptoResult<byte[]> plainResult = aliyunSDK.decrypt(provider, cipherResult.getResult());
    
            Assert.assertArrayEquals(PLAIN_TEXT, plainResult.getResult());
        }
    }
    說明
  • 對字節流類型的數據進行加解密

    public class FileStreamSample {
        private static final String FILE = "README.md";
        // accessKeyId accessKeySecret
        private static final String ACCESS_KEY_ID = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        private static final String ACCESS_KEY_SECRET = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // 日志系統。
        private static final Logger LOGGER = LoggerFactory.getLogger(FileStreamSample.class);
        // ARN格式的用戶主密鑰ID。
        private static final String CMK_ARN = "acs:kms:RegionId:UserId:key/CmkId";
    
        public static void main(String[] args) throws IOException {
            AliyunConfig config = new AliyunConfig();
            config.withAccessKey(ACCESS_KEY_ID, ACCESS_KEY_SECRET);
            encryptStream(config);
            decryptStream(config);
            Assert.assertEquals(getFileMD5(FILE), getFileMD5(FILE + ".decrypted"));
        }
    
        private static void encryptStream(AliyunConfig config) throws IOException {
            //1.創建SDK,傳入訪問aliyun配置。
            AliyunCrypto aliyunSDK = new AliyunCrypto(config);
    
            //2.構建加密上下文。
            final Map<String, String> encryptionContext = new HashMap<>();
            encryptionContext.put("this", "context");
            encryptionContext.put("can help you", "to confirm");
            encryptionContext.put("this data", "is your original data");
    
            //3.創建提供數據密鑰的provider。
            BaseDataKeyProvider provider = new DefaultDataKeyProvider(CMK_ARN);
    
            //4.創建輸入輸出流。
            FileInputStream inputStream = new FileInputStream(FILE);
            FileOutputStream outputStream = new FileOutputStream(FILE + ".encrypted");
    
            //5.調用加密接口。
            try {
                aliyunSDK.encrypt(provider, inputStream, outputStream, encryptionContext);
            } catch (InvalidAlgorithmException e) {
                System.out.println("Failed.");
                System.out.println("Error message: " + e.getMessage());
            }
        }
    
        private static void decryptStream(AliyunConfig config) throws IOException {
            //1.創建SDK,傳入訪問aliyun配置。
            AliyunCrypto aliyunSDK = new AliyunCrypto(config);
    
            //2.創建提供數據密鑰的provider。
            BaseDataKeyProvider provider = new DefaultDataKeyProvider(CMK_ARN);
    
            //3.創建輸入輸出流。
            FileInputStream inputStream = new FileInputStream(FILE + ".encrypted");
            FileOutputStream outputStream = new FileOutputStream(FILE + ".decrypted");
    
            //4.調用解密接口。
            try {
                aliyunSDK.decrypt(provider, inputStream, outputStream);
            } catch (InvalidAlgorithmException e) {
                System.out.println("Failed.");
                System.out.println("Error message: " + e.getMessage());
            }
        }
    
        private static String getFileMD5(String fileName) {
            File file = new File(fileName);
            if  (!file.isFile()) {
                return null;
            }
            MessageDigest digest;
            byte[] buffer = new byte[4096];
            try (FileInputStream in = new FileInputStream(file)){
                digest = MessageDigest.getInstance("MD5");
                int len;
                while  ((len = in.read(buffer)) != -1) {
                    digest.update(buffer,  0 , len);
                }
                return Hex.encodeHexString(digest.digest());
            }  catch  (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }