rsa 加密

 

des 3des 对称加密 唯一key 既可以加密 又可以解密  
rsa sm2 非对称密钥   公钥和私钥
sha md5 散列hash  

使用des 数据加解密,使用rsa对des 的key 进行加解密,保证数据的高效率传输

 

 

 

 

下面有rsa.js 的压缩包

 

public class RsaResponse {
    public int code;
    public String publicKeyExponent;
    public String publicKeyModulus;
}

 

 

import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.util.HashMap;

import javax.crypto.Cipher;

public class RSAUtils
{
	/**
	 * 生成公钥和私钥
	 * 
	 * @throws NoSuchAlgorithmException
	 * 
	 */
	public static HashMap<String, Object> getKeys() throws NoSuchAlgorithmException
	{
		Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
		HashMap<String, Object> map = new HashMap<String, Object>();
		KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA",
				new org.bouncycastle.jce.provider.BouncyCastleProvider());
		keyPairGen.initialize(1024);
		KeyPair keyPair = keyPairGen.generateKeyPair();
		RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
		RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
		map.put("public", publicKey);
		map.put("private", privateKey);
		return map;
	}

	/**
	 * 使用模和指数生成RSA公钥
	 * 
	 * @param modulus
	 *            模
	 * @param exponent
	 *            指数
	 * @return
	 */
	public static RSAPublicKey getPublicKey(String modulus, String exponent)
	{
		Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
		try
		{
			BigInteger b1 = new BigInteger(modulus);
			BigInteger b2 = new BigInteger(exponent);
			KeyFactory keyFactory = KeyFactory.getInstance("RSA",
					new org.bouncycastle.jce.provider.BouncyCastleProvider());
			RSAPublicKeySpec keySpec = new RSAPublicKeySpec(b1, b2);
			return (RSAPublicKey) keyFactory.generatePublic(keySpec);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 使用模和指数生成RSA私钥 /None/NoPadding
	 * 
	 * @param modulus
	 *            模
	 * @param exponent
	 *            指数
	 * @return
	 */
	public static RSAPrivateKey getPrivateKey(String modulus, String exponent)
	{
		try
		{
			Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
			BigInteger b1 = new BigInteger(modulus);
			BigInteger b2 = new BigInteger(exponent);
			KeyFactory keyFactory = KeyFactory.getInstance("RSA",
					new org.bouncycastle.jce.provider.BouncyCastleProvider());
			RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(b1, b2);
			return (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 公钥加密
	 * 
	 * @param data
	 * @param publicKey
	 * @return
	 * @throws Exception
	 */
	public static String encryptByPublicKey(String data, RSAPublicKey publicKey) throws Exception
	{
		Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
		Cipher cipher = Cipher.getInstance("RSA", new org.bouncycastle.jce.provider.BouncyCastleProvider());
		cipher.init(Cipher.ENCRYPT_MODE, publicKey);
		// 模长
		int key_len = publicKey.getModulus().bitLength() / 8;
		// 加密数据长度 <= 模长-11
		String[] datas = splitString(data, key_len - 11);
		String mi = "";
		// 如果明文长度大于模长-11则要分组加密
		for (String s : datas)
		{
			mi += bcd2Str(cipher.doFinal(s.getBytes()));
		}
		return mi;
	}

	/**
	 * 私钥解密
	 * 
	 * @param data
	 * @param privateKey
	 * @return
	 * @throws Exception
	 */
	public static String decryptByPrivateKey(String data, RSAPrivateKey privateKey) throws Exception
	{
		Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
		Cipher cipher = Cipher.getInstance("RSA", new org.bouncycastle.jce.provider.BouncyCastleProvider());
		cipher.init(Cipher.DECRYPT_MODE, privateKey);
		// 模长
		int key_len = privateKey.getModulus().bitLength() / 8;
		byte[] bytes = data.getBytes();
		byte[] bcd = ASCII_To_BCD(bytes, bytes.length);
		// System.err.println(bcd.length);
		// 如果密文长度大于模长则要分组解密
		String ming = "";
		byte[][] arrays = splitArray(bcd, key_len);
		for (byte[] arr : arrays)
		{
			ming += new String(cipher.doFinal(arr));
		}
		return ming;
	}

	/**
	 * ASCII码转BCD码
	 * 
	 */
	public static byte[] ASCII_To_BCD(byte[] ascii, int asc_len)
	{
		byte[] bcd = new byte[asc_len / 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++])) + (bcd[i] << 4));
		}
		return bcd;
	}

	public static byte asc_to_bcd(byte asc)
	{
		byte bcd;

		if ((asc >= '0') && (asc <= '9'))
			bcd = (byte) (asc - '0');
		else if ((asc >= 'A') && (asc <= 'F'))
			bcd = (byte) (asc - 'A' + 10);
		else if ((asc >= 'a') && (asc <= 'f'))
			bcd = (byte) (asc - 'a' + 10);
		else
			bcd = (byte) (asc - 48);
		return bcd;
	}

	/**
	 * BCD转字符串
	 */
	public static String bcd2Str(byte[] bytes)
	{
		char temp[] = new char[bytes.length * 2], val;

		for (int i = 0; i < bytes.length; i++)
		{
			val = (char) (((bytes[i] & 0xf0) >> 4) & 0x0f);
			temp[i * 2] = (char) (val > 9 ? val + 'A' - 10 : val + '0');

			val = (char) (bytes[i] & 0x0f);
			temp[i * 2 + 1] = (char) (val > 9 ? val + 'A' - 10 : val + '0');
		}
		return new String(temp);
	}

	/**
	 * 拆分字符串
	 */
	public static String[] splitString(String string, int len)
	{
		int x = string.length() / len;
		int y = string.length() % len;
		int z = 0;
		if (y != 0)
		{
			z = 1;
		}
		String[] strings = new String[x + z];
		String str = "";
		for (int i = 0; i < x + z; i++)
		{
			if (i == x + z - 1 && y != 0)
			{
				str = string.substring(i * len, i * len + y);
			}
			else
			{
				str = string.substring(i * len, i * len + len);
			}
			strings[i] = str;
		}
		return strings;
	}

	/**
	 *拆分数组
	 */
	public static byte[][] splitArray(byte[] data, int len)
	{
		int x = data.length / len;
		int y = data.length % len;
		int z = 0;
		if (y != 0)
		{
			z = 1;
		}
		byte[][] arrays = new byte[x + z][];
		byte[] arr;
		for (int i = 0; i < x + z; i++)
		{
			arr = new byte[len];
			if (i == x + z - 1 && y != 0)
			{
				System.arraycopy(data, i * len, arr, 0, y);
			}
			else
			{
				System.arraycopy(data, i * len, arr, 0, len);
			}
			arrays[i] = arr;
		}
		return arrays;
	}
	// public static void main(String[] args) throws Exception{
	// HashMap<String, Object> map = getKeys();
	// //生成公钥和私钥
	// RSAPublicKey publicKey = (RSAPublicKey) map.get("public");
	// RSAPrivateKey privateKey = (RSAPrivateKey) map.get("private");
	//          
	// //模
	// String modulus = publicKey.getModulus().toString();
	// System.out.println("pubkey modulus="+modulus);
	// //公钥指数
	// String public_exponent = publicKey.getPublicExponent().toString();
	// System.out.println("pubkey exponent="+public_exponent);
	// //私钥指数
	// String private_exponent = privateKey.getPrivateExponent().toString();
	// System.out.println("private exponent="+private_exponent);
	// //明文
	// String ming = "111";
	// //使用模和指数生成公钥和私钥
	// RSAPublicKey pubKey = RSAUtils.getPublicKey(modulus, public_exponent);
	// RSAPrivateKey priKey = RSAUtils.getPrivateKey(modulus, private_exponent);
	// //加密后的密文
	// String mi = RSAUtils.encryptByPublicKey(ming, pubKey);
	// System.err.println("mi="+mi);
	// //解密后的明文
	// String ming2 = RSAUtils.decryptByPrivateKey(mi, priKey);
	// System.err.println("ming2="+ming2);
	// }
}

 

 

 

 

