保存进度
This commit is contained in:
parent
9f805266d6
commit
b2237d3270
0
code/cpicxim-huixiabao/.mvn/jvm.config
Normal file
0
code/cpicxim-huixiabao/.mvn/jvm.config
Normal file
0
code/cpicxim-huixiabao/.mvn/maven.config
Normal file
0
code/cpicxim-huixiabao/.mvn/maven.config
Normal file
4
code/cpicxim-huixiabao/.vscode/settings.json
vendored
Normal file
4
code/cpicxim-huixiabao/.vscode/settings.json
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"java.configuration.updateBuildConfiguration": "automatic",
|
||||||
|
"java.compile.nullAnalysis.mode": "automatic"
|
||||||
|
}
|
130
code/cpicxim-huixiabao/pom.xml
Normal file
130
code/cpicxim-huixiabao/pom.xml
Normal file
@ -0,0 +1,130 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.cpic.xim</groupId>
|
||||||
|
<artifactId>cpicxim-huixiabao</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<packaging>war</packaging>
|
||||||
|
|
||||||
|
<name>cpicxim-huixiabao Maven Webapp</name>
|
||||||
|
<!-- FIXME change it to the project's website -->
|
||||||
|
<url>http://www.example.com</url>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<maven.compiler.source>18</maven.compiler.source>
|
||||||
|
<maven.compiler.target>18</maven.compiler.target>
|
||||||
|
<spring.version>6.2.4</spring.version>
|
||||||
|
<log4j.version>2.24.3</log4j.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.13.1</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-context</artifactId>
|
||||||
|
<version>${spring.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-test</artifactId>
|
||||||
|
<version>${spring.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-web</artifactId>
|
||||||
|
<version>${spring.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-webmvc</artifactId>
|
||||||
|
<version>${spring.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.servlet</groupId>
|
||||||
|
<artifactId>javax.servlet-api</artifactId>
|
||||||
|
<version>4.0.1</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.logging.log4j</groupId>
|
||||||
|
<artifactId>log4j-core</artifactId>
|
||||||
|
<version>${log4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.logging.log4j</groupId>
|
||||||
|
<artifactId>log4j-slf4j2-impl</artifactId>
|
||||||
|
<version>${log4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.logging.log4j</groupId>
|
||||||
|
<artifactId>log4j-api</artifactId>
|
||||||
|
<version>${log4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.directory.studio</groupId>
|
||||||
|
<artifactId>org.apache.commons.codec</artifactId>
|
||||||
|
<version>1.8</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>cpicxim-huixiabao</finalName>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>src/main/resources</directory>
|
||||||
|
<includes>
|
||||||
|
<include>**/*.properties</include>
|
||||||
|
<include>**/*.xml</include>
|
||||||
|
</includes>
|
||||||
|
<filtering>false</filtering>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to
|
||||||
|
parent pom) -->
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-war-plugin</artifactId>
|
||||||
|
<version>3.4.0</version>
|
||||||
|
<configuration>
|
||||||
|
<outputDirectory>${project.build.directory}/../../../../输出/back/</outputDirectory>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>3.5.2</version>
|
||||||
|
<configuration>
|
||||||
|
<skipTests>true</skipTests>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.14.0</version>
|
||||||
|
<configuration>
|
||||||
|
<source>18</source>
|
||||||
|
<target>18</target>
|
||||||
|
<encoding>UTF-8</encoding>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</pluginManagement>
|
||||||
|
</build>
|
||||||
|
</project>
|
@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
* @Author: Kane
|
||||||
|
* @Date: 2025-03-15 11:56:54
|
||||||
|
* @LastEditors: Kane
|
||||||
|
* @FilePath: /cpicxim-huixiabao/src/main/java/com/cpic/xim/huixiabao/web/filters/cros/CrosFilter.java
|
||||||
|
* @Description:
|
||||||
|
*
|
||||||
|
* Copyright (c) ${2023} by Kane, All Rights Reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.cpic.xim.huixiabao.web.filters.cros;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import javax.servlet.Filter;
|
||||||
|
import javax.servlet.FilterChain;
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.ServletRequest;
|
||||||
|
import javax.servlet.ServletResponse;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
|
|
||||||
|
public class CrosFilter implements Filter
|
||||||
|
{
|
||||||
|
private static Logger logger = LoggerFactory.getLogger( CrosFilter.class );
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void doFilter( ServletRequest req, ServletResponse resp, FilterChain chain )
|
||||||
|
throws ServletException,
|
||||||
|
IOException
|
||||||
|
{
|
||||||
|
HttpServletRequest request = ( HttpServletRequest ) req;
|
||||||
|
HttpServletResponse response = ( HttpServletResponse ) resp;
|
||||||
|
String method = request.getMethod();
|
||||||
|
String originHeader = request.getHeader( "Origin" );
|
||||||
|
|
||||||
|
logger.info( "收到" + method + "请求,来自" + originHeader );
|
||||||
|
|
||||||
|
// 如果是Options请求
|
||||||
|
if ( method.equals( HttpMethod.OPTIONS.toString() ) )
|
||||||
|
{
|
||||||
|
originHeader = "*";
|
||||||
|
}
|
||||||
|
|
||||||
|
response.setHeader( "Access-Control-Allow-Origin", originHeader );
|
||||||
|
response.setHeader( "Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT" );
|
||||||
|
response.setHeader( "Access-Control-Max-Age", "0" );
|
||||||
|
response.setHeader( "Access-Control-Allow-Headers",
|
||||||
|
"Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With,userId,token" );
|
||||||
|
response.setHeader( "Access-Control-Allow-Credentials", "true" );
|
||||||
|
response.setHeader( "XDomainRequestAllowed", "1" );
|
||||||
|
response.setHeader( "XDomainRequestAllowed", "1" );
|
||||||
|
|
||||||
|
chain.doFilter( request, response );
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,116 @@
|
|||||||
|
/*
|
||||||
|
* @Author: Kane
|
||||||
|
*
|
||||||
|
* @Date: 2025-03-15 12:04:19
|
||||||
|
*
|
||||||
|
* @LastEditors: Kane
|
||||||
|
*
|
||||||
|
* @FilePath: /cpicxim-huixiabao/src/main/java/com/cpic/xim/utils/secrecy/AESUtils.java
|
||||||
|
*
|
||||||
|
* @Description:
|
||||||
|
*
|
||||||
|
* Copyright (c) ${2023} by Kane, All Rights Reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.cpic.xim.utils.secrecy;
|
||||||
|
|
||||||
|
import javax.crypto.Cipher;
|
||||||
|
import javax.crypto.spec.IvParameterSpec;
|
||||||
|
import javax.crypto.spec.SecretKeySpec;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AESUtils
|
||||||
|
*/
|
||||||
|
public class AESUtils
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 偏移量,必须是16位字符串
|
||||||
|
*/
|
||||||
|
private static final String IV_STRING = "16-Bytes--String";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 产生随机密钥(这里产生密钥必须是16位)
|
||||||
|
*/
|
||||||
|
private static String generateKey()
|
||||||
|
{
|
||||||
|
String key = UUID.randomUUID().toString();
|
||||||
|
key = key.replace( "-", "" ).substring( 0, 16 );// 替换掉-号
|
||||||
|
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加密
|
||||||
|
*
|
||||||
|
* @param publicKey 公钥
|
||||||
|
* @param content 加密数据
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static Map<String, String> encryptData( String publicKey, String content )
|
||||||
|
{
|
||||||
|
byte[] encryptedBytes;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
byte[] byteContent = content.getBytes( "UTF-8" );
|
||||||
|
// 注意,为了能与 iOS 统一
|
||||||
|
// 这里的 key 不可以使用 KeyGenerator、SecureRandom、SecretKey 生成
|
||||||
|
String key = generateKey();
|
||||||
|
byte[] enCodeFormat = key.getBytes();
|
||||||
|
SecretKeySpec secretKeySpec = new SecretKeySpec( enCodeFormat, "AES" );
|
||||||
|
byte[] initParam = IV_STRING.getBytes();
|
||||||
|
IvParameterSpec ivParameterSpec = new IvParameterSpec( initParam );
|
||||||
|
// 指定加密的算法、工作模式和填充方式
|
||||||
|
Cipher cipher = Cipher.getInstance( "AES/CBC/PKCS5Padding" );
|
||||||
|
cipher.init( Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec );
|
||||||
|
encryptedBytes = cipher.doFinal( byteContent );
|
||||||
|
// 同样对加密后数据进行 base64 编码
|
||||||
|
key = RSAUtils.encryptByPublicKey( key, publicKey );
|
||||||
|
Map<String, String> result = new HashMap<>( 2 );
|
||||||
|
result.put( "key", key );
|
||||||
|
result.put( "content", Base64Utils.encode( encryptedBytes ) );
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
catch ( Exception e )
|
||||||
|
{
|
||||||
|
throw new RuntimeException( "加密失败:" + e.getMessage(), e );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解密
|
||||||
|
*
|
||||||
|
* @param key
|
||||||
|
* @param privateKey
|
||||||
|
* @param content
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String decryptData( String key, String privateKey, String content )
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
System.out.println( "KEY:" + key );
|
||||||
|
System.out.println( "privateKey:" + privateKey );
|
||||||
|
System.out.println( "content:" + content );
|
||||||
|
// base64 解码
|
||||||
|
byte[] encryptedBytes = Base64Utils.decode( content );
|
||||||
|
key = RSAUtils.decryptByPrivateKey( privateKey, key );
|
||||||
|
byte[] enCodeFormat = key.getBytes();
|
||||||
|
SecretKeySpec secretKey = new SecretKeySpec( enCodeFormat, "AES" );
|
||||||
|
byte[] initParam = IV_STRING.getBytes();
|
||||||
|
IvParameterSpec ivParameterSpec = new IvParameterSpec( initParam );
|
||||||
|
Cipher cipher = Cipher.getInstance( "AES/CBC/PKCS5Padding" );
|
||||||
|
cipher.init( Cipher.DECRYPT_MODE, secretKey, ivParameterSpec );
|
||||||
|
byte[] result = cipher.doFinal( encryptedBytes );
|
||||||
|
return new String( result, "UTF-8" );
|
||||||
|
}
|
||||||
|
catch ( Exception e )
|
||||||
|
{
|
||||||
|
throw new RuntimeException( "解密失败:" + e.getMessage(), e );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,164 @@
|
|||||||
|
/*
|
||||||
|
* @Author: Kane
|
||||||
|
* @Date: 2025-03-15 12:04:19
|
||||||
|
* @LastEditors: Kane
|
||||||
|
* @FilePath: /cpicxim-huixiabao/src/main/java/com/cpic/xim/utils/secrecy/Base64Utils.java
|
||||||
|
* @Description:
|
||||||
|
*
|
||||||
|
* Copyright (c) ${2023} by Kane, All Rights Reserved.
|
||||||
|
*/
|
||||||
|
package com.cpic.xim.utils.secrecy;
|
||||||
|
|
||||||
|
import org.apache.commons.codec.binary.Base64;
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base64Utils
|
||||||
|
*/
|
||||||
|
public class Base64Utils
|
||||||
|
{
|
||||||
|
|
||||||
|
/** */
|
||||||
|
/**
|
||||||
|
* 文件读取缓冲区大小
|
||||||
|
*/
|
||||||
|
private static final int CACHE_SIZE = 1024;
|
||||||
|
|
||||||
|
/** */
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* BASE64字符串解码为二进制数据
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param base64
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public static byte[] decode( String base64 )
|
||||||
|
throws Exception
|
||||||
|
{
|
||||||
|
return Base64.decodeBase64( base64.getBytes() );
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String decode( byte[] b )
|
||||||
|
throws Exception
|
||||||
|
{
|
||||||
|
return new String( Base64.decodeBase64( b ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
/** */
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* 二进制数据编码为BASE64字符串
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param bytes
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String encode( byte[] bytes )
|
||||||
|
{
|
||||||
|
return new String( Base64.encodeBase64( bytes ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
/** */
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* 将文件编码为BASE64字符串
|
||||||
|
* </p>
|
||||||
|
* <p>
|
||||||
|
* 大文件慎用,可能会导致内存溢出
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param filePath 文件绝对路径
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public static String encodeFile( String filePath )
|
||||||
|
throws Exception
|
||||||
|
{
|
||||||
|
byte[] bytes = fileToByte( filePath );
|
||||||
|
return encode( bytes );
|
||||||
|
}
|
||||||
|
|
||||||
|
/** */
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* BASE64字符串转回文件
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param filePath 文件绝对路径
|
||||||
|
* @param base64 编码字符串
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public static void decodeToFile( String filePath, String base64 )
|
||||||
|
throws Exception
|
||||||
|
{
|
||||||
|
byte[] bytes = decode( base64 );
|
||||||
|
byteArrayToFile( bytes, filePath );
|
||||||
|
}
|
||||||
|
|
||||||
|
/** */
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* 文件转换为二进制数组
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param filePath 文件路径
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public static byte[] fileToByte( String filePath )
|
||||||
|
throws Exception
|
||||||
|
{
|
||||||
|
byte[] data = new byte[0];
|
||||||
|
File file = new File( filePath );
|
||||||
|
if ( file.exists() )
|
||||||
|
{
|
||||||
|
FileInputStream in = new FileInputStream( file );
|
||||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream( 2048 );
|
||||||
|
byte[] cache = new byte[CACHE_SIZE];
|
||||||
|
int nRead = 0;
|
||||||
|
while ( (nRead = in.read( cache )) != -1 )
|
||||||
|
{
|
||||||
|
out.write( cache, 0, nRead );
|
||||||
|
out.flush();
|
||||||
|
}
|
||||||
|
out.close();
|
||||||
|
in.close();
|
||||||
|
data = out.toByteArray();
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** */
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* 二进制数据写文件
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param bytes 二进制数据
|
||||||
|
* @param filePath 文件生成目录
|
||||||
|
*/
|
||||||
|
public static void byteArrayToFile( byte[] bytes, String filePath )
|
||||||
|
throws Exception
|
||||||
|
{
|
||||||
|
InputStream in = new ByteArrayInputStream( bytes );
|
||||||
|
File destFile = new File( filePath );
|
||||||
|
if ( !destFile.getParentFile().exists() )
|
||||||
|
{
|
||||||
|
destFile.getParentFile().mkdirs();
|
||||||
|
}
|
||||||
|
destFile.createNewFile();
|
||||||
|
OutputStream out = new FileOutputStream( destFile );
|
||||||
|
byte[] cache = new byte[CACHE_SIZE];
|
||||||
|
int nRead = 0;
|
||||||
|
while ( (nRead = in.read( cache )) != -1 )
|
||||||
|
{
|
||||||
|
out.write( cache, 0, nRead );
|
||||||
|
out.flush();
|
||||||
|
}
|
||||||
|
out.close();
|
||||||
|
in.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,169 @@
|
|||||||
|
package com.cpic.xim.utils.secrecy;
|
||||||
|
|
||||||
|
import javax.crypto.Cipher;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.math.BigInteger;
|
||||||
|
import java.security.*;
|
||||||
|
import java.security.interfaces.RSAPrivateKey;
|
||||||
|
import java.security.interfaces.RSAPublicKey;
|
||||||
|
import java.security.spec.PKCS8EncodedKeySpec;
|
||||||
|
import java.security.spec.RSAPrivateKeySpec;
|
||||||
|
import java.security.spec.X509EncodedKeySpec;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RSAUtils
|
||||||
|
*/
|
||||||
|
public class RSAUtils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RSA最大加密明文大小
|
||||||
|
*/
|
||||||
|
private static final int MAX_ENCRYPT_BLOCK = 117;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RSA最大解密密文大小
|
||||||
|
*/
|
||||||
|
private static final int MAX_DECRYPT_BLOCK = 128;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加密算法RSA
|
||||||
|
*/
|
||||||
|
private static final String KEY_ALGORITHM = "RSA";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成公钥和私钥
|
||||||
|
*
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public static Map<String, String> getKeys() throws Exception {
|
||||||
|
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
|
||||||
|
keyPairGen.initialize(1024);
|
||||||
|
KeyPair keyPair = keyPairGen.generateKeyPair();
|
||||||
|
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
|
||||||
|
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
|
||||||
|
|
||||||
|
String publicKeyStr = getPublicKeyStr(publicKey);
|
||||||
|
String privateKeyStr = getPrivateKeyStr(privateKey);
|
||||||
|
|
||||||
|
Map<String, String> result = new HashMap<>(2);
|
||||||
|
result.put("publicKeyStr", publicKeyStr);
|
||||||
|
result.put("privateKeyStr", privateKeyStr);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
Map<String, String> keys = getKeys();
|
||||||
|
System.out.println("publicKeyStr=" + keys.get("publicKeyStr"));
|
||||||
|
System.out.println("privateKeyStr=" + keys.get("privateKeyStr"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用模和指数生成RSA私钥
|
||||||
|
* 注意:【此代码用了默认补位方式,为RSA/None/PKCS1Padding,不同JDK默认的补位方式可能不同,如Android默认是RSA
|
||||||
|
* /None/NoPadding】
|
||||||
|
*
|
||||||
|
* @param modulus 模
|
||||||
|
* @param exponent 指数
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static RSAPrivateKey getPrivateKey(String modulus, String exponent) {
|
||||||
|
try {
|
||||||
|
BigInteger b1 = new BigInteger(modulus);
|
||||||
|
BigInteger b2 = new BigInteger(exponent);
|
||||||
|
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||||
|
RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(b1, b2);
|
||||||
|
return (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 公钥加密
|
||||||
|
*
|
||||||
|
* @param data 加密数据
|
||||||
|
* @param publicKey 加密key
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public static String encryptByPublicKey(String data, String publicKey) throws Exception {
|
||||||
|
byte[] dataByte = data.getBytes();
|
||||||
|
byte[] keyBytes = Base64Utils.decode(publicKey);
|
||||||
|
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
|
||||||
|
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
|
||||||
|
Key publicK = keyFactory.generatePublic(x509KeySpec);
|
||||||
|
// 对数据加密
|
||||||
|
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
|
||||||
|
cipher.init(Cipher.ENCRYPT_MODE, publicK);
|
||||||
|
int inputLen = dataByte.length;
|
||||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
|
int offSet = 0;
|
||||||
|
byte[] cache;
|
||||||
|
int i = 0;
|
||||||
|
// 对数据分段加密
|
||||||
|
while (inputLen - offSet > 0) {
|
||||||
|
if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
|
||||||
|
cache = cipher.doFinal(dataByte, offSet, MAX_ENCRYPT_BLOCK);
|
||||||
|
} else {
|
||||||
|
cache = cipher.doFinal(dataByte, offSet, inputLen - offSet);
|
||||||
|
}
|
||||||
|
out.write(cache, 0, cache.length);
|
||||||
|
i++;
|
||||||
|
offSet = i * MAX_ENCRYPT_BLOCK;
|
||||||
|
}
|
||||||
|
byte[] encryptedData = out.toByteArray();
|
||||||
|
out.close();
|
||||||
|
return Base64Utils.encode(encryptedData);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 私钥解密
|
||||||
|
*
|
||||||
|
* @param privateKey 私钥
|
||||||
|
* @param data 加密数据
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public static String decryptByPrivateKey(String privateKey, String data) throws Exception {
|
||||||
|
byte[] encryptedData = Base64Utils.decode(data);
|
||||||
|
byte[] keyBytes = Base64Utils.decode(privateKey);
|
||||||
|
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
|
||||||
|
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
|
||||||
|
Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
|
||||||
|
// Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
|
||||||
|
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
|
||||||
|
|
||||||
|
cipher.init(Cipher.DECRYPT_MODE, privateK);
|
||||||
|
int inputLen = encryptedData.length;
|
||||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
|
int offSet = 0;
|
||||||
|
byte[] cache;
|
||||||
|
int i = 0;
|
||||||
|
// 对数据分段解密
|
||||||
|
while (inputLen - offSet > 0) {
|
||||||
|
if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
|
||||||
|
cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK);
|
||||||
|
} else {
|
||||||
|
cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet);
|
||||||
|
}
|
||||||
|
out.write(cache, 0, cache.length);
|
||||||
|
i++;
|
||||||
|
offSet = i * MAX_DECRYPT_BLOCK;
|
||||||
|
}
|
||||||
|
byte[] decryptedData = out.toByteArray();
|
||||||
|
out.close();
|
||||||
|
return new String(decryptedData);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getPrivateKeyStr(PrivateKey privateKey) {
|
||||||
|
return Base64Utils.encode(privateKey.getEncoded());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getPublicKeyStr(PublicKey publicKey) {
|
||||||
|
return Base64Utils.encode(publicKey.getEncoded());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns:context="http://www.springframework.org/schema/context"
|
||||||
|
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
|
||||||
|
xmlns:jee="http://www.springframework.org/schema/jee"
|
||||||
|
xmlns:tx="http://www.springframework.org/schema/tx"
|
||||||
|
xmlns:aop="http://www.springframework.org/schema/aop"
|
||||||
|
xmlns:mvc="http://www.springframework.org/schema/mvc"
|
||||||
|
xmlns:util="http://www.springframework.org/schema/util"
|
||||||
|
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
|
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
|
||||||
|
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
|
||||||
|
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
|
||||||
|
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
|
||||||
|
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
|
||||||
|
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
|
||||||
|
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
|
||||||
|
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
|
||||||
|
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
|
||||||
|
|
||||||
|
<context:component-scan base-package="com.cpic.xim" />
|
||||||
|
<mvc:annotation-driven />
|
||||||
|
<mvc:default-servlet-handler />
|
||||||
|
|
||||||
|
<!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
|
||||||
|
<property
|
||||||
|
name="prefix" value="/WEB-INF/jsp/" />
|
||||||
|
<property name="suffix" value=".jsp" />
|
||||||
|
</bean> -->
|
||||||
|
|
||||||
|
<bean id="multipartResolver"
|
||||||
|
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
|
||||||
|
<property name="defaultEncoding" value="UTF-8" />
|
||||||
|
<property name="maxUploadSize" value="-1" />
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
</beans>
|
25
code/cpicxim-huixiabao/src/main/webapp/WEB-INF/web.xml
Normal file
25
code/cpicxim-huixiabao/src/main/webapp/WEB-INF/web.xml
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<web-app
|
||||||
|
version="4.0"
|
||||||
|
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||||
|
xmlns:javaee="http://xmlns.jcp.org/xml/ns/javaee"
|
||||||
|
xmlns:xml="http://www.w3.org/XML/1998/namespace"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd">
|
||||||
|
<display-name>Archetype Created Web Application</display-name>
|
||||||
|
|
||||||
|
<servlet>
|
||||||
|
<servlet-name>springmvc</servlet-name>
|
||||||
|
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
|
||||||
|
<init-param>
|
||||||
|
<param-name>contextConfigLocation</param-name>
|
||||||
|
<param-value>classpath:spring.xml</param-value>
|
||||||
|
</init-param>
|
||||||
|
<load-on-startup>1</load-on-startup>
|
||||||
|
</servlet>
|
||||||
|
<servlet-mapping>
|
||||||
|
<servlet-name>springmvc</servlet-name>
|
||||||
|
<url-pattern>/huixibao</url-pattern>
|
||||||
|
</servlet-mapping>
|
||||||
|
|
||||||
|
</web-app>
|
5
code/cpicxim-huixiabao/src/main/webapp/index.jsp
Normal file
5
code/cpicxim-huixiabao/src/main/webapp/index.jsp
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<h2><%= "Hello World!" %></h2>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
x
Reference in New Issue
Block a user