工作中常常使用的几种加密以及签名的方式:RSA加密

import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;


public class RSACoder {
    private static final String Algorithm = "RSA";
    public static final String Signature_Algorithm_MD5 = "MD5withRSA";
    public static final String Signature_Algorithm_SHA1 = "SHA1WithRSA";

    
    public static String signMS(String data, String privateKeyFile) throws Exception { 
    	PrivateKey privateKey = getPrivateKeyPem(privateKeyFile);
        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKey.getEncoded());    
        KeyFactory keyFactory = KeyFactory.getInstance(Algorithm);   
        PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec);  
  
        Signature signature = Signature.getInstance(Signature_Algorithm_SHA1);  
        signature.initSign(priKey);  
        signature.update(data.getBytes("UTF-8"));   
        return Base64.encode(signature.sign());  
    }  
	
	private static PrivateKey getPrivateKeyPem(String fileName) throws Exception {
    	String key = readFile(fileName, "UTF-8");
    	byte[] keyBytes = buildPKCS8Key(key);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(Algorithm);
        PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
        return privateKey;       
    }
    
    private static byte[] buildPKCS8Key(String privateKey) throws IOException {
        if (privateKey.contains("-----BEGIN PRIVATE KEY-----")) {
            return Base64.decode(privateKey.replaceAll("-----\\w+ PRIVATE KEY-----", ""));
        } else if (privateKey.contains("-----BEGIN RSA PRIVATE KEY-----")) {
            final byte[] innerKey = Base64.decode(privateKey.replaceAll("-----\\w+ RSA PRIVATE KEY-----", ""));
            final byte[] result = new byte[innerKey.length + 26];
            System.arraycopy(Base64.decode("MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKY="), 0, result, 0, 26);
            System.arraycopy(BigInteger.valueOf(result.length - 4).toByteArray(), 0, result, 2, 2);
            System.arraycopy(BigInteger.valueOf(innerKey.length).toByteArray(), 0, result, 24, 2);
            System.arraycopy(innerKey, 0, result, 26, innerKey.length);
            return result;
        } else {
            return Base64.decode(privateKey);
        }
    }
    
    private static String readFile(String filePath, String charSet) throws Exception {
        FileInputStream fileInputStream = new FileInputStream(filePath);
        try {
            FileChannel fileChannel = fileInputStream.getChannel();
            ByteBuffer byteBuffer = ByteBuffer.allocate((int) fileChannel.size());
            fileChannel.read(byteBuffer);
            byteBuffer.flip();
            return new String(byteBuffer.array(), charSet);
        } finally {
            fileInputStream.close();
        }

    }
    
    public static boolean verifyMS(String data, String signData, String publicKeyFile)  
            throws Exception {
    	PublicKey publicKey = getPubliceKeyPem(publicKeyFile);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKey.getEncoded());    
		KeyFactory keyFactory = KeyFactory.getInstance(Algorithm);
        PublicKey pubKey = keyFactory.generatePublic(keySpec);    
        Signature signature = Signature.getInstance(Signature_Algorithm_SHA1);  
        signature.initVerify(pubKey);  
        signature.update(data.getBytes("UTF-8"));
        return signature.verify(Base64.decode(signData));  
    }
    
    private static PublicKey getPubliceKeyPem(String fileName) throws Exception {
    	String key = readFile(fileName, "UTF-8");
        key = key.replaceAll("\\-{5}[\\w\\s]+\\-{5}[\\r\\n|\\n]", "");
        KeyFactory keyFactory = KeyFactory.getInstance(Algorithm);
        byte[] encodedKey = Base64.decode(key);
        PublicKey pubKey = keyFactory
                .generatePublic(new X509EncodedKeySpec(encodedKey));
        return pubKey;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值