반응형

 

RSA Encryption with Excel Part 1.

https://youtu.be/zxMNNwvhj94

RSA_example_screencast.xlsx
0.04MB

 

RSA Encryption with Excel Part 2.

https://youtu.be/o4KamplcSeE

 

RSA Encryption with Excel Part 3.

https://youtu.be/2Y8CKXy-eRI

 

Encryption of Confidential Numbers in Excel

https://youtu.be/04Gp_ud-gLs

 

http://exceldevelopmentplatform.blogspot.com/2017/07/cryptography-vba-code-for-wikipedias.html

 

Cryptography - VBA code for Wikipedia's RSA example

So, following on from studying some cryptography I can give some VBA code which implements RSA or at least the example given on the RSA Wik...

exceldevelopmentplatform.blogspot.com

Option Explicit
Option Private Module

Private Type udtPublicKey
    n As Currency
    e As Currency
End Type

Private Type udtPrivateKey
    n As Currency
    d As Currency
End Type

'***************************************************
'               .__
'  _____ _____  |__| ____
' /     \\__  \ |  |/    \
'|  Y Y  \/ __ \|  |   |  \
'|__|_|  (____  /__|___|  /
'      \/     \/        \/
'***************************************************

Private Sub Main()

    Dim p As Currency
    Dim q As Currency
    Dim n As Currency
    Dim lambda_n As Currency
    Dim e As Currency
    Dim d As Currency


    p = 61
    q = 53
    n = p * q
    lambda_n = Application.Lcm(p - 1, q - 1)
    e = 17
    Debug.Assert IsCoPrime(e, lambda_n)
    
    d = ModularMultiplicativeInverse(e, lambda_n)
    Debug.Assert e <> d

    Dim uPrivate As udtPrivateKey
    uPrivate.d = d
    uPrivate.n = n
    
    Dim uPublic As udtPublicKey
    uPublic.e = e
    uPublic.n = n
        
    '* m is the message to encrypt, it needs to be a number
    '* 65 is ASCII for "A"
    Dim m As Currency
    m = 65
    
    '* c is the encrypted message
    Dim c As Currency
    c = Encrypt(m, uPublic)
    
    '* m2 is the decrypted message
    Dim m2 As Currency
    m2 = Decrypt(c, uPrivate)
    
    '* and the decrypted message should match the original
    Debug.Assert m2 = m
     
End Sub


Private Function Encrypt(ByVal m As Currency, _
                    ByRef uPublic As udtPublicKey) As Currency
    If m > uPublic.n Then Err.Raise vbObjectError, , _
            "#text is bigger than modulus, no way to decipher!"
    
    Dim lLoop As Long
    Dim lResult As Currency
    lResult = 1
    For lLoop = 1 To uPublic.e
    
        lResult = ((lResult Mod uPublic.n) * (m Mod uPublic.n)) Mod uPublic.n
    Next lLoop
    Encrypt = lResult
End Function

Private Function Decrypt(ByVal c As Currency, _
                    ByRef uPrivate As udtPrivateKey) As Currency
    If c > uPrivate.n Then Err.Raise vbObjectError, , _
            "#text is bigger than modulus, no way to decipher!"
    Dim lLoop As Long
    Dim lResult As Currency
    lResult = 1
    For lLoop = 1 To uPrivate.d
        lResult = ((lResult Mod uPrivate.n) * (c Mod uPrivate.n)) Mod uPrivate.n
    Next lLoop

    
    Decrypt = lResult
End Function

Private Function IsCoPrime(ByVal a As Currency, ByVal b As Currency) As Boolean
    IsCoPrime = (Application.Gcd(a, b) = 1)
End Function

Private Function ModularMultiplicativeInverse(ByVal e As Currency, _
                    ByVal lambda_n As Currency)
    Dim lLoop As Currency
    For lLoop = 1 To lambda_n
        If lLoop <> e Then
            Dim lComp As Currency
            lComp = lLoop * e Mod lambda_n
            If lComp = 1 Then
                ModularMultiplicativeInverse = lLoop
                Exit Function
            End If
        End If
    Next
SingleExit:
End Function

 

https://gist.github.com/ooltcloud/f9b3d1f933d3d0b13cf6

Sub Main()

    ' 鍵生成
    Call GetKey(publicKey, privateKey)
    Debug.Print publicKey
    Debug.Print privateKey
    
    ' 暗号化
    encryptString = Encrypt(publicKey, "あいう")
    Debug.Print encryptString

    ' 復号
    planeString = Decrypt(privateKey, encryptString)
    Debug.Print planeString & "*"

End Sub


' 鍵生成
Sub GetKey(publicKey, privateKey)

    Set rsa = CreateObject("System.Security.Cryptography.RSACryptoServiceProvider")

    publicKey = rsa.ToXmlString(False)
    privateKey = rsa.ToXmlString(True)

