测试加密和解密

This commit is contained in:
YuCheng Hu 2023-02-04 08:48:35 -05:00
parent 4ee8318869
commit bf7fd2c3e3

View File

@ -3,12 +3,15 @@ package com.ossez.framework.service;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.doubleclick.crypto.DoubleClickCrypto; import com.google.doubleclick.crypto.DoubleClickCrypto;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils; import org.springframework.util.Base64Utils;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
import java.security.SignatureException;
import java.util.Base64;
/** /**
@ -32,19 +35,30 @@ public class GoogleEncryptionService {
*/ */
public String decryptPrice(String url) { public String decryptPrice(String url) {
String encryptionKey ="encryption"; String encryptionKey = "encryption";
String integrityKey = "integrityKey"; String integrityKey = "integrityKey";
; ;
DoubleClickCrypto.Keys keys = null; DoubleClickCrypto.Keys keys = null;
try { try {
keys = new DoubleClickCrypto.Keys( keys = new DoubleClickCrypto.Keys(new SecretKeySpec(Base64Utils.encode(encryptionKey.getBytes()), "HmacSHA1"), new SecretKeySpec(Base64Utils.encode(integrityKey.getBytes()), "HmacSHA1"));
new SecretKeySpec(Base64Utils.encode(encryptionKey.getBytes()), "HmacSHA1"),
new SecretKeySpec(Base64Utils.encode(integrityKey.getBytes()), "HmacSHA1")); DoubleClickCrypto.Price crypto = new DoubleClickCrypto.Price(keys);
String price = "2.30";
byte[] bA = crypto.encrypt(Base64Utils.encode(price.getBytes()));
String encodedPrice = Base64.getEncoder().encodeToString(bA);
double priceEncode = crypto.decodePriceValue(encodedPrice);
} catch (InvalidKeyException e) { } catch (InvalidKeyException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} catch (SignatureException e) {
throw new RuntimeException(e);
} }
DoubleClickCrypto.Price crypto = new DoubleClickCrypto.Price(keys);
// String encodedPrice = httpRequest.getHeader("price"); // String encodedPrice = httpRequest.getHeader("price");
// double price = crypto.decodePriceValue(encodedPrice); // double price = crypto.decodePriceValue(encodedPrice);