Home > Back-end >  For help! Java RSA encryption, then use the Delphi, and vice versa...
For help! Java RSA encryption, then use the Delphi, and vice versa...

Time:09-20



Java client side their encryption and decryption are sure
Delphi there don't know much about, from the Internet, you can find their own encryption to decrypt all do
But the Java end private key encrypted results brought Delphi to restore public key to decrypt not
 
Package com. Algorithm;

Import the Java. IO. ByteArrayOutputStream;
Import the Java. Math. BigInteger;
Import the Java. Security. The Key;
Import the Java. Security. KeyFactory;
Import the Java. Security. KeyPair;
Import the Java. Security. KeyPairGenerator;
Import the Java. Security. NoSuchAlgorithmException;
Import the Java. Security. PrivateKey;
Import the Java. Security. PublicKey;
Import the Java. Security. Signature;
Import the Java. Security. Interfaces. RSAPrivateKey;
Import the Java. Security. Interfaces. RSAPublicKey;
Import the Java. Security. Spec. InvalidKeySpecException;
Import the Java. Security. Spec. PKCS8EncodedKeySpec;
Import the Java. Security. Spec. RSAPrivateKeySpec;
Import the Java. Security. Spec. RSAPublicKeySpec;
Import the Java. Security. Spec. X509EncodedKeySpec;
Import the Java. Util. HashMap;
Import the Java. Util. The Map;

The import javax.mail. Crypto. Cipher;

The import org.apache.com mons. Codec. Binary. Hex;

/* *
* RSA asymmetric encryption algorithm security coding component
* @ author Administrator
*
*/
Public abstract class RSACoder {
//asymmetric encryption key algorithms
Public static final String KEY_ALGORITHM="RSA";
//digital signature signature verification algorithm
Public static final String SIGNATURE_ALGORRITHM="SHA1withRSA";
//public key
Private static final String PUBLIC_KEY="RSAPublicKey";
//private key
Private static final String PRIVATE_KEY="RSAPrivateKey";
//RSA key length, defaults to 1024, the key length must be in multiples of 64, in the range between 521 ~ 65526
Private static final ints KEY_SIZE=512;

Private static final ints MAX_DECRYPT_BLOCK=64;



Public static byte [] getPublicKey (String modulus, String publicExponent) throws NoSuchAlgorithmException, InvalidKeySpecException {
BigInteger bigIntPrivateExponent=new BigInteger (publicExponent);
BigInteger bigIntModulus=new BigInteger (modulus);

RSAPublicKeySpec keySpec=new RSAPublicKeySpec (bigIntModulus bigIntPrivateExponent);
KeyFactory KeyFactory=KeyFactory. GetInstance (" RSA ");
PublicKey PublicKey=keyFactory. GeneratePublic (keySpec);
Return publicKey. GetEncoded ();
}
/* *
* public key decryption
* @ param data to decrypt data
* @ param key public key
* @ return byte [] to decrypt data
* @ throws the Exception
*/
Public static byte [] decryptByPublicKey (byte [] data, byte [] key) throws the Exception
{
//public key
X509EncodedKeySpec x509KeySpec=new X509EncodedKeySpec (key);
KeyFactory KeyFactory=KeyFactory. GetInstance (KEY_ALGORITHM);
//generated public key
PublicKey PublicKey=keyFactory. GeneratePublic (x509KeySpec);
//to decrypt the data
Cipher Cipher=Cipher. GetInstance (keyFactory getAlgorithm ());
Cipher. The init (cipher DECRYPT_MODE, publicKey);
Return cipher. DoFinal (data);
}

/* *
* private key encryption
* @ param data to be encrypted data
* @ param key private key
* @ return byte [] to encrypt data
* @ throws the Exception
*/
Public static byte [] encryptByPrivateKey (byte [] data, byte [] key) throws the Exception
{
//private key
PKCS8EncodedKeySpec pkcs8KeySpec=new PKCS8EncodedKeySpec (key);
KeyFactory KeyFactory=KeyFactory. GetInstance (KEY_ALGORITHM);
//private key generated
PrivateKey PrivateKey=keyFactory. GeneratePrivate (pkcs8KeySpec);
//the data encryption
Cipher Cipher=Cipher. GetInstance (keyFactory getAlgorithm ());
Cipher. The init (cipher ENCRYPT_MODE, privateKey);
Return cipher. DoFinal (data);
}
/* *
* private key encryption
* @ param data to be encrypted data
* @ param key private key
* @ return byte [] to encrypt data
* @ throws the Exception
*/
Public static byte [] encryptByPrivateKey (byte [] data, String key) throws the Exception
{
Return encryptByPrivateKey (data, getKey (key));
}

/* *
* get the private key
* @ param keyMap key Map
* @ return byte [] private key
* @ throws the Exception
*/
Public static byte [] getPrivateKey (Map KeyMap) throws the Exception
{
The Key Key=(Key) keyMap. Get (PRIVATE_KEY);
The return key. GetEncoded ();
}
/* *
* get the public key
* @ param keyMap key Map
* @ return byte [] public key
* @ throws the Exception
*/
Public static byte [] getPublicKey (Map KeyMap) throws the Exception
{
The Key Key=(Key) keyMap. Get (PUBLIC_KEY);
The return key. GetEncoded ();
}

/* *
* initialize the keys
* @ the return key Map
* @ throws the Exception
*/
Public static Map InitKey () throws the Exception
{
//instantiate the real key to the generator
KeyPairGenerator keyPairGen=KeyPairGenerator. GetInstance (KEY_ALGORITHM);
//initialize the key of generator
KeyPairGen. The initialize (KEY_SIZE);
//generate the key pair
KeyPair KeyPair=keyPairGen. GenerateKeyPair ();
//public key
RSAPublicKey publicKey=(RSAPublicKey) keyPair. GetPublic (); nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related