End Sub

' 暗号化
Function Encrypt(key, value) As String

    Set rsa = CreateObject("System.Security.Cryptography.RSACryptoServiceProvider")

    ' 文字列を Byte 列に (UTF-16)
    Dim byteString() As Byte
    byteString = value

    ' 暗号化
    Call rsa.FromXmlString(key)
    encryptData = rsa.Encrypt(byteString, False)

    ' 暗号 Byte 列 を文字列 に
    encryptString = ""
    For Each v In encryptData
        encryptString = encryptString & Right("00" & Hex(v), 2)
    Next

    ' return
    Encrypt = encryptString

End Function

' 復号
Function Decrypt(key, value) As String

    Set rsa = CreateObject("System.Security.Cryptography.RSACryptoServiceProvider")

    ' 文字列を Byte 列 に
    byteLength = Len(value) \ 2
    
    Dim encryptData() As Byte
    ReDim encryptData(byteLength - 1)
        
    For i = 0 To byteLength - 1
        encryptData(i) = CByte("&H" & Mid(value, i * 2 + 1, 2))
    Next
   
   ' 復号
    Call rsa.FromXmlString(key)
    planeData = rsa.Decrypt(encryptData, False)
    
    ' return
    Decrypt = planeData

End Function
반응형
반응형

하이퍼링크 일괄삭제

 

Sub dhDeleteAllHyperLinks()
'워크북내에 있는 도형에 연결되어 있는 하이퍼링크 삭제
    Dim s As Object
    For Each s In Sheets
       s.Hyperlinks.Delete
    Next s
End Sub

Sub dhDeleteAllHyperLinks1()
'액티브시트에 있는 도형에 연결되어 있는 하이퍼링크 삭제
    Dim s As Shape
    For Each s In ActiveSheet.Shapes
       s.Hyperlink.Delete
    Next s
End Sub

 

Sub dhDeleteAllHyperLinks2()
'액티브시트에 있는 도형에 연결되어 있는 하이퍼링크 삭제
Dim s As Shape
On Error Resume Next
For Each s In ActiveSheet.Shapes
       s.Hyperlink.Delete
Next s
On Error GoTo 0
End Sub

 


'활성화된 시트의 하이퍼링크 일괄제거
'ActiveSheet.Hyperlinks.Delete

'워크북의 하이퍼링크 일괄제거
'Cells.Hyperlinks.Delete

 

하이퍼통합 문서1.xlsm
0.02MB

 

2022.08.03 - [엑셀] - HYPERLINK 연결과 HYPERLINK 함수

 

HYPERLINK 연결과 HYPERLINK 함수

HYPERLINK 연결과 HYPERLINK 함수 하이퍼링크 삽입 1. 하이퍼링크를 삽입할 셀을 선택합니다. 2. 삽입으로 이동하여 링크를 선택하고 링크 삽입을 선택하거나 Ctrl+K를 누릅니다. 3. 필요한 세부 정보

labellota.tistory.com

 

반응형

'VBA' 카테고리의 다른 글

닫힌 다각형의 내부점인지 외부점인지 확인하는 방법  (0) 2023.01.12
RSA Example with Excel  (0) 2022.12.05
Maclauric Seires for Exp(x)  (0) 2022.07.12
Excel VBA Dictionary  (0) 2022.06.21
엑셀 VBA 설정  (0) 2022.06.21
반응형

Maclauric Seires for Exp(x)

 

Exp(x)함수를 VBA로 구현하기

 

A Guide to Microsoft Excel 2013 for Scientists and Engineers

 

 

Function MacExp(x)
    Const Precision = 0.000000000000001
    MacExp = 0
    term = 1
    k = 1
    Do While Abs(term) > Precision
        MacExp = MacExp + term
        Debug.Print k; term; MacExp
        term = term * x / k
        k = k + 1
        If k = 100 Then
            MsgBox "Loop aborted, k > 100"
            Exit Do
        End If
    Loop
End Function

 

 

출처 : A Guide to Microsoft Excel 2013 for Scientists and Engineers

반응형

'VBA' 카테고리의 다른 글

RSA Example with Excel  (0) 2022.12.05
하이퍼링크 일괄삭제  (0) 2022.08.04
Excel VBA Dictionary  (0) 2022.06.21
엑셀 VBA 설정  (0) 2022.06.21
Atn함수를 이용하여 Pi계산하기  (0) 2022.04.25
반응형

https://excelmacromastery.com/vba-dictionary/

 

Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

This post contains everything you need to know about using the VBA Dictionary. There are tons of code examples including a real world application.

excelmacromastery.com

Excel VBA Dictionary

반응형

'VBA' 카테고리의 다른 글

