This commit is contained in:
六如
2024-10-25 20:43:06 +08:00
parent dabf8032ca
commit 13b06f0592
21 changed files with 569 additions and 353 deletions

176
sop-gateway/checkstyle.xml Normal file
View File

@@ -0,0 +1,176 @@
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name="Checker">
<!-- 文件长度不超过1500行 -->
<module name="FileLength">
<property name="max" value="2500"/>
</module>
<!-- 长度检查 -->
<!-- 每行不超过200个字符 -->
<module name="LineLength">
<property name="max" value="400"/>
</module>
<module name="SuppressWarningsFilter" />
<!-- 每个java文件一个语法树 -->
<module name="TreeWalker">
<module name="SuppressWarningsHolder" />
<!-- import检查-->
<!-- 检查是否从非法的包中导入了类 -->
<module name="IllegalImport"/>
<!-- 检查是否导入了多余的包 -->
<module name="RedundantImport"/>
<!-- 没用的import检查比如1.没有被用到2.重复的3.import java.lang的4.import 与该类在同一个package的 -->
<module name="UnusedImports"/>
<!-- 注释检查 -->
<!-- 检查构造函数的javadoc -->
<module name="JavadocType">
<property name="allowUnknownTags" value="true"/>
<message key="javadoc.missing" value="类注释缺少Javadoc注释。"/>
</module>
<!-- 命名检查 -->
<!-- 局部的final变量包括catch中的参数的检查 -->
<module name="LocalFinalVariableName"/>
<!-- 局部的非final型的变量包括catch中的参数的检查 -->
<module name="LocalVariableName"/>
<!-- 包名的检查(只允许小写字母),默认^[a-z]+(\.[a-zA-Z_][a-zA-Z_0-9_]*)*$ -->
<module name="PackageName">
<property name="format" value="^[a-z]+(\.[a-z][a-z0-9]*)*$"/>
<message key="name.invalidPattern" value="包名 ''{0}'' 要符合 ''{1}''格式."/>
</module>
<!-- 仅仅是static型的变量不包括static final型的检查 -->
<module name="StaticVariableName"/>
<!-- Class或Interface名检查默认^[A-Z][a-zA-Z0-9]*$-->
<module name="TypeName">
<property name="severity" value="warning"/>
<message key="name.invalidPattern" value="名称 ''{0}'' 要符合 ''{1}''格式."/>
</module>
<!-- 非static型变量的检查
<module name="MemberName"/>
-->
<!-- 方法名的检查 -->
<module name="MethodName"/>
<!-- 方法的参数名
<module name="ParameterName "/>
-->
<!-- 常量名的检查(只允许大写),默认^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$ -->
<module name="ConstantName"/>
<!-- 定义检查 -->
<!-- 检查数组类型定义的样式 -->
<module name="ArrayTypeStyle"/>
<!-- 检查long型定义是否有大写的“L” -->
<module name="UpperEll"/>
<!-- 方法不超过100行 -->
<module name="MethodLength">
<property name="tokens" value="METHOD_DEF"/>
<property name="max" value="300"/>
</module>
<!-- 方法的参数个数不超过8个。 并且不对构造方法进行检查-->
<module name="ParameterNumber">
<property name="max" value="8"/>
<property name="ignoreOverriddenMethods" value="true"/>
<property name="tokens" value="METHOD_DEF"/>
</module>
<!-- 空格检查-->
<!-- 方法名后跟左圆括号"(" -->
<module name="MethodParamPad"/>
<!-- 在类型转换时,不允许左圆括号右边有空格,也不允许与右圆括号左边有空格 -->
<module name="TypecastParenPad"/>
<!-- 检查在某个特定关键字之后应保留空格 -->
<module name="NoWhitespaceAfter"/>
<!-- 检查在某个特定关键字之前应保留空格 -->
<module name="NoWhitespaceBefore"/>
<!-- 圆括号空白 -->
<module name="ParenPad"/>
<!-- 检查分隔符是否在空白之后 -->
<module name="WhitespaceAfter"/>
<!-- 检查分隔符周围是否有空白 -->
<module name="WhitespaceAround"/>
<!-- 修饰符检查 -->
<!-- 检查修饰符的顺序是否遵照java语言规范默认public、protected、private、abstract、static、final、transient、volatile、synchronized、native、strictfp -->
<module name="ModifierOrder"/>
<!-- 检查接口和annotation中是否有多余修饰符如接口方法不必使用public -->
<module name="RedundantModifier"/>
<!-- 代码块检查 -->
<!-- 检查是否有嵌套代码块 -->
<module name="AvoidNestedBlocks"/>
<!-- 检查是否有空代码块 -->
<module name="EmptyBlock"/>
<!-- 检查左大括号位置 -->
<module name="LeftCurly"/>
<!-- 检查代码块是否缺失{} -->
<module name="NeedBraces"/>
<!-- 检查右大括号位置 -->
<module name="RightCurly"/>
<!-- 代码检查 -->
<!-- 检查空的代码段 -->
<module name="EmptyStatement"/>
<!-- 检查在重写了equals方法后是否重写了hashCode方法 -->
<module name="EqualsHashCode"/>
<!-- 检查局部变量或参数是否隐藏了类中的变量 -->
<module name="HiddenField">
<property name="tokens" value="VARIABLE_DEF"/>
</module>
<!-- 检查子表达式中是否有赋值操作 -->
<module name="InnerAssignment"/>
<!-- 检查switch语句是否有default -->
<module name="MissingSwitchDefault"/>
<!-- 检查是否有过度复杂的布尔表达式 -->
<module name="SimplifyBooleanExpression"/>
<!-- 检查是否有过于复杂的布尔返回代码段 -->
<module name="SimplifyBooleanReturn"/>
<!-- 类设计检查 -->
<!-- 检查类是否为扩展设计l -->
<!-- 检查只有private构造函数的类是否声明为final
<module name="FinalClass"/>
-->
<!-- 检查接口是否仅定义类型 -->
<module name="InterfaceIsType"/>
<!-- 检查类成员的可见度 检查类成员的可见性。只有static final 成员是public的
除非在本检查的protectedAllowed和packagedAllowed属性中进行了设置-->
<module name="VisibilityModifier">
<property name="packageAllowed" value="true"/>
<property name="protectedAllowed" value="true"/>
</module>
<!-- 语法 -->
<!-- String的比较不能用!= 和 == -->
<module name="StringLiteralEquality"/>
<!-- 限制for循环最多嵌套2层 -->
<module name="NestedForDepth">
<property name="max" value="2"/>
</module>
<!-- if最多嵌套3层 -->
<module name="NestedIfDepth">
<property name="max" value="10"/>
</module>
<!-- 检查未被注释的main方法,排除以Appllication结尾命名的类 -->
<module name="UncommentedMain">
<property name="excludedClasses" value=".*[Application,Test]$"/>
</module>
<!-- 禁止使用System.out.println -->
<module name="Regexp">
<property name="format" value="System\.out\.println"/>
<property name="illegalPattern" value="true"/>
</module>
<!--try catch 异常处理数量 3-->
<module name="NestedTryDepth ">
<property name="max" value="3"/>
</module>
<!-- clone方法必须调用了super.clone() -->
<module name="SuperClone"/>
<!-- finalize 必须调用了super.finalize() -->
<module name="SuperFinalize"/>
</module>
</module>

