Click here to Skip to main content
15,882,114 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello,
I have sample code in Java for your reference.

  public static String decryptOpenSSL(String key,String data) throws IOException, GeneralSecurityException{
	   
	    OpenSSL opensll=new OpenSSL();
	    InputStream  is=OpenSSL.decrypt("AES256", key.getBytes(), new ByteArrayInputStream(data.getBytes()));
	    Base64 encode=new Base64();
	    
	     
	    BufferedReader in = new BufferedReader(new InputStreamReader(is));
	    String inputLine;
	   StringBuffer response = new StringBuffer();

	   while ((inputLine = in.readLine()) != null) {
	    response.append(inputLine);
	   }
	   in.close();
	      System.out.println(response);
	      //return encode.encode(response.toString().getBytes()).toString();
	      return response.toString();
	   }

public static String encryptOpenSSL(String key,String data) throws IOException, GeneralSecurityException{
	  
  	OpenSSL opensll=new OpenSSL();
  	InputStream  is=OpenSSL.encrypt("AES256", key.getBytes(), new ByteArrayInputStream(data.getBytes()), true);
  	BufferedReader in = new BufferedReader(new InputStreamReader(is));
  	String inputLine;
		StringBuffer response = new StringBuffer();

		while ((inputLine = in.readLine()) != null) {
			response.append(inputLine);
		}
		in.close();
     System.out.println(response);
     return response.toString();
  } 


What I have tried:

I have tried openssl ags nuget but not work for me.
Posted
Updated 15-Aug-20 1:37am
Comments
Sandeep Mewara 15-Aug-20 7:37am    
Now sure of what is the issue?

Did you look at:
https://stackoverflow.com/questions/15470190/aes-256-cbc-encryption-with-openssl-and-decryption-in-c-sharp
https://medium.com/asecuritysite-when-bob-met-alice/producing-an-aes-cipher-to-match-openssl-in-c-94209ba9615b

1 solution

"but not work for me" - that's a pretty useless description - why would you do openssl AES encryption in C# when the framework has it build in - Aes Class (System.Security.Cryptography) | Microsoft Docs[^]
 
Share this answer
 
Comments
vardhman shah 15-Aug-20 8:06am    
I have one API and In API this type of encryption used. thats why.

Thanks
Garth J Lancaster 17-Aug-20 2:38am    
ok, well the 2nd link from Sandeep shows you how to achieve the same without a) converting Java -> C# or b) using an external/non C# Framework library

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900