//获取密钥
/**
 * 对str做RSA加密,密钥是key,返回加密好的字符串
 */
function encrypPassword(str, key){
	str = str.split("").reverse().join("");
	var encrypedPwd = RSAUtils.encryptedString(key, str);
	return encrypedPwd;
}
/*<%-- //'<%=request.getContextPath()%>/user/rsaKey.action' --%>
*/   
function rsaKey(element) {  
	$.ajax({
	    cache: true,
	    type: "get",
	    url:'/FusionAbility/user/rsaKey.action', 
	    dataType: "json",
	    async: false,
	    error: function(data) {
	        alert("请刷新页面后重新登录");
	    },
	    success: function(data) {
	    	RSAUtils.setMaxDigits(200);
	    	key = new RSAUtils.getKeyPair(data.publicKeyExponent, "", data.publicKeyModulus);
	    	console.log(element.val());
	    	element.val(encrypPassword(element.val(), key));
	    	console.log(element.val());
	    }
	});    
}

 

 

	public void rsaKey() {
		RsaResponse rsaRsp = new RsaResponse();
		Map map;
		try {
			map = RSAUtils.getKeys();
			RSAPublicKey publicKey = (RSAPublicKey) map.get("public");
			RSAPrivateKey privateKey = (RSAPrivateKey) map.get("private");
			getRequest().getSession().setAttribute("privateKey", privateKey);
			rsaRsp.publicKeyExponent = publicKey.getPublicExponent().toString(
					16);
			rsaRsp.publicKeyModulus = publicKey.getModulus().toString(16);
			rsaRsp.code = 200;
			getResponse().getWriter().write(JSONArray.toJSONString(rsaRsp));
		} catch (Exception e) {
			logger.info("", e);
			e.printStackTrace();
			rsaRsp.code = 0;
		}
		//getRequest().setAttribute("rsaRsp", rsaRsp);	
		
	}

 

 

 

