CFCA Decryption with PrivateKey


JAVA

public static final String CHAR_ENCODING = "UTF-8"; public static final String AES_ALGORITHM = "AES/ECB/PKCS5Padding"; public static final String RSA_ALGORITHM = "RSA/ECB/PKCS1Padding"; public static String decryptByPrivateKey(String cryptograph, String privateKey) throws Exception { Key key = getPrivateKeyByString(privateKey); Cipher cipher = Cipher.getInstance(CipherConfigure.RSA_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, key); byte[] b1 = Base64.decodeBase64(cryptograph.getBytes(CipherConfigure.CHAR_ENCODING)); byte[] b = cipher.doFinal(b1); return new String(b); } public static Key getPrivateKeyByString(String key) throws Exception { byte[] keyBytes = decryptBASE64(key); PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); Key privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec); return privateKey; }

PHP

function rsaPrivateDecode($data,$private_key,$password){ $prikey=file_get_contents($private_key); $encryptKey =$data['encryptKey']; $results=array(); openssl_pkcs12_read($prikey,$results,$password); $private_key=$results['pkey']; $pi_key = openssl_pkey_get_public($private_key); openssl_private_decrypt(base64_decode($encryptKey),$decrypted,$private_key); return $decrypted; }

C#

public static string CFCADencryption(string prviateKeyPath, string data, string pfxPassword) { X509Certificate2 pubcrt = new X509Certificate2(prviateKeyPath, pfxPassword, X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet); string keyprivate = pubcrt.PrivateKey.ToXmlString(true); string rsadata = RSADecrypt(keyprivate, data); return rsadata; }

GOLang

//Decrypt the private key according to the path and password of the private key certificate and then return the data func PrivateDecrypt(data string,path string,password string)(string,error){ var pfxData []byte var erro error var private *rsa.PrivateKey pfxData,err := ioutil.ReadFile(path) if err != nil { erro = err } var priv interface{} priv,_,err = pkcs12.Decode(pfxData, password) if err != nil { erro = err } private = priv.(*rsa.PrivateKey) ciphertext,err := base64.StdEncoding.DecodeString(data) if err != nil { erro = err } reslut, err := rsa.DecryptPKCS1v15(rand.Reader, private, ciphertext) if err != nil { erro = err } return string(reslut), erro }

Python

def gen_decrypt(encrydata): path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) doc = os.path.join(path, 'client.pem') with open(doc) as pk: key_data = pk.read() #print(key_data) key_data1 =key_data #print (key_data1) private_keyBytes =base64.b64decode(key_data1) #print (private_keyBytes) rsakey = RSA.importKey(private_keyBytes) cipher = Cipher_pkcs1_v1_5.new(rsakey) #print('encrydata:------'+encrydata) text = cipher.decrypt(base64.b64decode(encrydata.encode()),None) return text