JAva暗号化アルゴリズム--RSA
6045 ワード
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
public class RSADecrypt {
public static Map createKeyPair() throws NoSuchAlgorithmException {
//
KeyPairGenerator generator = KeyPairGenerator.getInstance("rsa");
//
generator.initialize(1024);
//
KeyPair keyPair = generator.generateKeyPair();
//
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
String publicKeyStr = new String(publicKey.getEncoded());
String privateKeyStr = new String(privateKey.getEncoded());
// map
Map keyMap = new HashMap<>();
keyMap.put("publicKey", publicKeyStr);
keyMap.put("privateKey", privateKeyStr);
return keyMap;
}
/**
*
* @param publicKey
* @return
*/
public static RSAPublicKey getPublicKey(String publicKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
KeyFactory keyFactory = KeyFactory.getInstance("rsa");
//X509
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getBytes());
RSAPublicKey rsaPublicKey = (RSAPublicKey) keyFactory.generatePublic(x509EncodedKeySpec);
return rsaPublicKey;
}
/**
*
* @param privateKey
* @return
*/
public static RSAPrivateKey getPrivateKey(String privateKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
KeyFactory keyFactory = KeyFactory.getInstance("rsa");
//PKCS#8
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey.getBytes());
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyFactory.generatePrivate(pkcs8EncodedKeySpec);
return rsaPrivateKey;
}
/**
*
* @param src
* @param publicKey
* @return
*/
public static byte[] publicEncrypt(String src, RSAPublicKey publicKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance("rsa");
cipher.init(Cipher.ENCRYPT_MODE,publicKey);
byte[] encryptedData = cipher.doFinal(src.getBytes());
return encryptedData;
}
/**
*
* @param data
* @param privateKey
* @return
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
public static String privateDecrypt(byte[] data, RSAPrivateKey privateKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance("rsa");
cipher.init(Cipher.DECRYPT_MODE,privateKey);
byte[] result = cipher.doFinal(data);
return new String(result);
}
public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchPaddingException {
Map keyPair = createKeyPair();
RSAPublicKey publicKey = getPublicKey(keyPair.get("publicKey"));
RSAPrivateKey privateKey = getPrivateKey(keyPair.get("privateKey"));
String str = "hello world";
/**
* ,
*/
byte[] encryptedData = publicEncrypt(str,publicKey);
String result = privateDecrypt(encryptedData, privateKey);
/**
* ,
*/
byte[] encrypted = privateEncrypt(str, privateKey);
String source = publicDecrypt(encrypted, publicKey);
}
/**
*
* @param src
* @param privateKey
* @return
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
public static byte[] privateEncrypt(String src, RSAPrivateKey privateKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance("rsa");
cipher.init(Cipher.DECRYPT_MODE,privateKey);
byte[] result = cipher.doFinal(src.getBytes());
return result;
}
/**
*
* @param data
* @param publicKey
* @return
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
public static String publicDecrypt(byte[] data, RSAPublicKey publicKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance("rsa");
cipher.init(Cipher.DECRYPT_MODE,publicKey);
byte[] result = cipher.doFinal(data);
return new String(result);
}
}
上記の例では、鍵長1024を使用し、2048鍵を使用するには、初期化鍵でジェネレータをいくつか変更するだけで、他の部分を多重化することができる.
generator.initialize(2048);
この例は、基本的によく用いられるRSA復号化にすぎず、base 64に署名してもよい.
JAvaのデフォルトはPKCS 8、PKCS 1相互転送PKCS 8を使用します.https://blog.csdn.net/weixin_43203497/article/details/100903486