AES 加密 解密 Hex编码解码

本文介绍了一种使用RSA加密算法来保护微信小程序用户ID安全的实现方法。通过生成公钥和私钥对,实现了对用户ID的加密和解密过程,确保了数据在传输过程中的安全性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  最近做小程序用户ID验证的问题,文明传递有风险,所以想加密再传送用户ID,找了些加密的方法,

最开始想用MD5加密,以前有过这样的加密方式,现在换一种RES方式   对应的一组公钥和私钥加密方式,

 


/**
* @author lmc
* @version date:2018年9月27日 上午11:08:27
*/

package com.yitai.tms.weixin.rsa;

import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;

import org.apache.commons.codec.binary.Hex;


/**
 * @author lmc
 * @Email huyang@126.com
 * @date 2018年9月27日
 */
public class RSAwechat {
	
    private RSAPublicKey rsaPublicKey;
    private RSAPrivateKey rsaPrivateKey;

    public String encode(String src)
    {
        try
        {
            //初始化密钥
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
            keyPairGenerator.initialize(512);
            KeyPair keyPair = keyPairGenerator.generateKeyPair();
            rsaPublicKey = (RSAPublicKey)keyPair.getPublic();
            rsaPrivateKey = (RSAPrivateKey)keyPair.getPrivate();

            //私钥加密 公钥解密
            PKCS8EncodedKeySpec pkcs8EncodedKeySpec 
                = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE, privateKey);
            byte[] resultBytes = cipher.doFinal(src.getBytes());

            //私钥解密 公钥加密
//          X509EncodedKeySpec x509EncodedKeySpec =
//                  new X509EncodedKeySpec(rsaPublicKey.getEncoded());
//          KeyFactory keyFactory = KeyFactory.getInstance("RSA");
//          PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
//          Cipher cipher = Cipher.getInstance("RSA");
//          cipher.init(Cipher.ENCRYPT_MODE, publicKey);
//          byte[] resultBytes = cipher.doFinal(src.getBytes());

            return Hex.encodeHexString(resultBytes);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        return null;
    }

    public String decode(String src)
    {
        try
        {
            //私钥加密 公钥解密
            X509EncodedKeySpec x509EncodedKeySpec =
                    new X509EncodedKeySpec(rsaPublicKey.getEncoded());
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.DECRYPT_MODE, publicKey);
            byte[] resultBytes = cipher.doFinal(Hex.decodeHex(src.toCharArray()));

            //私钥解密 公钥加密
//          PKCS8EncodedKeySpec pkcs8EncodedKeySpec 
//              = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
//          KeyFactory keyFactory = KeyFactory.getInstance("RSA");
//          PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
//          Cipher cipher = Cipher.getInstance("RSA");
//          cipher.init(Cipher.DECRYPT_MODE, privateKey);
//          byte[] resultBytes = cipher.doFinal(Hex.decodeHex(src.toCharArray()));

            return new String(resultBytes);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }
	
}

加密方式


/**
* @author lmc
* @version date:2018年9月27日 上午11:11:11
*/

package com.yitai.tms.weixin.rsa;

/**
 * @author lmc
 * @Email huyang@126.com
 * @date 2018年9月27日
 */
public class RSAtool {

	
	private static 	RSAwechat rsa=new RSAwechat();
	public static String encRSAwechat(String token){
		return rsa.encode(token);
	}
	
	public static String decRSAwechat(String token){
		return rsa.decode(token);
	}
	
	public static void main(String args[]) 
	{
		System.out.println(RSAtool.encRSAwechat("asdfasdfasdfa"));
		String str = RSAtool.encRSAwechat("asdfasdfasdfa");
		System.out.println(RSAtool.decRSAwechat(str));
	}


}

 

hex编码实现AES加密解密功能示例可以按照以下步骤进行: 1. 导入所需的模块: ```python import binascii from Crypto.Cipher import AES from Crypto.Random import get_random_bytes ``` 2. 定义AES加密解密函数: ```python def encrypt(plain_text, key): cipher = AES.new(key, AES.MODE_EAX) nonce = cipher.nonce ciphertext, tag = cipher.encrypt_and_digest(plain_text.encode()) return binascii.hexlify(nonce + ciphertext + tag).decode() def decrypt(cipher_text, key): cipher_data = binascii.unhexlify(cipher_text) nonce = cipher_data[:16] ciphertext = cipher_data[16:-16] tag = cipher_data[-16:] cipher = AES.new(key, AES.MODE_EAX, nonce) decrypted_text = cipher.decrypt_and_verify(ciphertext, tag) return decrypted_text.decode() ``` 3. 生成随机密钥: ```python key = get_random_bytes(16) ``` 4. 加密解密示例数据: ```python plain_text = "This is a sample plaintext." cipher_text = encrypt(plain_text, key) decrypted_text = decrypt(cipher_text, key) ``` 完整的代码示例如下: ```python import binascii from Crypto.Cipher import AES from Crypto.Random import get_random_bytes def encrypt(plain_text, key): cipher = AES.new(key, AES.MODE_EAX) nonce = cipher.nonce ciphertext, tag = cipher.encrypt_and_digest(plain_text.encode()) return binascii.hexlify(nonce + ciphertext + tag).decode() def decrypt(cipher_text, key): cipher_data = binascii.unhexlify(cipher_text) nonce = cipher_data[:16] ciphertext = cipher_data[16:-16] tag = cipher_data[-16:] cipher = AES.new(key, AES.MODE_EAX, nonce) decrypted_text = cipher.decrypt_and_verify(ciphertext, tag) return decrypted_text.decode() key = get_random_bytes(16) plain_text = "This is a sample plaintext." cipher_text = encrypt(plain_text, key) decrypted_text = decrypt(cipher_text, key) print("Plain text:", plain_text) print("Encrypted text:", cipher_text) print("Decrypted text:", decrypted_text) ``` 这样,就可以使用hex编码实现AES加密解密功能了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值