//RSA解密
		String password=user.getUserpswd();
		RSAPrivateKey privateKey = (RSAPrivateKey)getRequest().getSession().getAttribute("privateKey");
		if(privateKey!=null){
			password = RSAUtils.decryptByPrivateKey(password, privateKey);  
			user.setUserpswd(password);
		}else{
			return "index";
		}
			

 

 

function loginSubmit(){
	var ele = $("#userpswd");
	rsaKey(ele);
	
	  $.ajax({
   cache: true,
   type: "POST",
   url:'<%=request.getContextPath()%>/user/login

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。



 
 
 谢谢您的赞助,我会做的更好!

 

 

 

本课题设计了一种利用Matlab平台开发的植物叶片健康状态识别方案,重点融合了色彩与纹理双重特征以实现对叶片病害的自动化判别。该系统构建了直观的图形操作界面,便于用户提交叶片影像并快速获得分析结论。Matlab作为具备高效数值计算与数据处理能力的工具,在图像分析与模式分类领域应用广泛,本项目正是借助其功能解决农业病害监测的实际问题。 在色彩特征分析方面,叶片影像的颜色分布常与其生理状态密切相关。通常,健康的叶片呈现绿色,而出现黄化、褐变等异常色彩往往指示病害或虫害的发生。Matlab提供了一系列图像处理函数,例如可通过色彩空间转换与直方图统计来量化颜色属性。通过计算各颜色通道的统计参数(如均值、标准差及主成分等),能够提取具有判别力的色彩特征,从而为不同病害类别的区分提供依据。 纹理特征则用于描述叶片表面的微观结构与形态变化,如病斑、皱缩或裂纹等。Matlab中的灰度共生矩阵计算函数可用于提取对比度、均匀性、相关性等纹理指标。此外,局部二值模式与Gabor滤波等方法也能从多尺度刻画纹理细节,进一步增强病害识别的鲁棒性。 系统的人机交互界面基于Matlab的图形用户界面开发环境实现。用户可通过该界面上传待检图像,系统将自动执行图像预处理、特征抽取与分类判断。采用的分类模型包括支持向量机、决策树等机器学习方法,通过对已标注样本的训练,模型能够依据新图像的特征向量预测其所属的病害类别。 此类课题设计有助于深化对Matlab编程、图像处理技术与模式识别原理的理解。通过完整实现从特征提取到分类决策的流程,学生能够将理论知识与实际应用相结合,提升解决复杂工程问题的能力。总体而言,该叶片病害检测系统涵盖了图像分析、特征融合、分类算法及界面开发等多个技术环节,为学习与掌握基于Matlab的智能检测技术提供了综合性实践案例。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值