하이퍼링크 일괄삭제  (0) 2022.08.04
Maclauric Seires for Exp(x)  (0) 2022.07.12
엑셀 VBA 설정  (0) 2022.06.21
Atn함수를 이용하여 Pi계산하기  (0) 2022.04.25
셀 값에서 숫자만 없애는 방법  (0) 2022.04.24
반응형

엑셀 VBA 설정

 

1. Excel 보안설정

 

1-1. Excel 옵션 > 보안센터

MIcrosoft Excel 보안 센터 > 보안 센터 설정

 

1-2.  보안센터 > 매크로 설정

매크로 설정 : 모든 매크로 포함(위험성 있는 코드가 실행될 수 있으므로 권장하지 않음) (E)

 

개발자 매크로 설정 : VBA 프로젝트 개체 모델에 안전하게 액세스할 수 있음(V)

 

1-3. 제한된 보기

체크 모두 해제

 

 

2. 개발도구 탭 설정

 

아래 그림에서 개발도구 탭이 아직 없다.

 

2-1. 파일 > 옵션 > 리본 사용자 지정

 

개발 도구 체크 후 확인

 

 

3. 개발도구 창 설정

 

보기 > 직접실행창 클릭

보기 > 도구모음 > 디버그

보기 > 도구모음 >  편집

 

엑셀 VBA 폰트 설정

도구 > 옵션

 

옵션 > 편집기 형식

글꼴 Consolas (영어)

크기 : 10~12가 적당하다.

 

consolas는 I(대문자 i), l(소문자 L), 1(숫자 1)의 구분이 용이하다.

 

 

이 내용은 엑사남 강의의 내용을 정리한 것이다.

https://youtu.be/OjDSqGqQ_uA

 

반응형

'VBA' 카테고리의 다른 글

Maclauric Seires for Exp(x)  (0) 2022.07.12
Excel VBA Dictionary  (0) 2022.06.21
Atn함수를 이용하여 Pi계산하기  (0) 2022.04.25
셀 값에서 숫자만 없애는 방법  (0) 2022.04.24
Maclaurin series를 이용한 Exp(x) 함수 계산  (0) 2022.04.20
반응형

Atn함수를 이용하여 Pi계산하기

Dim pi

pi = 4 * Atn(1) ' Calculate the value of pi.

 

 

pi = 4 *  Atn(1) 증명

pi = 4 * Atn(1) 증명
pi = 4 *  Atn(1) 증명

 

반응형

'VBA' 카테고리의 다른 글

Excel VBA Dictionary  (0) 2022.06.21
엑셀 VBA 설정  (0) 2022.06.21
셀 값에서 숫자만 없애는 방법  (0) 2022.04.24
Maclaurin series를 이용한 Exp(x) 함수 계산  (0) 2022.04.20
VBA에서 ":"의 사용법  (0) 2022.04.19
반응형

셀 값에서 숫자만 없애는 방법에 대해 알아보자.

 

1. SUBSTITUTE함수를 이용하는 방법

숫자는 0~9까지이므로 문자열에서 0부터 9까지 하나씩 공백으로 바꾸면 된다.

 

B2셀에 =SUBSTITUTE(A2,B$1,"") 를 입력한다.

B2셀을 복사하여 B2:K7 수식붙여넣기를 한다.

이렇게 하면 K열에는 숫자가 모두 지워진 문자열만 추출 할 수 있다.

 

2.VBA함수를 이용하는 방법

Option Explicit

Function Numdell(text)
    Dim i As Integer
    Dim text_i As String
    text_i = text
    
    For i = 0 To 9
        text_i = Application.WorksheetFunction.Substitute(text_i, i, "")
    Next i
    Numdell = text_i
End Function

Alt+F11를 눌러 VBA 에디터를 연다.

module를 추가한다.

함수코드를 입력한다.

L2셀에 =numdell(A2)를 입력하고 L7셀까지 복사하여 붙여넣는다.

 

문자치환.xlsm
0.01MB

반응형

'VBA' 카테고리의 다른 글

엑셀 VBA 설정  (0) 2022.06.21
Atn함수를 이용하여 Pi계산하기  (0) 2022.04.25
Maclaurin series를 이용한 Exp(x) 함수 계산  (0) 2022.04.20
VBA에서 ":"의 사용법  (0) 2022.04.19
Functions Visual Basic for Applications  (0) 2022.04.13
반응형

 

 

Do ... Loop문을 이용한다.

Function MacExp(x)
    Const Precision = 0.000000000000001
    MacExp = 0
    term = 1
    k = 1
    Do While Abs(term) > Precision
        MacExp = MacExp + term
        Debug.Print k; term; MacExp
        term = term * x / k
        k = k + 1
        If k = 100 Then
            MsgBox "Loop aborted, k > 100"
            Exit Do
        End If
    Loop