View File

@@ -272,6 +272,26 @@
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.3.1</version>
<configuration>
<configLocation>checkstyle.xml</configLocation>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<linkXRef>false</linkXRef>
</configuration>
<executions>
<execution>
<id>validate</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

View File

@@ -6,9 +6,10 @@ import java.nio.charset.StandardCharsets;
/**
* @author 六如
*/
public class SopConstants {
public final class SopConstants {
private SopConstants() {}
private SopConstants() {
}
public static final Charset CHARSET_UTF8 = StandardCharsets.UTF_8;
public static final String UTF8 = "UTF-8";

View File

@@ -1,14 +1,12 @@
package com.gitee.sop.gateway.dao.entity;
import java.time.LocalDateTime;
import java.util.Date;
import com.gitee.fastmybatis.annotation.Pk;
import com.gitee.fastmybatis.annotation.PkStrategy;
import com.gitee.fastmybatis.annotation.Table;
import lombok.Data;
import java.time.LocalDateTime;
/**
* 表名isv_info

View File

@@ -2,113 +2,209 @@ package com.gitee.sop.gateway.message;
/**
* 网关错误定义
*
* @author 六如
*/
public enum ErrorEnum {
/** 成功 */
/**
* 成功
*/
SUCCESS(Codes.CODE_SUCCESS, ""),
/** 服务暂不可用 */
/**
* 服务暂不可用
*/
ISP_UNKNOWN_ERROR(Codes.CODE_UNKNOWN, "isp.unknown-error"),
/** 微服务未知错误 */
/**
* 微服务未知错误
*/
ISP_SERVICE_UNKNOWN_ERROR(Codes.CODE_UNKNOWN, "isp.service-unknown-error"),
/** 服务不可用,路由被禁用 */
/**
* 服务不可用,路由被禁用
*/
ISP_API_DISABLED(Codes.CODE_UNKNOWN, "isp.service-not-available"),
/** 网关响应超时 */
/**
* 网关响应超时
*/
ISP_GATEWAY_RESPONSE_TIMEOUT(Codes.CODE_UNKNOWN, "isp.gateway-response-timeout"),
/** 限流处理 */
/**
* 限流处理
*/
ISV_REQUEST_LIMIT(Codes.CODE_UNKNOWN, "isv.service-busy"),
/** 无效的访问令牌 */
/**
* 无效的访问令牌
*/
AOP_INVALID_AUTH_TOKEN(Codes.CODE_AUTH, "aop.invalid-auth-token"),
/** 访问令牌已过期 */
/**
* 访问令牌已过期
*/
AOP_AUTH_TOKEN_TIME_OUT(Codes.CODE_AUTH, "aop.auth-token-time-out"),
/** 无效的应用授权令牌 */
/**
* 无效的应用授权令牌
*/
AOP_INVALID_APP_AUTH_TOKEN(Codes.CODE_AUTH, "aop.invalid-app-auth-token"),
/** 商户未授权当前接口 */
/**
* 商户未授权当前接口
*/
AOP_INVALID_APP_AUTH_TOKEN_NO_API(Codes.CODE_AUTH, "aop.invalid-app-auth-token-no-api"),
/** 应用授权令牌已过期 */
/**
* 应用授权令牌已过期
*/
AOP_APP_AUTH_TOKEN_TIME_OUT(Codes.CODE_AUTH, "aop.app-auth-token-time-out"),
/** 商户未签约任何产品 */
/**
* 商户未签约任何产品
*/
AOP_NO_PRODUCT_REG_BY_PARTNER(Codes.CODE_AUTH, "aop.no-product-reg-by-partner"),
/** 缺少方法名参数 */
/**
* 缺少方法名参数
*/
ISV_MISSING_METHOD(Codes.CODE_MISSING, "isv.missing-method"),
/** 缺少签名参数 */
/**
* 缺少签名参数
*/
ISV_MISSING_SIGNATURE(Codes.CODE_MISSING, "isv.missing-signature"),
/** 缺少签名类型参数 */
/**
* 缺少签名类型参数
*/
ISV_MISSING_SIGNATURE_TYPE(Codes.CODE_MISSING, "isv.missing-signature-type"),
/** 缺少签名配置 */
/**
* 缺少签名配置
*/
ISV_MISSING_SIGNATURE_KEY(Codes.CODE_MISSING, "isv.missing-signature-key"),
/** 缺少appId参数 */
/**
* 缺少appId参数
*/
ISV_MISSING_APP_ID(Codes.CODE_MISSING, "isv.missing-app-id"),
/** 缺少时间戳参数 */
/**
* 缺少时间戳参数
*/
ISV_MISSING_TIMESTAMP(Codes.CODE_MISSING, "isv.missing-timestamp"),
/** 缺少版本参数 */
/**
* 缺少版本参数
*/
ISV_MISSING_VERSION(Codes.CODE_MISSING, "isv.missing-version"),
/** 解密出错, 未指定加密算法 */
/**
* 解密出错, 未指定加密算法
*/
ISV_DECRYPTION_ERROR_MISSING_ENCRYPT_TYPE(Codes.CODE_MISSING, "isv.decryption-error-missing-encrypt-type"),
/** 参数无效 */
/**
* 参数无效
*/
ISV_INVALID_PARAMETER(Codes.CODE_INVALID, "isv.invalid-parameter"),
/** 参数不正确 */
/**
* 参数不正确
*/
ISV_ERROR_PARAMETER(Codes.CODE_INVALID, "isv.error-parameter"),
/** 文件上传失败 */
/**
* 文件上传失败
*/
ISV_UPLOAD_FAIL(Codes.CODE_INVALID, "isv.upload-fail"),
/** 文件扩展名无效 */
/**
* 文件扩展名无效
*/
ISV_INVALID_FILE_EXTENSION(Codes.CODE_INVALID, "isv.invalid-file-extension"),
/** 文件大小无效 */
/**
* 文件大小无效
*/
ISV_INVALID_FILE_SIZE(Codes.CODE_INVALID, "isv.invalid-file-size"),
/** 不存在的方法名 */
/**
* 不存在的方法名
*/
ISV_INVALID_METHOD(Codes.CODE_INVALID, "isv.invalid-method"),
/** 无效的数据格式 */
/**
* 无效的数据格式
*/
ISV_INVALID_FORMAT(Codes.CODE_INVALID, "isv.invalid-format"),
/** 无效的签名类型 */
/**
* 无效的签名类型
*/
ISV_INVALID_SIGNATURE_TYPE(Codes.CODE_INVALID, "isv.invalid-signature-type"),
/** 无效签名 */
/**
* 无效签名
*/
ISV_INVALID_SIGNATURE(Codes.CODE_INVALID, "isv.invalid-signature"),
/** 无效的加密类型 */
/**
* 无效的加密类型
*/
ISV_INVALID_ENCRYPT_TYPE(Codes.CODE_INVALID, "isv.invalid-encrypt-type"),
/** 解密异常 */
/**
* 解密异常
*/
ISV_INVALID_ENCRYPT(Codes.CODE_INVALID, "isv.invalid-encrypt"),
/** 无效的appId参数 */
/**
* 无效的appId参数
*/
ISV_INVALID_APP_ID(Codes.CODE_INVALID, "isv.invalid-app-id"),
/** 非法的时间戳参数 */
/**
* 非法的时间戳参数
*/
ISV_INVALID_TIMESTAMP(Codes.CODE_INVALID, "isv.invalid-timestamp"),
/** 字符集错误 */
/**
* 字符集错误
*/
ISV_INVALID_CHARSET(Codes.CODE_INVALID, "isv.invalid-charset"),
/** 摘要错误 */
/**
* 摘要错误
*/
ISV_INVALID_DIGEST(Codes.CODE_INVALID, "isv.invalid-digest"),
/** 解密出错,不支持的加密算法 */
/**
* 解密出错,不支持的加密算法
*/
ISV_DECRYPTION_ERROR_NOT_VALID_ENCRYPT_TYPE(Codes.CODE_INVALID, "isv.decryption-error-not-valid-encrypt-type"),
/** 解密出错, 未配置加密密钥或加密密钥格式错误 */
/**
* 解密出错, 未配置加密密钥或加密密钥格式错误
*/
ISV_DECRYPTION_ERROR_NOT_VALID_ENCRYPT_KEY(Codes.CODE_INVALID, "isv.decryption-error-not-valid-encrypt-key"),
/** 解密出错,未知异常 */
/**
* 解密出错,未知异常
*/
ISV_DECRYPTION_ERROR_UNKNOWN(Codes.CODE_INVALID, "isv.decryption-error-unknown"),
/** 验签出错, 未配置对应签名算法的公钥或者证书 */
/**
* 验签出错, 未配置对应签名算法的公钥或者证书
*/
ISV_MISSING_SIGNATURE_CONFIG(Codes.CODE_INVALID, "isv.missing-signature-config"),
/** 本接口不支持第三方代理调用 */
/**
* 本接口不支持第三方代理调用
*/
ISV_NOT_SUPPORT_APP_AUTH(Codes.CODE_INVALID, "isv.not-support-app-auth"),
/** 可疑的攻击请求 */
/**
* 可疑的攻击请求
*/
ISV_SUSPECTED_ATTACK(Codes.CODE_INVALID, "isv.suspected-attack"),
/** 无效的content-type */
/**
* 无效的content-type
*/
ISV_INVALID_CONTENT_TYPE(Codes.CODE_INVALID, "isv.invalid-content-type"),
/** 业务处理失败 */
/**
* 业务处理失败
*/
BIZ_ERROR(Codes.CODE_BIZ, ""),
/** 请检查配置的账户是否有当前接口权限 */
/**
* 请检查配置的账户是否有当前接口权限
*/
ISV_INSUFFICIENT_ISV_PERMISSIONS(Codes.CODE_ISV_PERM, "isv.insufficient-isv-permissions"),
/** 代理的商户没有当前接口权限 */
/**
* 代理的商户没有当前接口权限
*/
ISV_INSUFFICIENT_USER_PERMISSIONS(Codes.CODE_ISV_PERM, "isv.insufficient-user-permissions"),
/** 没有当前接口权限 */
/**
* 没有当前接口权限
*/
ISV_ROUTE_NO_PERMISSIONS(Codes.CODE_ISV_PERM, "isv.route-no-permissions"),
/** 禁止访问 */
/**
* 禁止访问
*/
ISV_ACCESS_FORBIDDEN(Codes.CODE_ISV_PERM, "isv.access-forbidden"),
/** 禁止IP访问 */
ISV_IP_FORBIDDEN(Codes.CODE_ISV_PERM, "isv.ip-forbidden"),
/**
* 禁止IP访问
*/
ISV_IP_FORBIDDEN(Codes.CODE_ISV_PERM, "isv.ip-forbidden");
;
private final ErrorMeta errorMeta;
ErrorEnum(String code, String subCode) {

View File

@@ -22,7 +22,7 @@ import java.util.Set;
* @author 六如
*/
@Slf4j
public class ErrorFactory {
public final class ErrorFactory {
private static final String ZH = "zh";

View File

@@ -10,15 +10,15 @@ public class ErrorImpl implements IError {
private String code;
private String msg;
private String sub_code;
private String sub_msg;
private String subCode;
private String subMsg;
private String solution;
public ErrorImpl(String code, String msg, String sub_code, String sub_msg, String solution) {
public ErrorImpl(String code, String msg, String subCode, String subMsg, String solution) {
this.code = code;
this.msg = msg;
this.sub_code = sub_code;
this.sub_msg = sub_msg;
this.subCode = subCode;
this.subMsg = subMsg;
this.solution = solution;
}
}

View File

@@ -28,13 +28,13 @@ public interface IError {
* sub_code明细返回码
* @return sub_code明细返回码
*/
String getSub_code();
String getSubCode();
/**
* sub_msg明细返回码描述
* @return sub_msg明细返回码描述
*/
String getSub_msg();
String getSubMsg();
/**
* 解决方案

View File

@@ -11,8 +11,8 @@ import lombok.Getter;
public enum RequestFormatEnum {
NONE(""),
JSON("json"),
XML("xml"),
;
XML("xml");
private final String value;
public static RequestFormatEnum of(String value) {

View File

@@ -98,8 +98,8 @@ public class ApiResponse {
IError error = e.getError();
apiResponse.setCode(error.getCode());
apiResponse.setMsg(error.getMsg());
apiResponse.setSub_code(error.getSub_code());
apiResponse.setSub_msg(error.getSub_msg());
apiResponse.setSub_code(error.getSubCode());
apiResponse.setSub_msg(error.getSubMsg());
return apiResponse;
}
@@ -109,7 +109,7 @@ public class ApiResponse {
IError error = errorMeta.getError(locale);
apiResponse.setCode(error.getCode());
apiResponse.setMsg(error.getMsg());
apiResponse.setSub_code(error.getSub_code());
apiResponse.setSub_code(error.getSubCode());
apiResponse.setSub_msg(subMsg);
return apiResponse;
}
@@ -120,8 +120,8 @@ public class ApiResponse {
IError error = errorMeta.getError(locale);
apiResponse.setCode(error.getCode());
apiResponse.setMsg(error.getMsg());
apiResponse.setSub_code(error.getSub_code());
apiResponse.setSub_msg(error.getSub_msg());
apiResponse.setSub_code(error.getSubCode());
apiResponse.setSub_msg(error.getSubMsg());
return apiResponse;
}

View File

@@ -8,6 +8,8 @@ import java.io.IOException;
/**
* 参数处理
*
* @param <Req> 请求参数
* @param <Resp> 响应参数
* @author 六如
*/
public interface ParamExecutor<Req, Resp> {

View File

@@ -26,7 +26,6 @@ import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

View File

@@ -3,7 +3,6 @@ package com.gitee.sop.gateway.service.manager;
import com.gitee.sop.gateway.common.ApiInfoDTO;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -16,7 +15,8 @@ public interface IsvApiPermissionManager extends Manager<Collection<Long>, Map<L
/**
* isv是否可以访问接口
* @param isvId isvId
*
* @param isvId isvId
* @param apiInfoDTO apiInfoDTO
* @return true:能访问
*/

View File

@@ -5,7 +5,6 @@ import com.gitee.sop.gateway.dao.entity.ApiInfo;
import com.gitee.sop.gateway.dao.mapper.ApiInfoMapper;
import com.gitee.sop.gateway.service.manager.ApiManager;
import com.gitee.sop.gateway.util.CopyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@@ -18,6 +17,7 @@ import java.util.function.Function;
/**
* 本地存储接口信息.
*
* @author 六如
*/
@Slf4j
@@ -38,7 +38,7 @@ public class LocalApiManagerImpl implements ApiManager {
@Override
public ApiInfoDTO get(String apiName, String apiVersion) {
String key = apiName + apiVersion;
return CACHE.computeIfAbsent(key, k-> {
return CACHE.computeIfAbsent(key, k -> {
ApiInfo apiInfo = apiInfoMapper.getByNameVersion(apiName, apiVersion);
return Optional.ofNullable(CopyUtil.copyBean(apiInfo, ApiInfoDTO::new));
}).orElse(null);

View File

@@ -329,18 +329,8 @@ public class AlipaySignature {
/**
* 验签并解密
* <p>
* <b>目前适用于公众号</b><br>
* params参数示例
* <br>{
* <br>biz_content=M0qGiGz+8kIpxe8aF4geWJdBn0aBTuJRQItLHo9R7o5JGhpic/MIUjvXo2BLB++BbkSq2OsJCEQFDZ0zK5AJYwvBgeRX30gvEj6eXqXRt16/IkB9HzAccEqKmRHrZJ7PjQWE0KfvDAHsJqFIeMvEYk1Zei2QkwSQPlso7K0oheo/iT+HYE8aTATnkqD/ByD9iNDtGg38pCa2xnnns63abKsKoV8h0DfHWgPH62urGY7Pye3r9FCOXA2Ykm8X4/Bl1bWFN/PFCEJHWe/HXj8KJKjWMO6ttsoV0xRGfeyUO8agu6t587Dl5ux5zD/s8Lbg5QXygaOwo3Fz1G8EqmGhi4+soEIQb8DBYanQOS3X+m46tVqBGMw8Oe+hsyIMpsjwF4HaPKMr37zpW3fe7xOMuimbZ0wq53YP/jhQv6XWodjT3mL0H5ACqcsSn727B5ztquzCPiwrqyjUHjJQQefFTzOse8snaWNQTUsQS7aLsHq0FveGpSBYORyA90qPdiTjXIkVP7mAiYiAIWW9pCEC7F3XtViKTZ8FRMM9ySicfuAlf3jtap6v2KPMtQv70X+hlmzO/IXB6W0Ep8DovkF5rB4r/BJYJLw/6AS0LZM9w5JfnAZhfGM2rKzpfNsgpOgEZS1WleG4I2hoQC0nxg9IcP0Hs+nWIPkEUcYNaiXqeBc=,
* <br>sign=rlqgA8O+RzHBVYLyHmrbODVSANWPXf3pSrr82OCO/bm3upZiXSYrX5fZr6UBmG6BZRAydEyTIguEW6VRuAKjnaO/sOiR9BsSrOdXbD5Rhos/Xt7/mGUWbTOt/F+3W0/XLuDNmuYg1yIC/6hzkg44kgtdSTsQbOC9gWM7ayB4J4c=,
* sign_type=RSA,
* <br>charset=UTF-8
* <br>}
* </p>
*
* @param params
* @param params 参数
* @param alipayPublicKey 支付宝公钥
* @param cusPrivateKey 商户私钥
* @param isCheckSign 是否验签
@@ -367,18 +357,8 @@ public class AlipaySignature {
/**
* 验签并解密
* <p>
* <b>目前适用于公众号</b><br>
* params参数示例
* <br>{
* <br>biz_content=M0qGiGz+8kIpxe8aF4geWJdBn0aBTuJRQItLHo9R7o5JGhpic/MIUjvXo2BLB++BbkSq2OsJCEQFDZ0zK5AJYwvBgeRX30gvEj6eXqXRt16/IkB9HzAccEqKmRHrZJ7PjQWE0KfvDAHsJqFIeMvEYk1Zei2QkwSQPlso7K0oheo/iT+HYE8aTATnkqD/ByD9iNDtGg38pCa2xnnns63abKsKoV8h0DfHWgPH62urGY7Pye3r9FCOXA2Ykm8X4/Bl1bWFN/PFCEJHWe/HXj8KJKjWMO6ttsoV0xRGfeyUO8agu6t587Dl5ux5zD/s8Lbg5QXygaOwo3Fz1G8EqmGhi4+soEIQb8DBYanQOS3X+m46tVqBGMw8Oe+hsyIMpsjwF4HaPKMr37zpW3fe7xOMuimbZ0wq53YP/jhQv6XWodjT3mL0H5ACqcsSn727B5ztquzCPiwrqyjUHjJQQefFTzOse8snaWNQTUsQS7aLsHq0FveGpSBYORyA90qPdiTjXIkVP7mAiYiAIWW9pCEC7F3XtViKTZ8FRMM9ySicfuAlf3jtap6v2KPMtQv70X+hlmzO/IXB6W0Ep8DovkF5rB4r/BJYJLw/6AS0LZM9w5JfnAZhfGM2rKzpfNsgpOgEZS1WleG4I2hoQC0nxg9IcP0Hs+nWIPkEUcYNaiXqeBc=,
* <br>sign=rlqgA8O+RzHBVYLyHmrbODVSANWPXf3pSrr82OCO/bm3upZiXSYrX5fZr6UBmG6BZRAydEyTIguEW6VRuAKjnaO/sOiR9BsSrOdXbD5Rhos/Xt7/mGUWbTOt/F+3W0/XLuDNmuYg1yIC/6hzkg44kgtdSTsQbOC9gWM7ayB4J4c=,
* sign_type=RSA,
* <br>charset=UTF-8
* <br>}
* </p>
*
* @param params
* @param params 参数
* @param alipayPublicKey 支付宝公钥
* @param cusPrivateKey 商户私钥
* @param isCheckSign 是否验签
@@ -432,7 +412,8 @@ public class AlipaySignature {
charset = AlipayConstants.CHARSET_GBK;
}
sb.append("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>");
if (isEncrypt) {// 加密
if (isEncrypt) {
// 加密
sb.append("<alipay>");
String encrypted = rsaEncrypt(bizContent, alipayPublicKey, charset);
sb.append("<response>" + encrypted + "</response>");
@@ -443,14 +424,16 @@ public class AlipaySignature {
sb.append("<sign_type>RSA</sign_type>");
}
sb.append("</alipay>");
} else if (isSign) {// 不加密,但需要签名
} else if (isSign) {
// 不加密,但需要签名
sb.append("<alipay>");
sb.append("<response>" + bizContent + "</response>");
String sign = rsaSign(bizContent, cusPrivateKey, charset);
sb.append("<sign>" + sign + "</sign>");
sb.append("<sign_type>RSA</sign_type>");
sb.append("</alipay>");
} else {// 不加密,不加签
} else {
// 不加密,不加签
sb.append(bizContent);
}
return sb.toString();
@@ -485,7 +468,8 @@ public class AlipaySignature {
charset = AlipayConstants.CHARSET_GBK;
}
sb.append("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>");
if (isEncrypt) {// 加密
if (isEncrypt) {
// 加密
sb.append("<alipay>");
String encrypted = rsaEncrypt(bizContent, alipayPublicKey, charset);
sb.append("<response>" + encrypted + "</response>");
@@ -498,7 +482,8 @@ public class AlipaySignature {
sb.append("</sign_type>");
}
sb.append("</alipay>");
} else if (isSign) {// 不加密,但需要签名
} else if (isSign) {
// 不加密,但需要签名
sb.append("<alipay>");
sb.append("<response>" + bizContent + "</response>");
String sign = rsaSign(bizContent, cusPrivateKey, charset, signType);
@@ -507,7 +492,8 @@ public class AlipaySignature {
sb.append(signType);
sb.append("</sign_type>");
sb.append("</alipay>");
} else {// 不加密,不加签
} else {
// 不加密,不加签
sb.append(bizContent);
}
return sb.toString();

View File

@@ -75,7 +75,7 @@ public class AlipaySigner implements Signer {
static class SkipNullHashMap extends HashMap<String, String> {
private static final long serialVersionUID = -5660619374444097587L;
public SkipNullHashMap(int initialCapacity) {
SkipNullHashMap(int initialCapacity) {
super(initialCapacity);
}

View File

@@ -13,11 +13,11 @@ import java.io.StringWriter;
import java.io.Writer;
/**
*
* @author runzhi
*/
public class StreamUtil {
private StreamUtil(){}
private StreamUtil() {
}
private static final int DEFAULT_BUFFER_SIZE = 8192;
@@ -72,9 +72,9 @@ public class StreamUtil {
}
public static String readText(InputStream in, String encoding, int bufferSize)
throws IOException {
throws IOException {
Reader reader = (encoding == null) ? new InputStreamReader(in) : new InputStreamReader(in,
encoding);
encoding);
return readText(reader, bufferSize);
}
@@ -92,7 +92,7 @@ public class StreamUtil {
private static class SynchronizedOutputStream extends OutputStream {
private OutputStream out;
private Object lock;
private Object lock;
SynchronizedOutputStream(OutputStream out) {
this(out, out);

View File

@@ -6,166 +6,166 @@ package com.gitee.sop.gateway.service.validate.alipay;
* @author carver.gu
* @since 1.0, Sep 12, 2009
*/
public class StringUtils {
public final class StringUtils {
private StringUtils() {}
private StringUtils() {
}
/**
* 检查指定的字符串是否为空。
* <ul>
* <li>SysUtils.isEmpty(null) = true</li>
* <li>SysUtils.isEmpty("") = true</li>
* <li>SysUtils.isEmpty(" ") = true</li>
* <li>SysUtils.isEmpty("abc") = false</li>
* </ul>
*
* @param value 待检查的字符串
* @return true/false
*/
public static boolean isEmpty(String value) {
int strLen;
if (value == null || (strLen = value.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if ((Character.isWhitespace(value.charAt(i)) == false)) {
return false;
}
}
return true;
}
/**
* 检查指定的字符串是否为空。
* <ul>
* <li>SysUtils.isEmpty(null) = true</li>
* <li>SysUtils.isEmpty("") = true</li>
* <li>SysUtils.isEmpty(" ") = true</li>
* <li>SysUtils.isEmpty("abc") = false</li>
* </ul>
*
* @param value 待检查的字符串
* @return true/false
*/
public static boolean isEmpty(String value) {
if (value == null || value.length() == 0) {
return true;
}
for (int i = 0; i < value.length(); i++) {
if (!Character.isWhitespace(value.charAt(i))) {
return false;
}
}
return true;
}
/**
* 检查对象是否为数字型字符串,包含负数开头的。
*/
public static boolean isNumeric(Object obj) {
if (obj == null) {
return false;
}
char[] chars = obj.toString().toCharArray();
int length = chars.length;
if(length < 1) {
return false;
}
/**
* 检查对象是否为数字型字符串,包含负数开头的。
*/
public static boolean isNumeric(Object obj) {
if (obj == null) {
return false;
}
char[] chars = obj.toString().toCharArray();
int length = chars.length;
if (length < 1) {
return false;
}
int i = 0;
if(length > 1 && chars[0] == '-') {
i = 1;
}
int i = 0;
if (length > 1 && chars[0] == '-') {
i = 1;
}
for (; i < length; i++) {
if (!Character.isDigit(chars[i])) {
return false;
}
}
return true;
}
for (; i < length; i++) {
if (!Character.isDigit(chars[i])) {
return false;
}
}
return true;
}
/**
* 检查指定的字符串列表是否不为空。
*/
public static boolean areNotEmpty(String... values) {
boolean result = true;
if (values == null || values.length == 0) {
result = false;
} else {
for (String value : values) {
result &= !isEmpty(value);
}
}
return result;
}
/**
* 检查指定的字符串列表是否不为空。
*/
public static boolean areNotEmpty(String... values) {
boolean result = true;
if (values == null || values.length == 0) {
result = false;
} else {
for (String value : values) {
result &= !isEmpty(value);
}
}
return result;
}
/**
* 把通用字符编码的字符串转化为汉字编码。
*/
public static String unicodeToChinese(String unicode) {
StringBuilder out = new StringBuilder();
if (!isEmpty(unicode)) {
for (int i = 0; i < unicode.length(); i++) {
out.append(unicode.charAt(i));
}
}
return out.toString();
}
/**
* 把通用字符编码的字符串转化为汉字编码。
*/
public static String unicodeToChinese(String unicode) {
StringBuilder out = new StringBuilder();
if (!isEmpty(unicode)) {
for (int i = 0; i < unicode.length(); i++) {
out.append(unicode.charAt(i));
}
}
return out.toString();
}
/**
* 过滤不可见字符
*/
public static String stripNonValidXMLCharacters(String input) {
if (input == null || ("".equals(input))) {
return "";
}
StringBuilder out = new StringBuilder();
char current;
for (int i = 0; i < input.length(); i++) {
current = input.charAt(i);
if ((current == 0x9) || (current == 0xA) || (current == 0xD)
|| ((current >= 0x20) && (current <= 0xD7FF))
|| ((current >= 0xE000) && (current <= 0xFFFD))
|| ((current >= 0x10000) && (current <= 0x10FFFF))) {
out.append(current);
}
}
return out.toString();
}
/**
* 过滤不可见字符
*/
public static String stripNonValidXMLCharacters(String input) {
if (input == null || ("".equals(input))) {
return "";
}
StringBuilder out = new StringBuilder();
char current;
for (int i = 0; i < input.length(); i++) {
current = input.charAt(i);
if ((current == 0x9) || (current == 0xA) || (current == 0xD)
|| ((current >= 0x20) && (current <= 0xD7FF))
|| ((current >= 0xE000) && (current <= 0xFFFD))
|| ((current >= 0x10000) && (current <= 0x10FFFF))) {
out.append(current);
}
}
return out.toString();
}
public static String leftPad(String str, int size, char padChar) {
if (str == null) {
return null;
} else {
int pads = size - str.length();
if (pads <= 0) {
return str;
} else {
return pads > 8192 ? leftPad(str, size, String.valueOf(padChar)) : padding(pads, padChar).concat(str);
}
}
}
public static String leftPad(String str, int size, char padChar) {
if (str == null) {
return null;
} else {
int pads = size - str.length();
if (pads <= 0) {
return str;
} else {
return pads > 8192 ? leftPad(str, size, String.valueOf(padChar)) : padding(pads, padChar).concat(str);
}
}
}
public static String leftPad(String str, int size, String padStr) {
if (str == null) {
return null;
} else {
if (isEmpty(padStr)) {
padStr = " ";
}
public static String leftPad(String str, int size, String padStr) {
if (str == null) {
return null;
} else {
if (isEmpty(padStr)) {
padStr = " ";
}
int padLen = padStr.length();
int strLen = str.length();
int pads = size - strLen;
if (pads <= 0) {
return str;
} else if (padLen == 1 && pads <= 8192) {
return leftPad(str, size, padStr.charAt(0));
} else if (pads == padLen) {
return padStr.concat(str);
} else if (pads < padLen) {
return padStr.substring(0, pads).concat(str);
} else {
char[] padding = new char[pads];
char[] padChars = padStr.toCharArray();
int padLen = padStr.length();
int strLen = str.length();
int pads = size - strLen;
if (pads <= 0) {
return str;
} else if (padLen == 1 && pads <= 8192) {
return leftPad(str, size, padStr.charAt(0));
} else if (pads == padLen) {
return padStr.concat(str);
} else if (pads < padLen) {
return padStr.substring(0, pads).concat(str);
} else {
char[] padding = new char[pads];
char[] padChars = padStr.toCharArray();
for(int i = 0; i < pads; ++i) {
padding[i] = padChars[i % padLen];
}
for (int i = 0; i < pads; ++i) {
padding[i] = padChars[i % padLen];
}
return (new String(padding)).concat(str);
}
}
}
return (new String(padding)).concat(str);
}
}
}
private static String padding(int repeat, char padChar) {
if (repeat < 0) {
throw new IndexOutOfBoundsException("Cannot pad a negative amount: " + repeat);
} else {
char[] buf = new char[repeat];
private static String padding(int repeat, char padChar) {
if (repeat < 0) {
throw new IndexOutOfBoundsException("Cannot pad a negative amount: " + repeat);
} else {
char[] buf = new char[repeat];
for(int i = 0; i < buf.length; ++i) {
buf[i] = padChar;
}
for (int i = 0; i < buf.length; ++i) {
buf[i] = padChar;
}
return new String(buf);
}
}
return new String(buf);
}
}
}

View File

@@ -19,24 +19,26 @@ import javax.crypto.spec.SecretKeySpec;
* </pre>
*
* @author 六如
*
*/
public class AESUtil {
private static final String UTF8 = "UTF-8";
private static final String ALGORITHM = "AES";
/** 默认的加密算法 */
/**
* 默认的加密算法
*/
private static final String ALGORITHM_CIPHER = "AES/ECB/PKCS5Padding";
private static final int LIMIT_LEN = 16;
/**
* 生成一个SecretKey
*
* @param password 长度必须小于等于16
* @return 返回SecretKey
*/
public static SecretKey getSecretKey(String password) {
byte[] passwordData = password.getBytes();
if(passwordData.length > LIMIT_LEN) {
if (passwordData.length > LIMIT_LEN) {
throw new IllegalArgumentException("password 长度必须小于等于16");
}
// 创建一个空的16位字节数组默认值为0,16byte128bit
@@ -48,7 +50,8 @@ public class AESUtil {
/**
* 加密
* @param data 待加密数据
*
* @param data 待加密数据
* @param password 密码
* @return 返回加密成功后数据
* @throws Exception
@@ -65,7 +68,8 @@ public class AESUtil {
/**
* 解密
* @param data 待解密数据
*
* @param data 待解密数据
* @param password 密码
* @return 返回解密后的数据
* @throws Exception
@@ -82,7 +86,8 @@ public class AESUtil {
/**
* 文本加密
* @param content 明文
*
* @param content 明文
* @param password 密码
* @return 返回base64内容
* @throws Exception
@@ -95,8 +100,9 @@ public class AESUtil {
/**
* 文本解密
*
* @param base64String 待解密文本
* @param password 密码
* @param password 密码
* @return 返回明文
* @throws Exception
*/
@@ -108,7 +114,8 @@ public class AESUtil {
/**
* 文本加密
* @param content 明文
*
* @param content 明文
* @param password 密码
* @return 返回16进制内容
* @throws Exception
@@ -121,7 +128,8 @@ public class AESUtil {
/**
* 文本解密
* @param hex 待解密文本
*
* @param hex 待解密文本
* @param password 密码
* @return 返回明文
* @throws Exception
@@ -129,18 +137,7 @@ public class AESUtil {
public static String decryptFromHex(String hex, String password) throws Exception {
byte[] data = Hex.decodeHex(hex);
byte[] contentData = decrypt(data, password);
return new String(contentData,UTF8);
return new String(contentData, UTF8);
}
/*public static void main(String[] args) throws Exception {
String content = "我爱你";
String password = "1234567890123456";
System.out.println("password:" + password);
String ret2 = encryptToBase64String(content, password);
System.out.println("密文:" + ret2);
String content3 = decryptFromBase64String(ret2, password);
System.out.println(content.equals(content3));
}*/
}

View File

@@ -170,39 +170,4 @@ public class RSANewUtil {
}
/*
pubKey:
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCG/iIZZzb16PxKqslkDMYa4tVFb3IVPBpLj4BgHQmDfe843sG4gkJIPXCm7+t6QxIbfDfynBpqZJLvu0c6E7TqlCtynBIlRFOBZrQVNEFkaanR2Kln3vd3CIidR571UstOC32XDyqAQNlvjD19zeIDVfmLa0Q+Or0zaxY99QwBHwIDAQAB
priKey:
MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIb+IhlnNvXo/EqqyWQMxhri1UVvchU8GkuPgGAdCYN97zjewbiCQkg9cKbv63pDEht8N/KcGmpkku+7RzoTtOqUK3KcEiVEU4FmtBU0QWRpqdHYqWfe93cIiJ1HnvVSy04LfZcPKoBA2W+MPX3N4gNV+YtrRD46vTNrFj31DAEfAgMBAAECgYBiNPQdwwcq86rHr2QAE4L0AF3ju+YlKKqAmg9s3PMU5ENq/jO0xZ7u6zPPXu/S7IR51m7lY0ecazqyiW6SA9AzYH7ImWWkZ4stZ03beTB2US3cSeJIkugoexoN5fQRAGZiZezTLs91CeJivESOZyDKnnQdgJ49mveBV5OvievD8QJBAMztpqiWWavdR4tqQ+plat+rwYoXqejsK3Hyfg0pVJqEdazve2sr74rla7yI9P47ZAh1sklCv0CO//ctICv366UCQQCoop3T0FeZtbKJG+fHzZvpAe63tXpdhLMaQvTBuXLG8vi78Wyfhg5r7HOWR0Z1V7nzF1gzMywL53Pmkq9tB65zAkAiHu/A4kfL9ewTqn3kaT6CP3baJ1aDEc+qCVYzms4bbDKruLQ0A/y+g7SMj8E7E2h0gCRPTm3JsgWsgjb5Gy6BAkAA8mjQd6sGQe7utilnBdCKTmh4v5wgSk53J0kYjWIHm/WpmIFzo90Q3hMIFP5gSk3Q/6CPKQpmRrZv5QL3KcPhAkEAuMoQbij/7hyLlIxRHZs2SMXxfHPiZgDc6rVi1KNxeq8HXTlERi7Npc2Uz5TeWN4JwBBx9uA50zowk9iS05nclQ==
用公钥加密mi : c3B0jtMdvkqrgaPxHZCK2cXMUQC2QzLud2ouLMNx0nBAj9k2/ytOuVJViTGe/DozB/ky5jvl4spD9Ey6aTMrwLHfQVhn0gRJ+wHcmx/51dXQDIgsldt6bf7YpdPdnghBjQz2+P5RhqSkeFDbTZKkl2BNaLE78a/OyWWeCGwN+4s=
true
用私钥加密mi2 : QU5vDnQ1ukj8GsauokFlgcB/g61U882tj82wHGrrqHEnvaga+4cXjML9RhjpZtKqwDGZTCujsmpynDk4qek6IGOQ/oxdWLwV4ZNjfa/oqA8OFDothVUT8wpqCu9kOYHrTdGybmXD0dB2Iy1/AMQTAgPNNXXiRXdvsz9xWYTV6z8=
true
*/
/*public static void main(String[] args) throws Exception {
KeyStore keys = createKeys();
String pubKey = keys.getPublicKey();
System.out.println("pubKey:");
System.out.println(pubKey);
String priKey = keys.getPrivateKey();
System.out.println("priKey:");
System.out.println(priKey);
String ming = "1234567890123456";
// 用公钥加密
String mi = encryptByPublicKey(ming, pubKey);
System.out.println("用公钥加密mi : " + mi);
// 用私钥解密
System.out.println(ming.equals(decryptByPrivateKey(mi, priKey)));
// 用私钥加密
String mi2 = encryptByPrivateKey(ming, priKey);
System.out.println("用私钥加密mi2 : " + mi2);
// 用公钥解密
String ming2 = decryptByPublicKey(mi2, pubKey);
System.out.println(ming.equals(ming2));
}*/
}

View File

@@ -16,7 +16,7 @@ import java.security.spec.X509EncodedKeySpec;
* @author 六如
*/
public class RSAUtil {
public static String RSA_ALGORITHM = "RSA";
private static final String RSA_ALGORITHM = "RSA";
/**
* 创建公钥私钥
@@ -102,9 +102,9 @@ public class RSAUtil {
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
// 模长
int key_len = publicKey.getModulus().bitLength() / 8;
int keyLen = publicKey.getModulus().bitLength() / 8;
// 加密数据长度 <= 模长-11
String[] datas = splitString(data, key_len - 11);
String[] datas = splitString(data, keyLen - 11);
String mi = "";
// 如果明文长度大于模长-11则要分组加密
for (String s : datas) {
@@ -128,9 +128,9 @@ public class RSAUtil {
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
// 模长
int key_len = privateKey.getModulus().bitLength() / 8;
int keyLen = privateKey.getModulus().bitLength() / 8;
// 加密数据长度 <= 模长-11
String[] datas = splitString(data, key_len - 11);
String[] datas = splitString(data, keyLen - 11);
String mi = "";
// 如果明文长度大于模长-11则要分组加密
for (String s : datas) {
@@ -155,12 +155,12 @@ public class RSAUtil {
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
// 模长
int key_len = privateKey.getModulus().bitLength() / 8;
int keyLen = privateKey.getModulus().bitLength() / 8;
byte[] bytes = data.getBytes();
byte[] bcd = ASCII_To_BCD(bytes, bytes.length);
byte[] bcd = asciiToBcd(bytes, bytes.length);
// 如果密文长度大于模长则要分组解密
String ming = "";
byte[][] arrays = splitArray(bcd, key_len);
byte[][] arrays = splitArray(bcd, keyLen);
for (byte[] arr : arrays) {
ming += new String(cipher.doFinal(arr));
}
@@ -179,12 +179,12 @@ public class RSAUtil {
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, rsaPublicKey);
// 模长
int key_len = rsaPublicKey.getModulus().bitLength() / 8;
int keyLen = rsaPublicKey.getModulus().bitLength() / 8;
byte[] bytes = data.getBytes();
byte[] bcd = ASCII_To_BCD(bytes, bytes.length);
byte[] bcd = asciiToBcd(bytes, bytes.length);
// 如果密文长度大于模长则要分组解密
String ming = "";
byte[][] arrays = splitArray(bcd, key_len);
byte[][] arrays = splitArray(bcd, keyLen);
for (byte[] arr : arrays) {
ming += new String(cipher.doFinal(arr));
}
@@ -196,17 +196,17 @@ public class RSAUtil {
* ASCII码转BCD码
*
*/
public static byte[] ASCII_To_BCD(byte[] ascii, int asc_len) {
byte[] bcd = new byte[asc_len / 2];
public static byte[] asciiToBcd(byte[] ascii, int ascLen) {
byte[] bcd = new byte[ascLen / 2];
int j = 0;
for (int i = 0; i < (asc_len + 1) / 2; i++) {
bcd[i] = asc_to_bcd(ascii[j++]);
bcd[i] = (byte) (((j >= asc_len) ? 0x00 : asc_to_bcd(ascii[j++]) & 0xff) + (bcd[i] << 4));
for (int i = 0; i < (ascLen + 1) / 2; i++) {
bcd[i] = ascToBcd(ascii[j++]);
bcd[i] = (byte) (((j >= ascLen) ? 0x00 : ascToBcd(ascii[j++]) & 0xff) + (bcd[i] << 4));
}
return bcd;
}
public static byte asc_to_bcd(byte asc) {
public static byte ascToBcd(byte asc) {
byte bcd;
if ((asc >= '0') && (asc <= '9')) {
@@ -285,28 +285,4 @@ public class RSAUtil {
return arrays;
}
/*public static void main(String[] args) throws Exception {
KeyStore keys = createKeys();
String pubKey = keys.getPublicKey();
System.out.println("pubKey:");
System.out.println(pubKey);
String priKey = keys.getPrivateKey();
System.out.println("priKey:");
System.out.println(priKey);
String ming = "6460201d23954f8e90cf79b818844ca0";
// 用公钥加密
String mi = encryptByPublicKey(ming, getPublicKey(pubKey));
System.out.println("mi : " + mi);
// 用私钥解密
System.out.println("ming : " + decryptByPrivateKey(mi, getPrivateKey(priKey)));
// 用私钥加密
String mi2 = encryptByPrivateKey(ming, getPrivateKey(priKey));
System.out.println("mi2 : " + mi2);
// 用公钥解密
System.out.println("ming2 : " + decryptByPublicKey(mi2, getPublicKey(pubKey)));
}*/
}