Guest User

Untitled

a guest
Mar 21st, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. private const string AesIV256 = "IVFBWjJXU1gjRURDNFJGVg==";
  2. private const string AesKey256 = "ZzRtNDNuY3J5cHRrM3kuLi4=";//"UzNtM25jcnlwdGszeS4uLg==";
  3.  
  4.  
  5.  
  6. public static string Encrypt(string text)
  7. {
  8. var sToEncrypt = text;
  9. var rj = new RijndaelManaged()
  10. {
  11. Padding = PaddingMode.Zeros,
  12. Mode = CipherMode.ECB,
  13. KeySize = 256,
  14. BlockSize = 256
  15. };
  16.  
  17.  
  18. var key = Convert.FromBase64String(AesKey256);
  19. var IV = Convert.FromBase64String(AesIV256);
  20.  
  21. var encryptor = rj.CreateEncryptor(key, IV);
  22.  
  23. var msEncrypt = new MemoryStream();
  24. var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
  25.  
  26. var toEncrypt = Encoding.UTF8.GetBytes(sToEncrypt);
  27.  
  28. csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
  29. csEncrypt.FlushFinalBlock();
  30.  
  31. var encrypted = msEncrypt.ToArray();
  32.  
  33. return (Convert.ToBase64String(encrypted));
  34. }
  35.  
  36. private final static String SecretKey = "g4m43ncryptk3y..";
  37. public CryptoHelper() {
  38. //ivspec = new IvParameterSpec(Base64.decode(ivector.getBytes(),Base64.DEFAULT));
  39. keyspec = new SecretKeySpec(SecretKey.getBytes(), "AES");
  40.  
  41.  
  42. try {
  43. cipher = Cipher.getInstance("AES/ECB/ZeroBytePadding");
  44. } catch (NoSuchAlgorithmException e) {
  45. // TODO Auto-generated catch block
  46. e.printStackTrace();
  47. } catch (NoSuchPaddingException e) {
  48. // TODO Auto-generated catch block
  49. e.printStackTrace();
  50. }
  51. }
  52.  
  53. private byte[] decryptInternal(String code) throws Exception {
  54. if (code == null || code.length() == 0) {
  55. throw new Exception("Empty string");
  56. }
  57.  
  58. byte[] decrypted = null;
  59. byte[] newDecrypted=null;
  60. try {
  61.  
  62. cipher.init(Cipher.DECRYPT_MODE, keyspec);
  63. //decrypted = cipher.doFinal(Base64.decode(code,Base64.DEFAULT));
  64. decrypted= Base64.decode(code,Base64.DEFAULT);
  65. newDecrypted= cipher.doFinal(decrypted);
  66.  
  67. } catch (Exception e) {
  68. throw new Exception("[decrypt] " + e.getMessage());
  69. }
  70. return newDecrypted;
  71. }
Add Comment
Please, Sign In to add comment