Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private const string AesIV256 = "IVFBWjJXU1gjRURDNFJGVg==";
- private const string AesKey256 = "ZzRtNDNuY3J5cHRrM3kuLi4=";//"UzNtM25jcnlwdGszeS4uLg==";
- public static string Encrypt(string text)
- {
- var sToEncrypt = text;
- var rj = new RijndaelManaged()
- {
- Padding = PaddingMode.Zeros,
- Mode = CipherMode.ECB,
- KeySize = 256,
- BlockSize = 256
- };
- var key = Convert.FromBase64String(AesKey256);
- var IV = Convert.FromBase64String(AesIV256);
- var encryptor = rj.CreateEncryptor(key, IV);
- var msEncrypt = new MemoryStream();
- var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
- var toEncrypt = Encoding.UTF8.GetBytes(sToEncrypt);
- csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
- csEncrypt.FlushFinalBlock();
- var encrypted = msEncrypt.ToArray();
- return (Convert.ToBase64String(encrypted));
- }
- private final static String SecretKey = "g4m43ncryptk3y..";
- public CryptoHelper() {
- //ivspec = new IvParameterSpec(Base64.decode(ivector.getBytes(),Base64.DEFAULT));
- keyspec = new SecretKeySpec(SecretKey.getBytes(), "AES");
- try {
- cipher = Cipher.getInstance("AES/ECB/ZeroBytePadding");
- } catch (NoSuchAlgorithmException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (NoSuchPaddingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- private byte[] decryptInternal(String code) throws Exception {
- if (code == null || code.length() == 0) {
- throw new Exception("Empty string");
- }
- byte[] decrypted = null;
- byte[] newDecrypted=null;
- try {
- cipher.init(Cipher.DECRYPT_MODE, keyspec);
- //decrypted = cipher.doFinal(Base64.decode(code,Base64.DEFAULT));
- decrypted= Base64.decode(code,Base64.DEFAULT);
- newDecrypted= cipher.doFinal(decrypted);
- } catch (Exception e) {
- throw new Exception("[decrypt] " + e.getMessage());
- }
- return newDecrypted;
- }
Add Comment
Please, Sign In to add comment