CFCA public key encryption


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 encryptByPublicKey(String source, String publicKey) throws Exception { Key key = getPublicKeyByString(publicKey); Cipher cipher = Cipher.getInstance(CipherConfigure.RSA_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] b = source.getBytes(CipherConfigure.CHAR_ENCODING); byte[] b1 = cipher.doFinal(b); return new String(Base64.encodeBase64(b1), CipherConfigure.CHAR_ENCODING); }

PHP

function rsaPublicEncode($public_key,$rands){ $encryptKey=file_get_contents($public_key); $pem = chunk_split(base64_encode($encryptKey),64,"\n");//Convert into a public key in PEM format $public_key = "-----BEGIN CERTIFICATE-----\n".$pem."-----END CERTIFICATE-----\n"; $pu_key = openssl_pkey_get_public($public_key); openssl_public_encrypt($rands,$encrypted,$pu_key); $encryptKey=base64_encode($encrypted); return $encryptKey; } public static Key getPublicKeyByString(String key) throws Exception { if (StringUtils.isBlank(key)) { LOGGER.error("key is null."); } byte[] keyBytes = decryptBASE64(key); X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); Key publicKey = keyFactory.generatePublic(x509EncodedKeySpec); return publicKey; } public static byte[] decryptBASE64(String key) throws Exception { return Base64.decodeBase64(key.getBytes(CipherConfigure.CHAR_ENCODING)); }

C#

public static string CFCAencryption(string publicKeyPath, string data){ X509Certificate2 pubcrt = new X509Certificate2(publicKeyPath); string keyPublic2 = pubcrt.PublicKey.Key.ToXmlString(false); string rsadata = RSAEncrypt(keyPublic2, data); return rsadata; }

GOLang

func PublicEncrypt(data string,path string)(string, error){ var public *rsa.PublicKey var erro error pubData,err := ioutil.ReadFile(path) if err != nil { fmt.Println("perr:", err) erro = err } pub := base64.StdEncoding.EncodeToString(pubData) var temp string split(pub,&temp) public_key := "\n-----BEGIN CERTIFICATE-----\n" + temp + "-----END CERTIFICATE-----\n" var publicKey = []byte(public_key) block, _ := pem.Decode(publicKey) if block == nil { erro = errors.New("public key error") } var cert* x509.Certificate cert, _ = x509.ParseCertificate(block.Bytes) public = cert.PublicKey.(*rsa.PublicKey) dataByte := []byte(data) cipherText, err := rsa.EncryptPKCS1v15(rand.Reader, public, dataByte) if err!=nil{ fmt.Println("cherr:", err) erro = err } //θΏ”ε›žε―†ζ–‡ base64Str := base64.StdEncoding.EncodeToString(cipherText) return base64Str, erro }

Python

def gen_encrypt(encrydata): path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) doc = os.path.join(path, 'server.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) cipher_text = base64.b64encode(cipher.encrypt(encrydata.encode())) #print (encrydata) return cipher_text