End Function

Exp함수 maclauric_seires.xlsm
0.08MB

반응형

'VBA' 카테고리의 다른 글

Atn함수를 이용하여 Pi계산하기  (0) 2022.04.25
셀 값에서 숫자만 없애는 방법  (0) 2022.04.24
VBA에서 ":"의 사용법  (0) 2022.04.19
Functions Visual Basic for Applications  (0) 2022.04.13
목표값찾기 단축 매크로  (0) 2022.03.29
반응형

아래코드에서 형광색으로 칠해 놓은 부분이 이해가 되지 않아 네이버 지식인에 문의를 했더니

아주 좋은 답변을 얻게 되었다.

 

Function Tritype(a, b, c)
    'Sort the three sides in ascending order
    If b > a Then holder = a: a = b: b = holder
    If c > a Then holder = a: a = c: c = holder
    If c > b Then holder = b: b = c: c = holder
    'Determin triangle type
    If a > b + c Then
                    Tritype = "None"
        ElseIf a * a = b * b + c * c Then
                    Tritype = "Right"
        ElseIf (a = b) And (b = c) Then
                    Tritype = "Equilateral"
        ElseIf (a = b) Or (b = c) Then
                    Tritype = "Isosceles"
        Else
                    Tritype = "Scalene"
    End If
End Function

 

:는 줄넘김을 하지 않고 코딩할때 사용한다.

If b > a Then holder = a: a = b: b = holder 는

 

아래와 같은 코드이다.

If b > a Then 

  holder = a

  a = b

  b = holder

 

 

따라서 아래 코드는

If b > a Then holder = a: a = b: b = holder
if c > a Then holder = a: a = c: c = holder
If c > b Then holder = b: b = c: c = holder

 

a값, b값, c값 중에서..

제일 큰값은 a에 저장..

다음 큰값은 b에 저장..

제일 작은 값은 c에 저장..

 

https://kin.naver.com/qna/detail.naver?d1id=1&dirId=102020101&docId=417594885&scrollTo=answer1 

 

엑셀VBA if함수 질문

요즘 공부하고 있는 책에 있는 내용입니다. 아래 형광색으로 칠해 놓은 부분이 이해가 되지 않습니다.If b > a Then holder = a: a = b: b = holde...

kin.naver.com

 

반응형

'VBA' 카테고리의 다른 글

셀 값에서 숫자만 없애는 방법  (0) 2022.04.24
Maclaurin series를 이용한 Exp(x) 함수 계산  (0) 2022.04.20
Functions Visual Basic for Applications  (0) 2022.04.13
목표값찾기 단축 매크로  (0) 2022.03.29
Worksheets  (0) 2022.03.28
반응형

Functions Visual Basic for Applications

 

Abs(x) 

The absolute value of x

 

Atn(x)

Inverse tangent of x. Other inverse functions may be computed using trigonometric identities such as Arcsin(X) = Atn(X / Sqr(-X*X + 1)). For more information, search Visual Basic Help for derived math functions

 

Cos(x) 

The cosine of x, where x is expressed in radians

 

Exp(x)

The value e^x

 

Fix(x) 

Returns the integer portion of x. If x is negative, Fix returns the first negative integer greater than or equal to x; for
example, Fix(-7.3) returns -7. See also Int

 

Int(x) 

Returns the integer portion of x. If x is negative, Int returns the first negative integer less than or equal to x; for
example, Int(-7.3) returns -8. See also Fix

 

Log(x) 

The value of the natural logarithm of x. Note how this differs from the worksheet function with the same name that, without a second argument, returns the logarithm to base 10. In VBA, the logarithm of x to base n may be
found using the statement y = Log(x)/Log(n)

 

Mod

In Visual Basic, this is an operator, not a function, but it is similar to the worksheet MOD function. It is used in the
form number Mod divisor and returns the remainder of number divided by divisor after rounding floating-point
values to integers. The worksheet function and the VBA operator return different values when the number and
divisor have opposite signs; see Help for details

 

Rnd(x) 

Returns a random number between 0 and 1

 

Sgn(x) 

Returns -1, 0, or 1 depending on whether x has a negative, zero, or positive value

 

Sin(x) 

The sine of x, where x is expressed in radians

 

Sqr(x) 

Square root of x

 

Tan(x) 

The tangent of x

반응형

'VBA' 카테고리의 다른 글

Maclaurin series를 이용한 Exp(x) 함수 계산  (0) 2022.04.20
VBA에서 ":"의 사용법  (0) 2022.04.19
목표값찾기 단축 매크로  (0) 2022.03.29
Worksheets  (0) 2022.03.28
주석 블록 설정 및 해제 단축키  (0) 2022.03.27

+ Recent posts