Guest User

Untitled

a guest
Jan 16th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. IV is equal to 81 114 150 34 27 90 82 1 78 188 221 119 110 240 56 183
  2. AES key is TXlwYXNzcGhyYXNlS2V5MQ==
  3. Unencrypted string: TextMustBe16BytesUsually
  4. Encrypted string: ZjE5NGRkMjY0MGU3NzJhNjRlZWI1MjlhYzlmNzk4N2NhNjE4ZjlmZDE5MmE3MWJjZDczMTBlZjBmNDQ3ZTUzMw==
  5. Unencrypted string: g�V��⓪����DĖ u���.Ӣ���B�#�!�v� ���ƭɐ
  6.  
  7. from Crypto.Cipher import AES
  8. import hashlib
  9. import sys
  10. import base64
  11. import binascii
  12. import Padding
  13.  
  14. val='TextMustBe16BytesUsually'
  15. password='ew+39INFhCg+rcNZsY/bd64hWoopaOA5m8r9mgfF/x0='
  16. ival= 12345678
  17.  
  18.  
  19. plaintext=val
  20.  
  21. def encrypt2(plaintext,key, mode,iv):
  22. encobj = AES.new(key,mode,iv)
  23. return(encobj.encrypt(plaintext))
  24.  
  25. def decrypt2(ciphertext,key, mode,iv):
  26. encobj = AES.new(key,mode,iv)
  27. return(encobj.decrypt(ciphertext))
  28.  
  29.  
  30. key = hashlib.sha256(password).digest()
  31.  
  32. iv= hex(ival)[2:8].zfill(16)
  33.  
  34.  
  35.  
  36. print "IV: "+ base64.b64encode(iv)
  37.  
  38. plaintext=val
  39. plaintext = Padding.appendPadding(plaintext,blocksize=Padding.AES_blocksize,mode=0)
  40.  
  41. ciphertext = encrypt2(plaintext,key,AES.MODE_CBC,iv)
  42. print ciphertext
  43. print "Cipher (CBC): "+ base64.b64encode(binascii.hexlify(bytearray(ciphertext)))
  44.  
  45. plaintext = decrypt2(ciphertext,key,AES.MODE_CBC,iv)
  46. plaintext = Padding.removePadding(plaintext,mode=0)
  47. print "Decrypt: "+plaintext
  48.  
  49. function Create-AesManagedObject($key, $IV) {
  50. $aesManaged = New-Object "System.Security.Cryptography.AesManaged"
  51. $aesManaged.Mode = [System.Security.Cryptography.CipherMode]::CBC
  52. $aesManaged.Padding = [System.Security.Cryptography.PaddingMode]::Zeros
  53. $aesManaged.BlockSize = 128
  54. $aesManaged.KeySize = 256
  55. if ($IV) {
  56. if ($IV.getType().Name -eq "String") {
  57. $aesManaged.IV = [System.Convert]::FromBase64String($IV)
  58. }
  59. else {
  60. $aesManaged.IV = $IV
  61. }
  62. }
  63. if ($key) {
  64. if ($key.getType().Name -eq "String") {
  65. $aesManaged.Key = [System.Convert]::FromBase64String($key)
  66. }
  67. else {
  68. $aesManaged.Key = $key
  69. }
  70. }
  71. $aesManaged
  72. }
  73.  
  74. function Create-AesKey() {
  75. $aesManaged = Create-AesManagedObject
  76. $aesManaged.GenerateKey()
  77. [System.Convert]::ToBase64String($aesManaged.Key)
  78. }
  79.  
  80. function Encrypt-String($key, $unencryptedString) {
  81. $bytes = [System.Text.Encoding]::UTF8.GetBytes($unencryptedString)
  82. $aesManaged = Create-AesManagedObject $key
  83. $encryptor = $aesManaged.CreateEncryptor()
  84. $encryptedData = $encryptor.TransformFinalBlock($bytes, 0, $bytes.Length);
  85. [byte[]] $fullData = $aesManaged.IV + $encryptedData
  86. $aesManaged.Dispose()
  87. [System.Convert]::ToBase64String($fullData)
  88. }
  89.  
  90. function Decrypt-String($key, $encryptedStringWithIV) {
  91. $bytes = [System.Convert]::FromBase64String($encryptedStringWithIV)
  92. $aesManaged = Create-AesManagedObject $key $IV
  93. $decryptor = $aesManaged.CreateDecryptor();
  94. $unencryptedData = $decryptor.TransformFinalBlock($bytes, 16, $bytes.Length - 16);
  95. $aesManaged.Dispose()
  96. [System.Text.Encoding]::UTF8.GetString($unencryptedData).Trim([char]0)
  97. }
  98.  
  99.  
  100. $key = "ew+39INFhCg+rcNZsY/bd64hWoopaOA5m8r9mgfF/x0="
  101. "KEY:"
  102. $key
  103. "IV:"
  104. $IV
  105. $unencryptedString = "TextMustBe16BytesUsually"
  106. #$encryptedString = Encrypt-String $key $unencryptedString
  107. "ENCRYPTED STRING"
  108. $encryptedString = "ZjE5NGRkMjY0MGU3NzJhNjRlZWI1MjlhYzlmNzk4N2NhNjE4ZjlmZDE5MmE3MWJjZDczMTBlZjBmNDQ3ZTUzMw=="
  109. $encryptedString
  110. $backToPlainText = Decrypt-String $key $encryptedString
  111. "Plain Text"
  112. $backToPlainText
Add Comment
Please, Sign In to add comment