RSA加密解密坑

1、用NODE RSA JS 加密解密正常,用JAVA RSAUtils工具类加密解密正常。但是用node加密玩的java解密不了。原因:node默认的是

DEFAULT_ENCRYPTION_SCHEME = 'pkcs1_oaep'

     而java中默认的是pkcs1。

     另:NODE-RSA中其它的默认值:

      

 var SUPPORTED_HASH_ALGORITHMS = {
        node10: ['md4', 'md5', 'ripemd160', 'sha', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512'],
        node: ['md4', 'md5', 'ripemd160', 'sha', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512'],
        iojs: ['md4', 'md5', 'ripemd160', 'sha', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512'],
        browser: ['md5', 'ripemd160', 'sha1', 'sha256', 'sha512']
    };

    var DEFAULT_ENCRYPTION_SCHEME = 'pkcs1_oaep';
    var DEFAULT_SIGNING_SCHEME = 'pkcs1';

    var DEFAULT_EXPORT_FORMAT = 'private';
    var EXPORT_FORMAT_ALIASES = {
        'private': 'pkcs1-private-pem',
        'private-der': 'pkcs1-private-der',
        'public': 'pkcs8-public-pem',
        'public-der': 'pkcs8-public-der',
    };



2、java 中用的方法类RSAUtils中用的是从网上找来的类,里面密钥都是通过pkcs8加密的,所以要让node的代码和java的代码一致。

Node:

var NodeRSA = require('node-rsa');

var key=new NodeRSA({b: 1024});
key.setOptions({encryptionScheme: 'pkcs1'})

var publicDer = key.exportKey('public-der');
var privateDer = key.exportKey('pkcs8-private-der');

var publicKeyStr=publicDer.toString('base64')
var privateKeyStr=privateDer.toString('base64')

console.log(publicKeyStr)
console.log(privateKeyStr)

var str="马志宇|marsyoung|0907__"
// var y=key.encrypt(str,'base64')
// console.log(y)

var key2 = new NodeRSA();
key2.setOptions({encryptionScheme: 'pkcs1'})
key2.importKey("-----BEGIN PRIVATE KEY-----\n"+privateKeyStr+"-----END PRIVATE KEY-----","pkcs8-private")
var x=key2.encrypt(str, 'base64');
console.log(x)
console.log(key2.decrypt(x,'utf8'))

Java:(有点坑的类,网上还有更加新的)

package mobi.mixiong.util;

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

import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;


public class RSAUtils {

    /** *//**
     * 加密算法RSA
     */
    public static final String KEY_ALGORITHM = "RSA";
    
    /** *//**
     * 签名算法
     */
    public static final String SIGNATURE_ALGORITHM = "MD5withRSA";

    /** *//**
     * 获取公钥的key
     */
    private static final String PUBLIC_KEY = "RSAPublicKey";
    
    /** *//**
     * 获取私钥的key
     */
    private static final String PRIVATE_KEY = "RSAPrivateKey";
    
    /** *//**
     * RSA最大加密明文大小
     */
    private static final int MAX_ENCRYPT_BLOCK = 117;
    
    /** *//**
     * RSA最大解密密文大小
     */
    private static final int MAX_DECRYPT_BLOCK = 128;

    public static Map genKeyPair() throws Exception {
        KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
        keyPairGen.initialize(1024);
        KeyPair keyPair = keyPairGen.generateKeyPair();
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
        Map keyMap = new HashMap(2);
        keyMap.put(PUBLIC_KEY, publicKey);
        keyMap.put(PRIVATE_KEY, privateKey);
        return keyMap;
    }

    public static String sign(byte[] data, String privateKey) throws Exception {
        byte[] keyBytes =Base64.decodeBase64(privateKey);
        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec);
        Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
        signature.initSign(privateK);
        signature.update(data);
        return Base64.encodeBase64String(signature.sign());
    }

    public static boolean verify(byte[] data, String publicKey, String sign)
            throws Exception {
        byte[] keyBytes = Base64.decodeBase64(publicKey);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        PublicKey publicK = keyFactory.generatePublic(keySpec);
        Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
        signature.initVerify(publicK);
        signature.update(data);
        return signature.verify(Base64.decodeBase64(sign));
    }

    public static byte[] decryptByPrivateKey(byte[] encryptedData, String privateKey)
            throws Exception {
        byte[] keyBytes = Base64.decodeBase64(privateKey);
        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.DECRYPT_MODE, privateK);
        int inputLen = encryptedData.length;
        byte[] result = new byte[(encryptedData.length/128 + 1 )*117];
        int offSet = 0;
        byte[] cache;
        int i = 0;
        int len=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);
            }
            i++;
            offSet = i * MAX_DECRYPT_BLOCK;
            for(byte b:cache){
            	result[len++]=b;
            }
        }
        return result;
    }

    public static byte[] decryptByPublicKey(byte[] encryptedData, String publicKey)
            throws Exception {
        byte[] keyBytes = Base64.decodeBase64(publicKey);
        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        Key publicK = keyFactory.generatePublic(x509KeySpec);
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.DECRYPT_MODE, publicK);
        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 decryptedData;
    }

    public static byte[] encryptByPublicKey(byte[] data, String publicKey)
            throws Exception {
        byte[] keyBytes = Base64.decodeBase64(publicKey);
        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        Key publicK = keyFactory.generatePublic(x509KeySpec);
        // 对数据加密
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.ENCRYPT_MODE, publicK);
        int inputLen = data.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(data, offSet, MAX_ENCRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(data, offSet, inputLen - offSet);
            }
            out.write(cache, 0, cache.length);
            i++;
            offSet = i * MAX_ENCRYPT_BLOCK;
        }
        byte[] encryptedData = out.toByteArray();
        out.close();
        return encryptedData;
    }

    public static byte[] encryptByPrivateKey(byte[] data, String privateKey)
            throws Exception {
        byte[] keyBytes = Base64.decodeBase64(privateKey);
        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.ENCRYPT_MODE, privateK);
        int inputLen = data.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(data, offSet, MAX_ENCRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(data, offSet, inputLen - offSet);
            }
            out.write(cache, 0, cache.length);
            i++;
            offSet = i * MAX_ENCRYPT_BLOCK;
        }
        byte[] encryptedData = out.toByteArray();
        out.close();
        return encryptedData;
    }

    public static String getPrivateKey(Map keyMap)
            throws Exception {
        Key key = (Key) keyMap.get(PRIVATE_KEY);
        return Base64.encodeBase64String(key.getEncoded());
    }

    public static String getPublicKey(Map keyMap)
            throws Exception {
        Key key = (Key) keyMap.get(PUBLIC_KEY);
        return Base64.encodeBase64String(key.getEncoded());
    }
    
    public static void main(String[] args) {
		try {
//            Map keyPair=genKeyPair();
//            System.out.println(getPrivateKey(keyPair));
//            System.out.println(getPublicKey(keyPair));

			String pwd="马志宇|marsyoung|0907__";
			//encode
			byte[] b=encryptByPublicKey(pwd.getBytes("UTF-8"), "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCvYA5q75yqwIv+I8MiI6Aeq2iGxtv0zzUu4bmHW5LxswqZA1c3DjBy/rreirm1J7Y4qXo83yuG3f5VfvDH/0/lGf/YPb7pCKhBUIyamfEXEliN01bH4pq8JMsy28M8/cuE3LMw6Scfijejuplvg6Dy6Z+JY/k0ENXXItdrvBOvtQIDAQAB");
			String pwd_encoded=Base64.encodeBase64String(b);
			//decode
			System.out.println(pwd_encoded);
            String pwd_decoded=new String(decryptByPrivateKey(Base64.decodeBase64(pwd_encoded), "MIICeQIBADANBgkqhkiG9w0BAQEFAASCAmMwggJfAgEAAoGBAK9gDmrvnKrAi/4jwyIjoB6raIbG2/TPNS7huYdbkvGzCpkDVzcOMHL+ut6KubUntjipejzfK4bd/lV+8Mf/T+UZ/9g9vukIqEFQjJqZ8RcSWI3TVsfimrwkyzLbwzz9y4TcszDpJx+KN6O6mW+DoPLpn4lj+TQQ1dci12u8E6+1AgMBAAECgYEAkUVBTCVloi/TBtZ62jmmdiFIeXQaWYSWqloCg/RAGYRUwAD7ariPO4J0KsnBISVfFTqV7fxzIsma1xlKvHBprZ8g9CMr77EK8fYLCBTlXNZXKDJy2prvBC1UOufYTWZJZ4gIr7ef6lHTuPAHXs1MDWaT3oxAh+kwRgPzAIxUd3UCQQDZVWBY/txyqoxlBxyA+xivNVsLX8wrKeRR9ApCZCAdVF9sAGw0lXIAC2QYxKfbRW5MhRT5IsLqy6Ofmjuf9hJHAkEAzpOoR9EaCP1BBdlf3GU21wyDb2ndyYNXlqqfZDJSd7xgwupF6KL3QHA1EfknGY2lqH7BsoGT7/4aw4N8pf9QIwJBAIFZ/TQ6zD47H8BpkR97U1/Eo+GLLNtr3YahdxG4whxywTBot/48DFPWc8cCFN2+3z0P4EG/3L8eYM53X5F/RbUCQQC9m/ZoBNv6yEn4jm9YomZVuRyjFO8H2dB5cJ7gnvfnAzdlcRc90udTdCqiFqj24XbeU4qCyED7lacHMhclXlU1AkEAtJHqT4QX5wIaqLCbxq2IGUTh6bA3Y7CIBtw//L6W4GrWy/LXsUI4kpInC4o42CRjLUHc7MS0fq1rWKsJX7ljZA=="));
			System.out.println(pwd_decoded.trim());

        } catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}




3、Android这边加密过的数据,服务器端死活解密不了,原来android系统的RSA实现是"RSA/None/NoPadding",而标准JDK实现是"RSA/None/PKCS1Padding" ,这造成了在android机上加密后无法在服务器上解密的原因,所以在实现的时候这个一定要注意。



你可能感兴趣的:(BUG,java基础)